summaryrefslogtreecommitdiff
path: root/src/lib/auth/components/Activate.jsx
blob: f67cc16861934e4785212fb752897e1d2367526a (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
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
import Image from 'next/image'
import Link from 'next/link'
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'
import IndoteknikLogo from '@/images/logo.png'
import axios from 'axios'
import { setAuth } from '@/core/utils/auth'
import Alert from '@/core/components/elements/Alert/Alert'
import odooApi from '@/core/api/odooApi'

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

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

  const [email, setEmail] = useState(router.query?.email || '')

  useEffect(() => {
    const activateIfTokenExist = async () => {
      if (token) {
        let isActivated = await odooApi('POST', '/api/v1/user/activation', { token })
        if (isActivated.activation) {
          setAuth(isActivated.user)
          setAlert({
            children: (
              <>
                Selamat, akun anda berhasil diaktifkan,{' '}
                <Link
                  className='text-gray_r-12'
                  href='/'
                >
                  kembali ke beranda
                </Link>
                .
              </>
            ),
            type: 'success'
          })
        } else {
          setAlert({
            children: (
              <>
                Mohon maaf token sudah tidak aktif, lakukan permintaan aktivasi akun kembali atau{' '}
                <Link
                  className='text-gray_r-12'
                  href='/login'
                >
                  masuk
                </Link>{' '}
                jika sudah memiliki akun.
              </>
            ),
            type: 'info'
          })
        }
      }
    }
    activateIfTokenExist()
  }, [token])

  useEffect(() => {
    if (router.query.email) setEmail(router.query.email)
  }, [router])

  const activationRequest = async (e) => {
    e.preventDefault()
    setIsLoading(true)
    let activationRequest = await axios.post(`${process.env.NEXT_PUBLIC_SELF_HOST}/api/activation-request`, {
      email
    })
    if (activationRequest.data.activationRequest) {
      setAlert({
        children: <>Mohon cek email anda untuk aktivasi akun Indoteknik</>,
        type: 'success'
      })
    } else {
      switch (activationRequest.data.reason) {
        case 'NOT_FOUND':
          setAlert({
            children: (
              <>
                Email tersebut belum terdaftar,{' '}
                <Link
                  className='text-gray_r-12'
                  href='/register'
                >
                  daftar sekarang
                </Link>
                .
              </>
            ),
            type: 'info'
          })
          break
        case 'ACTIVE':
          setAlert({
            children: (
              <>
                Email tersebut sudah terdaftar dan sudah aktif,{' '}
                <Link
                  className='text-gray_r-12'
                  href='/login'
                >
                  masuk sekarang
                </Link>
                .
              </>
            ),
            type: 'info'
          })
          break
      }
    }
    setIsLoading(false)
  }

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

      <h1 className='text-2xl mt-4 font-semibold'>Aktivasi Akun Indoteknik</h1>

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

      <form
        onSubmit={activationRequest}
        className='mt-6 w-full'
      >
        <input
          type='email'
          id='email'
          className='form-input w-full text-center'
          value={email}
          onChange={(e) => setEmail(e.target.value)}
          placeholder='Masukan alamat email'
          autoFocus
        />
        <button
          type='submit'
          disabled={email != ''}
          className='btn-yellow font-semibold mt-4 w-full'
        >
          {isLoading ? 'Loading...' : 'Aktivasi'}
        </button>
      </form>
    </div>
  )
}

export default Activate