aboutsummaryrefslogtreecommitdiffstats
path: root/src/views/Authorization/ForgotPasswordView.jsx
blob: f964a6852b69709298f75e85c4e93de529b76108 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// create boilerplate for ResetPasswordView:
import LogoSVG from '@/assets/logo.svg'
import {
  Box,
  Button,
  Container,
  FormControl,
  FormHelperText,
  Input,
  Sheet,
  Snackbar,
  Typography,
} from '@mui/joy'
import { useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { API_URL } from './../../Config'

const ForgotPasswordView = () => {
  const navigate = useNavigate()
  // const [showLoginSnackbar, setShowLoginSnackbar] = useState(false)
  // const [snackbarMessage, setSnackbarMessage] = useState('')
  const [resetStatusOk, setResetStatusOk] = useState(null)
  const [email, setEmail] = useState('')
  const [emailError, setEmailError] = useState(null)

  const validateEmail = email => {
    return !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i.test(email)
  }

  const handleSubmit = async () => {
    if (!email) {
      return setEmailError('Email is required')
    }

    // validate email:
    if (validateEmail(email)) {
      setEmailError('Please enter a valid email address')
      return
    }

    if (emailError) {
      return
    }

    try {
      const response = await fetch(`${API_URL}/auth/reset`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ email: email }),
      })

      if (response.ok) {
        setResetStatusOk(true)
        //  wait 3 seconds and then redirect to login:
      } else {
        setResetStatusOk(false)
      }
    } catch (error) {
      setResetStatusOk(false)
    }
  }

  const handleEmailChange = e => {
    setEmail(e.target.value)
    if (validateEmail(e.target.value)) {
      setEmailError('Please enter a valid email address')
    } else {
      setEmailError(null)
    }
  }

  return (
    <Container
      component='main'
      maxWidth='xs'

      // make content center in the middle of the page:
    >
      <Box
        sx={{
          marginTop: 4,
          display: 'flex',
          flexDirection: 'column',

          justifyContent: 'space-between',
          alignItems: 'center',
        }}
      >
        <Sheet
          component='form'
          sx={{
            mt: 1,
            width: '100%',
            display: 'flex',
            flexDirection: 'column',
            alignItems: 'center',
            padding: 2,
            borderRadius: '8px',
            boxShadow: 'md',
            minHeight: '70vh',
            justifyContent: 'space-between',
            justifyItems: 'center',
          }}
        >
          <Box>
            <img src={LogoSVG} alt='logo' width='128px' height='128px' />
            {/* <Logo /> */}
            <Typography level='h2'>
              Done
              <span
                style={{
                  color: '#06b6d4',
                }}
              >
                tick
              </span>
            </Typography>
          </Box>
          {/* HERE */}
          <Box sx={{ textAlign: 'center' }}></Box>
          {resetStatusOk === null && (
            <form onSubmit={handleSubmit}>
              <div className='grid gap-6'>
                <Typography level='body2' gutterBottom>
                  Enter your email, and we'll send you a link to get into your
                  account.
                </Typography>
                <FormControl error={emailError !== null}>
                  <Input
                    placeholder='Email'
                    type='email'
                    variant='soft'
                    fullWidth
                    size='lg'
                    value={email}
                    onChange={handleEmailChange}
                    error={emailError !== null}
                    onKeyDown={e => {
                      if (e.key === 'Enter') {
                        e.preventDefault()
                        handleSubmit()
                      }
                    }}
                  />
                  <FormHelperText>{emailError}</FormHelperText>
                </FormControl>
                <Box>
                  <Button
                    variant='solid'
                    size='lg'
                    fullWidth
                    sx={{
                      mb: 1,
                    }}
                    onClick={handleSubmit}
                  >
                    Reset Password
                  </Button>
                  <Button
                    fullWidth
                    size='lg'
                    variant='soft'
                    sx={{
                      width: '100%',
                      border: 'moccasin',
                      borderRadius: '8px',
                    }}
                    onClick={() => {
                      navigate('/login')
                    }}
                    color='neutral'
                  >
                    Back to Login
                  </Button>
                </Box>
              </div>
            </form>
          )}
          {resetStatusOk != null && (
            <>
              <Box mt={-30}>
                <Typography level='body-md'>
                  if there is an account associated with the email you entered,
                  you will receive an email with instructions on how to reset
                  your
                </Typography>
              </Box>
              <Button
                variant='soft'
                size='lg'
                sx={{ position: 'relative', bottom: '0' }}
                onClick={() => {
                  navigate('/login')
                }}
                fullWidth
              >
                Go to Login
              </Button>
            </>
          )}
          <Snackbar
            open={resetStatusOk ? resetStatusOk : resetStatusOk === false}
            autoHideDuration={5000}
            onClose={() => {
              if (resetStatusOk) {
                navigate('/login')
              }
            }}
          >
            {resetStatusOk
              ? 'Reset email sent, check your email'
              : 'Reset email failed, try again later'}
          </Snackbar>
        </Sheet>
      </Box>
    </Container>
  )
}

export default ForgotPasswordView