|
| 1 | +import * as React from 'react'; |
| 2 | +import type { ActionFunctionArgs } from "@remix-run/node"; |
| 3 | +import { Form, useActionData, useNavigation } from "@remix-run/react"; |
| 4 | +import { Container, Grid, Box, TextField, Typography, Button, CircularProgress } from '@mui/material'; |
| 5 | +import { showToast } from '../utils/toastUtils'; |
| 6 | +import api from '../utils/api'; |
| 7 | + |
| 8 | +interface ActionData { |
| 9 | + errors?: { |
| 10 | + email?: string; |
| 11 | + }; |
| 12 | + success?: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +export const action = async ({ request }: ActionFunctionArgs) => { |
| 16 | + const formData = await request.formData(); |
| 17 | + const email = formData.get("email") as string; |
| 18 | + |
| 19 | + if (!email) { |
| 20 | + return { errors: { email: "Please enter your email address." } }; |
| 21 | + } |
| 22 | + |
| 23 | + try { |
| 24 | + await api.post('/users/password-reset/request/', { email }); |
| 25 | + return { success: true }; |
| 26 | + } catch (error) { |
| 27 | + return { |
| 28 | + errors: { |
| 29 | + email: error.response?.data?.error || 'Failed to process password reset request.' |
| 30 | + } |
| 31 | + }; |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +export default function ForgotPassword() { |
| 36 | + const actionData = useActionData<ActionData>(); |
| 37 | + const navigation = useNavigation(); |
| 38 | + const isLoading = navigation.state === "submitting"; |
| 39 | + |
| 40 | + if (actionData?.success) { |
| 41 | + return ( |
| 42 | + <Container> |
| 43 | + <Grid container justifyContent="center"> |
| 44 | + <Grid item xs={12} md={8} lg={4}> |
| 45 | + <Box> |
| 46 | + <Typography variant="h5" component="h2" sx={{ mb: 4 }}> |
| 47 | + Check Your Email |
| 48 | + </Typography> |
| 49 | + <Typography variant="body1" sx={{ mb: 4 }}> |
| 50 | + We've sent password reset instructions to your email address. |
| 51 | + Please check your inbox and follow the instructions to reset your password. |
| 52 | + </Typography> |
| 53 | + <Button |
| 54 | + variant="contained" |
| 55 | + component="a" |
| 56 | + href="/login" |
| 57 | + sx={{ mt: 3 }} |
| 58 | + > |
| 59 | + Return to Login |
| 60 | + </Button> |
| 61 | + </Box> |
| 62 | + </Grid> |
| 63 | + </Grid> |
| 64 | + </Container> |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + return ( |
| 69 | + <Container> |
| 70 | + <Grid container justifyContent="center"> |
| 71 | + <Grid item xs={12} md={8} lg={4}> |
| 72 | + <Box component={Form} method="post" noValidate> |
| 73 | + <Typography variant="h4" component="h2" sx={{ mb: 4 }}> |
| 74 | + Forgot Password |
| 75 | + </Typography> |
| 76 | + <Typography variant="body1" sx={{ mb: 4 }}> |
| 77 | + Enter your email address and we'll send you instructions to reset your password. |
| 78 | + </Typography> |
| 79 | + <TextField |
| 80 | + id="email" |
| 81 | + name="email" |
| 82 | + label="Email" |
| 83 | + type="email" |
| 84 | + variant="outlined" |
| 85 | + fullWidth |
| 86 | + required |
| 87 | + error={!!actionData?.errors?.email} |
| 88 | + helperText={actionData?.errors?.email} |
| 89 | + sx={{ mb: 4 }} |
| 90 | + /> |
| 91 | + <Button |
| 92 | + type="submit" |
| 93 | + variant="contained" |
| 94 | + fullWidth |
| 95 | + disabled={isLoading} |
| 96 | + sx={{ mb: 3 }} |
| 97 | + > |
| 98 | + {isLoading ? <CircularProgress size={24} /> : 'Send Reset Instructions'} |
| 99 | + </Button> |
| 100 | + <Button |
| 101 | + variant="text" |
| 102 | + fullWidth |
| 103 | + component="a" |
| 104 | + href="/login" |
| 105 | + > |
| 106 | + Back to Login |
| 107 | + </Button> |
| 108 | + </Box> |
| 109 | + </Grid> |
| 110 | + </Grid> |
| 111 | + </Container> |
| 112 | + ); |
| 113 | +} |
0 commit comments