Skip to content

Commit e49d5a3

Browse files
committed
Use the new GraphQL APIs in the frontend to add emails
1 parent 70e8c53 commit e49d5a3

16 files changed

+420
-620
lines changed

frontend/locales/en.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@
4949
},
5050
"add_email_form": {
5151
"email_denied_error": "The entered email is not allowed by the server policy",
52-
"email_exists_error": "The entered email is already added to this account",
5352
"email_field_help": "Add an alternative email you can use to access this account.",
5453
"email_field_label": "Add email",
54+
"email_in_use_error": "The entered email is already in use",
5555
"email_invalid_error": "The entered email is invalid"
5656
},
5757
"browser_session_details": {

frontend/src/components/PageHeading/PageHeading.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ type Props = {
1212
Icon: React.ComponentType<React.SVGAttributes<SVGElement>>;
1313
invalid?: boolean;
1414
success?: boolean;
15-
title: string;
16-
subtitle?: string;
15+
title: React.ReactNode;
16+
subtitle?: React.ReactNode;
1717
};
1818

1919
const PageHeading: React.FC<Props> = ({

frontend/src/components/UserProfile/AddEmailForm.tsx

+20-21
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,42 @@ import { graphql } from "../../gql";
1515
import { graphqlRequest } from "../../graphql";
1616

1717
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 }) {
2020
status
2121
violations
22-
email {
22+
authentication {
2323
id
24-
...UserEmail_email
2524
}
2625
}
2726
}
2827
`);
2928

3029
const AddEmailForm: React.FC<{
31-
userId: string;
3230
onAdd: (id: string) => Promise<void>;
33-
}> = ({ userId, onAdd }) => {
34-
const { t } = useTranslation();
31+
}> = ({ onAdd }) => {
32+
const { t, i18n } = useTranslation();
3533
const queryClient = useQueryClient();
3634
const addEmail = useMutation({
37-
mutationFn: ({ userId, email }: { userId: string; email: string }) =>
35+
mutationFn: ({ email, language }: { email: string; language: string }) =>
3836
graphqlRequest({
3937
query: ADD_EMAIL_MUTATION,
40-
variables: { userId, email },
38+
variables: { email, language },
4139
}),
4240
onSuccess: async (data) => {
4341
queryClient.invalidateQueries({ queryKey: ["userEmails"] });
4442

4543
// 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") {
4745
return;
4846
}
4947

50-
if (!data.addEmail.email?.id) {
48+
if (!data.startEmailAuthentication.authentication?.id) {
5149
throw new Error("Unexpected response from server");
5250
}
5351

5452
// Call the onAdd callback
55-
await onAdd(data.addEmail.email?.id);
53+
await onAdd(data.startEmailAuthentication.authentication?.id);
5654
},
5755
});
5856

@@ -63,34 +61,35 @@ const AddEmailForm: React.FC<{
6361

6462
const formData = new FormData(e.currentTarget);
6563
const email = formData.get("input") as string;
66-
addEmail.mutate({ userId, email });
64+
addEmail.mutate({ email, language: i18n.languages[0] });
6765
};
6866

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 ?? [];
7169

7270
return (
7371
<EditInPlace
7472
onSave={handleSubmit}
7573
required
7674
type="email"
77-
serverInvalid={
78-
status === "INVALID" || status === "EXISTS" || status === "DENIED"
79-
}
75+
serverInvalid={!!status && status !== "STARTED"}
8076
label={t("frontend.add_email_form.email_field_label")}
8177
helpLabel={t("frontend.add_email_form.email_field_help")}
8278
saveButtonLabel={t("action.save")}
8379
savingLabel={t("common.saving")}
8480
savedLabel={t("common.saved")}
8581
cancelButtonLabel={t("action.cancel")}
8682
>
87-
<ErrorMessage match="typeMismatch" forceMatch={status === "INVALID"}>
83+
<ErrorMessage
84+
match="typeMismatch"
85+
forceMatch={status === "INVALID_EMAIL_ADDRESS"}
86+
>
8887
{t("frontend.add_email_form.email_invalid_error")}
8988
</ErrorMessage>
9089

91-
{status === "EXISTS" && (
90+
{status === "IN_USE" && (
9291
<ErrorMessage>
93-
{t("frontend.add_email_form.email_exists_error")}
92+
{t("frontend.add_email_form.email_in_use_error")}
9493
</ErrorMessage>
9594
)}
9695

frontend/src/components/VerifyEmail/VerifyEmail.module.css

-32
This file was deleted.

frontend/src/components/VerifyEmail/VerifyEmail.tsx

-169
This file was deleted.

0 commit comments

Comments
 (0)