From 308d07785f811ff470d0e90b11680926a823027b Mon Sep 17 00:00:00 2001 From: piotrruss Date: Sun, 29 May 2022 17:45:54 +0100 Subject: add change password option --- apps/ChangePassword/components/App.js | 113 ++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 apps/ChangePassword/components/App.js (limited to 'apps/ChangePassword/components/App.js') diff --git a/apps/ChangePassword/components/App.js b/apps/ChangePassword/components/App.js new file mode 100644 index 0000000..ce32ffb --- /dev/null +++ b/apps/ChangePassword/components/App.js @@ -0,0 +1,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 ( +
+ + { currPassError && setCurrPassError(false) }} + required + /> + + { newPassError && setNewPassError(false) }} + required + /> + { newPassError && setNewPassError(false) }} + required + /> + +
+ ) +} + +export default App -- cgit v1.2.3