aboutsummaryrefslogtreecommitdiffstats
path: root/apps/ChangePassword/components/App.js
blob: ce32ffbba3fc659a568d6380416a5a8260630e40 (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
import styles from '../styles/ChangePassword.module.scss'
import { useState } from 'react'
import useSettings from 'hooks/useSettings'
import usePopup from 'hooks/usePopup'
import useApps from 'hooks/useApps'
import useUser from 'hooks/useUser'
import { close } from 'helpers/windowActions'
import { changePassword } from '../api'

const App = () => {
  const { t } = useSettings()
  const { user } = useUser()
  const { setApps } = useApps()
  const { setPopup } = usePopup()
  const [currPassError, setCurrPassError] = useState(false)
  const [newPassError, setNewPassError] = useState(false)

  const onSubmit = async (e) => {
    e.preventDefault()

    const {
      current_password: {value: currentPassword},
      new_password: { value: newPassword},
      confirm_password: {value: confirmPassword}
    } = e.currentTarget

    if (newPassword !== confirmPassword) {
      setPopup({
        content: t('change_password_wrong_new'),
        time: 2000,
        error: true
      })
      setNewPassError(true)

    } else if (newPassword === currentPassword) {
      setPopup({
        content: t('change_password_same_password'),
        time: 2000,
        error: true
      })
      setNewPassError(true)
    } else {
      try {
        await changePassword({_id: user._id, currentPassword, newPassword})
        setPopup({
          content: t('change_password_changed'),
          time: 2000,
        })
        close('ChangePassword', setApps)
      } catch(e) {
        if (e?.data?.error === 'Wrong password') {
          setPopup({
            content: t('change_password_wrong_current'),
            time: 2000,
            error: true
          })
          setCurrPassError(true)
        } else {
          setPopup({
            content: t('change_password_error'),
            time: 2000,
            error: true
          })
        }
      }
    }
  }

  return (
    <form className={styles.password} onSubmit={onSubmit}>
      <label className={styles.label} htmlFor='current_password'>
        {t('change_password_current')}
      </label>
      <input
        className={`${styles.input} ${currPassError ? styles.error : ''}`}
        id='current_password'
        type='password'
        name='current_password'
        title={t('change_password_current')}
        onChange={() => { currPassError && setCurrPassError(false) }}
        required
      />
      <label className={styles.label} htmlFor='new_password'>
        {t('change_password_new')}
      </label>
      <input
        className={`${styles.input} ${newPassError ? styles.error : ''}`}
        id='new_password'
        type='password'
        name='new_password'
        title={t('change_password_new')}
        onChange={() => { newPassError && setNewPassError(false) }}
        required
      />
      <input
        className={`${styles.input} ${newPassError ? styles.error : ''}`}
        type='password'
        name='confirm_password'
        placeholder={t('change_password_confirm')}
        title={t('change_password_confirm')}
        onChange={() => { newPassError && setNewPassError(false) }}
        required
      />
      <input
        className='window__button'
        type='submit'
        value={t('save')}
      />
    </form>
  )
}

export default App