@@ -15,44 +15,42 @@ import { graphql } from "../../gql";
15
15
import { graphqlRequest } from "../../graphql" ;
16
16
17
17
const ADD_EMAIL_MUTATION = graphql ( /* GraphQL */ `
18
- mutation AddEmail($userId: ID !, $email : String!) {
19
- addEmail (input: { userId : $userId, email : $email }) {
18
+ mutation AddEmail($email: String !, $language : String!) {
19
+ startEmailAuthentication (input: { email : $email, language : $language }) {
20
20
status
21
21
violations
22
- email {
22
+ authentication {
23
23
id
24
- ...UserEmail_email
25
24
}
26
25
}
27
26
}
28
27
` ) ;
29
28
30
29
const AddEmailForm : React . FC < {
31
- userId : string ;
32
30
onAdd : ( id : string ) => Promise < void > ;
33
- } > = ( { userId , onAdd } ) => {
34
- const { t } = useTranslation ( ) ;
31
+ } > = ( { onAdd } ) => {
32
+ const { t, i18n } = useTranslation ( ) ;
35
33
const queryClient = useQueryClient ( ) ;
36
34
const addEmail = useMutation ( {
37
- mutationFn : ( { userId , email } : { userId : string ; email : string } ) =>
35
+ mutationFn : ( { email , language } : { email : string ; language : string } ) =>
38
36
graphqlRequest ( {
39
37
query : ADD_EMAIL_MUTATION ,
40
- variables : { userId , email } ,
38
+ variables : { email , language } ,
41
39
} ) ,
42
40
onSuccess : async ( data ) => {
43
41
queryClient . invalidateQueries ( { queryKey : [ "userEmails" ] } ) ;
44
42
45
43
// Don't clear the form if the email was invalid or already exists
46
- if ( data . addEmail . status !== "ADDED " ) {
44
+ if ( data . startEmailAuthentication . status !== "STARTED " ) {
47
45
return ;
48
46
}
49
47
50
- if ( ! data . addEmail . email ?. id ) {
48
+ if ( ! data . startEmailAuthentication . authentication ?. id ) {
51
49
throw new Error ( "Unexpected response from server" ) ;
52
50
}
53
51
54
52
// Call the onAdd callback
55
- await onAdd ( data . addEmail . email ?. id ) ;
53
+ await onAdd ( data . startEmailAuthentication . authentication ?. id ) ;
56
54
} ,
57
55
} ) ;
58
56
@@ -63,34 +61,35 @@ const AddEmailForm: React.FC<{
63
61
64
62
const formData = new FormData ( e . currentTarget ) ;
65
63
const email = formData . get ( "input" ) as string ;
66
- addEmail . mutate ( { userId , email } ) ;
64
+ addEmail . mutate ( { email , language : i18n . languages [ 0 ] } ) ;
67
65
} ;
68
66
69
- const status = addEmail . data ?. addEmail . status ?? null ;
70
- const violations = addEmail . data ?. addEmail . violations ?? [ ] ;
67
+ const status = addEmail . data ?. startEmailAuthentication . status ?? null ;
68
+ const violations = addEmail . data ?. startEmailAuthentication . violations ?? [ ] ;
71
69
72
70
return (
73
71
< EditInPlace
74
72
onSave = { handleSubmit }
75
73
required
76
74
type = "email"
77
- serverInvalid = {
78
- status === "INVALID" || status === "EXISTS" || status === "DENIED"
79
- }
75
+ serverInvalid = { ! ! status && status !== "STARTED" }
80
76
label = { t ( "frontend.add_email_form.email_field_label" ) }
81
77
helpLabel = { t ( "frontend.add_email_form.email_field_help" ) }
82
78
saveButtonLabel = { t ( "action.save" ) }
83
79
savingLabel = { t ( "common.saving" ) }
84
80
savedLabel = { t ( "common.saved" ) }
85
81
cancelButtonLabel = { t ( "action.cancel" ) }
86
82
>
87
- < ErrorMessage match = "typeMismatch" forceMatch = { status === "INVALID" } >
83
+ < ErrorMessage
84
+ match = "typeMismatch"
85
+ forceMatch = { status === "INVALID_EMAIL_ADDRESS" }
86
+ >
88
87
{ t ( "frontend.add_email_form.email_invalid_error" ) }
89
88
</ ErrorMessage >
90
89
91
- { status === "EXISTS " && (
90
+ { status === "IN_USE " && (
92
91
< ErrorMessage >
93
- { t ( "frontend.add_email_form.email_exists_error " ) }
92
+ { t ( "frontend.add_email_form.email_in_use_error " ) }
94
93
</ ErrorMessage >
95
94
) }
96
95
0 commit comments