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/profile: convert UserContactMethodVerificationForm to typescript #3494

Merged
merged 3 commits into from
Dec 4, 2023
Merged
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useEffect } from 'react'
import { useMutation, gql } from '@apollo/client'
import p from 'prop-types'
import { useMutation, gql } from 'urql'
import Grid from '@mui/material/Grid'
import TextField from '@mui/material/TextField'
import LoadingButton from '../loading/components/LoadingButton'
Expand Down Expand Up @@ -29,36 +28,50 @@ const useStyles = makeStyles({
},
})

export default function UserContactMethodVerificationForm(props) {
interface UserContactMethodVerificationFormProps {
contactMethodID: string
disabled: boolean
errors: Error[]
onChange: (value: { code: string }) => void
setSendError: (err: string) => void
value: {
code: string
}
}
export default function UserContactMethodVerificationForm(
props: UserContactMethodVerificationFormProps,
): React.ReactNode {
const classes = useStyles()

const [sendCode, sendCodeStatus] = useMutation(sendVerificationCodeMutation, {
variables: {
const [sendCodeStatus, sendCode] = useMutation(sendVerificationCodeMutation)

function sendAndCatch(): void {
// Clear error on new actions.
props.setSendError('')
sendCode({
input: {
contactMethodID: props.contactMethodID,
},
},
})

function sendAndCatch() {
// Clear error on new actions.
props.setSendError(null)
sendCode().catch((err) => props.setSendError(err.message))
}).catch((err) => props.setSendError(err.message))
}

// Attempt to send a code on load, but it's ok if it fails.
//
// We only want to display an error in response to a user action.
useEffect(() => {
sendCode().catch(() => {})
sendCode({
input: {
contactMethodID: props.contactMethodID,
},
}).catch(() => {})
}, [])

return (
<FormContainer optionalLabels {...props}>
<Grid container spacing={2}>
<Grid item className={classes.sendGridItem}>
<LoadingButton
loading={sendCodeStatus.loading}
loading={sendCodeStatus.fetching}
disabled={props.disabled}
buttonText='Resend Code'
noSubmit
Expand All @@ -74,26 +87,10 @@ export default function UserContactMethodVerificationForm(props) {
component={TextField}
type='number'
step='1'
mapOnChangeValue={(value) => value.toString()}
mapOnChangeValue={(value: number) => value.toString()}
/>
</Grid>
</Grid>
</FormContainer>
)
}

UserContactMethodVerificationForm.propTypes = {
contactMethodID: p.string.isRequired,
disabled: p.bool.isRequired,
errors: p.arrayOf(
p.shape({
field: p.oneOf(['code']).isRequired,
message: p.string.isRequired,
}),
),
onChange: p.func.isRequired,
setSendError: p.func.isRequired,
value: p.shape({
code: p.string.isRequired,
}).isRequired,
}