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 all 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.

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

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

const mutation = gql`
mutation ($input: UpdateUserContactMethodInput!) {
updateUserContactMethod(input: $input)
}
`

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

export default function UserContactMethodEditDialog({
onClose,
contactMethodID,
}: {
onClose: () => void
contactMethodID: string
}): React.ReactNode {
const [value, setValue] = useState<Value | null>(null)
const [{ data, fetching }] = useQuery({
query,
variables: { id: contactMethodID },
})
const [commit, status] = useMutation(mutation)
const { error } = status

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

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
}
commit({
variables: {
input: {
...updates,
id: contactMethodID,
},
},
}).then((result) => {
if (result.errors) return
onClose()
})
}}
form={
<UserContactMethodForm
errors={fieldErrs}
disabled={fetching}
edit
value={value || defaultValue}
onChange={(value) => setValue(value)}
/>
}
/>
)
}