Skip to content

Commit 55bc1c2

Browse files
committed
feat: add page to resend confirmation email
1 parent b1dd7b6 commit 55bc1c2

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"use server"
2+
3+
import {z} from 'zod';
4+
import {FieldError, GeneralError, isFieldError} from "../../../lib/auth/guards";
5+
6+
export interface ResendConfirmationMailRequest {
7+
success: boolean;
8+
error?: GeneralError | FieldError;
9+
}
10+
11+
export const postResendConfirmationMail = async (_: ResendConfirmationMailRequest, formData: FormData): Promise<ResendConfirmationMailRequest> => {
12+
const schema = z.object({
13+
email: z.string().email()
14+
});
15+
16+
const parsed = schema.safeParse({
17+
email: formData.get('email')
18+
});
19+
20+
if (!parsed.success) {
21+
const fieldErrors = parsed.error.errors.reduce((acc, error) => {
22+
return {...acc, [error.path[0]]: error.message}
23+
}, {});
24+
25+
return {success: false, error: {status: 400, error: fieldErrors}};
26+
}
27+
28+
try {
29+
const response = await fetch(`${process.env.CC_API_URL}/accounts/resend-account-confirmation`, {
30+
method: "POST",
31+
headers: {
32+
'Content-Type': 'application/json'
33+
},
34+
body: JSON.stringify({
35+
email: parsed.data.email
36+
})
37+
});
38+
39+
if (!response.ok) {
40+
const error = await response.json();
41+
if (isFieldError(error)) {
42+
return {success: false, error: error};
43+
}
44+
45+
throw new Error('Unknown error');
46+
}
47+
48+
} catch (e) {
49+
return {success: false, error: {error: 'An unexpected error happened', status: 500}};
50+
}
51+
52+
return {success: true}
53+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"use client"
2+
3+
4+
import FormContainer from "../../common/uiLibrary/Layouters/formContainer";
5+
import TextField from "../../common/uiLibrary/forms/textField";
6+
import Button from "../../common/uiLibrary/Button";
7+
import ContactInformation from "../../(home)/contactInformation";
8+
import React from "react";
9+
import FullPage from "../../common/uiLibrary/Layouters/fullPage";
10+
import {useFormState} from "react-dom";
11+
import {postResendConfirmationMail, ResendConfirmationMailRequest} from "./actions";
12+
13+
const ResendConfirmationMail = () => {
14+
const initialState: ResendConfirmationMailRequest = {
15+
success: false,
16+
}
17+
18+
const [state, formAction] = useFormState(postResendConfirmationMail, initialState);
19+
20+
const resendForm = () => {
21+
return (
22+
<>
23+
{!state.success && state.error && errorMessage()}
24+
25+
<TextField
26+
id='email'
27+
name='email'
28+
label='Email'
29+
type='email'
30+
placeholder='[email protected]'
31+
required
32+
shadow
33+
/>
34+
<Button type="submit" className="mt-4 w-full" filled>Submit</Button>
35+
</>
36+
)
37+
}
38+
const errorMessage = () => {
39+
return (
40+
<div className='flex flex-col gap-4'>
41+
<h3 className="text-lg text-center font-medium text-red-700">Oops!</h3>
42+
<p>There was an unexpected error while trying to resend the confirmation email.</p>
43+
<p>Please try again later or contact us.</p>
44+
</div>
45+
)
46+
}
47+
48+
const successMessage = () => (
49+
<div className='flex flex-col gap-4'>
50+
<h3 className="text-lg text-center font-medium text-green-800">Success!</h3>
51+
<p>Your request has been processed successfully. If your account is found in our system and the email
52+
address you provided matches our records, we have sent a confirmation email to that address.</p>
53+
<p>Please check your inbox for the confirmation email. If you don't receive it within a few minutes, check
54+
your spam or junk folder. For further assistance, don't hesitate to contact us.</p>
55+
</div>
56+
);
57+
58+
return (
59+
<FullPage>
60+
<div className='flex-grow'>
61+
<FormContainer action={formAction} title='Resend password confirmation'>
62+
{state.success ? successMessage() : resendForm()}
63+
</FormContainer>
64+
</div>
65+
<ContactInformation/>
66+
</FullPage>
67+
)
68+
};
69+
70+
export default ResendConfirmationMail;

0 commit comments

Comments
 (0)