summaryrefslogtreecommitdiff
path: root/src/pages/reset-password.jsx
blob: 0bf6f31ea9e398282a45b64e757452b11a6cbcee (plain)
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
import Alert from '@/core/components/elements/Alert/Alert'
import SimpleFooter from '@/core/components/elements/Footer/SimpleFooter'
import Link from '@/core/components/elements/Link/Link'
import BasicLayout from '@/core/components/layouts/BasicLayout'
import DesktopView from '@/core/components/views/DesktopView'
import MobileView from '@/core/components/views/MobileView'
import Image from 'next/image'
import { useRouter } from 'next/router'
import { useRef, useState } from 'react'
import IndoteknikLogo from '@/images/logo.png'
import odooApi from '@/core/api/odooApi'
import { setAuth } from '@/core/utils/auth'

export default function ResetPassword() {
  return (
    <>
      <MobileView>
        <FormComponent />
        <SimpleFooter />
      </MobileView>

      <DesktopView>
        <BasicLayout>
          <div className='container mx-auto'>
            <div className='w-1/2 mx-auto'>
              <FormComponent />
            </div>
          </div>
        </BasicLayout>
      </DesktopView>
    </>
  )
}

const FormComponent = () => {
  const router = useRouter()
  const { token = '' } = router.query

  const password = useRef(null)
  const retypePassword = useRef(null)

  const [isLoading, setIsLoading] = useState(false)
  const [isValidPassword, setIsValidPassword] = useState(false)
  const [alert, setAlert] = useState(null)

  const checkValidPassword = () => {
    const passwordVal = password.current.value
    const retypePasswordVal = retypePassword.current.value
    if (passwordVal && passwordVal == retypePasswordVal) {
      setIsValidPassword(true)
    } else {
      setIsValidPassword(false)
    }
  }

  const resetPasswordRequest = async (e) => {
    e.preventDefault()
    setIsLoading(true)
    const result = await odooApi('POST', '/api/v1/user/reset-password', {
      token,
      password: password.current.value
    })
    setIsLoading(false)
    password.current.value = ''
    retypePassword.current.value = ''
    if (result.success) {
      setAuth(result.user)
      setAlert({
        children: (
          <>
            Berhasil melakukan reset password,{' '}
            <Link className='text-gray_r-12 inline-block' href='/'>
              kembali ke halaman utama
            </Link>
          </>
        ),
        type: 'success'
      })
    } else {
      setAlert({
        children: (
          <>
            Gagal melakukan reset password, token tidak ditemukan. Buat permintaan reset password{' '}
            <Link className='text-gray_r-12 inline-block' href='/forgot-password'>
              disini
            </Link>
          </>
        ),
        type: 'info'
      })
    }
  }

  return (
    <div className='p-6 pt-10 md:px-0 flex flex-col items-center min-h-screen'>
      <Link href='/'>
        <Image src={IndoteknikLogo} alt='Logo Indoteknik' width={150} height={50} />
      </Link>

      <h1 className='text-2xl mt-4 font-semibold text-center'>Reset Kata Sandi Akun Indoteknik</h1>

      {alert && (
        <Alert className='text-center mt-4' type={alert.type}>
          {alert.children}
        </Alert>
      )}

      <form onSubmit={resetPasswordRequest} className='mt-6 w-full'>
        <input
          type='password'
          id='password'
          className='form-input w-full text-center'
          ref={password}
          onChange={checkValidPassword}
          placeholder='Masukan kata sandi baru'
          autoFocus
        />

        <input
          type='password'
          id='retypePassword'
          className='form-input w-full text-center mt-4'
          ref={retypePassword}
          onChange={checkValidPassword}
          placeholder='Masukan kembali kata sandi baru'
        />

        <button
          type='submit'
          disabled={!isValidPassword || isLoading}
          className='btn-yellow font-semibold mt-4 w-full'
        >
          {isLoading ? 'Loading...' : 'Reset Password'}
        </button>
      </form>
    </div>
  )
}