Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: convert UserContactMethodEditDialog to typescript #3472

Merged
merged 5 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 0 additions & 100 deletions web/src/app/users/UserContactMethodEditDialog.js

This file was deleted.

96 changes: 96 additions & 0 deletions web/src/app/users/UserContactMethodEditDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import React, { useState } from 'react'
import { gql, useMutation, useQuery } from 'urql'
import FormDialog from '../dialogs/FormDialog'
import { fieldErrors, nonFieldErrors } from '../util/errutil'
import UserContactMethodForm from './UserContactMethodForm'
import { pick } from 'lodash'
import { ContactMethodType, StatusUpdateState } from '../../schema'

type Value = {
name: string
type: ContactMethodType
value: string
statusUpdates?: StatusUpdateState
}

const query = gql`
query ($id: ID!) {
userContactMethod(id: $id) {
id
name
type
value
statusUpdates
}
}
`

const mutation = gql`
mutation ($input: UpdateUserContactMethodInput!) {
updateUserContactMethod(input: $input)
}
`
function UserContactMethodEditDialog(props: {
contactMethodID: string
onClose: () => void
}): JSX.Element {
const { contactMethodID, onClose } = props

const [{ data, fetching }] = useQuery({
query,
variables: { id: contactMethodID },
})

const defaultValue = {
name: data.userContactMethod.name,
type: data.userContactMethod.type,
value: data.userContactMethod.value,
statusUpdates: data.userContactMethod.statusUpdates,
}

const [mutationStatus, commit] = useMutation(mutation)
const { error } = mutationStatus
const [value, setValue] = useState<Value | null>(null)

const fieldErrs = fieldErrors(error)

return (
<FormDialog
title='Edit Contact Method'
loading={fetching}
errors={nonFieldErrors(error)}
onClose={onClose}
onSubmit={() => {
const updates = pick(value, 'name', 'statusUpdates') || {}
// the form uses the 'statusUpdates' enum but the mutation simply
// needs to know if the status updates should be enabled or not via
// the 'enableStatusUpdates' boolean
if ('statusUpdates' in updates) {
delete Object.assign(updates, {
enableStatusUpdates: updates.statusUpdates === 'ENABLED',
}).statusUpdates
}
return commit({
input: {
...updates,
id: contactMethodID,
},
}).then((res) => {
if (res.error) return
props.onClose()
})
}}
form={
<UserContactMethodForm
errors={fieldErrs}
disabled={fetching}
edit
value={value || defaultValue}
onChange={(value) => setValue(value)}
/>
}
/>
)
}

export default UserContactMethodEditDialog