import { FC, useEffect, useState, useCallback } from 'react' import { Logo, Button, Input } from '@components/ui' import { useUI } from '@components/ui/context' import useCustomer from '@framework/customer/use-customer' import useChangePassword from '@framework/auth/use-change-password' import changePassword from '@framework/api/endpoints/change-password/change-password' // import { validate } from 'email-validator' interface Props { } const ChangePassword: FC = () => { const changePassword = useChangePassword() // return (
FOo
) // const { data: customer } = useCustomer() // if (customer) { console.log(customer) } else { return <>; } // Form State const [email, _setEmail] = useState(customer.email as string) const [currentPassword, setCurrentPassword] = useState('') const [newPassword, setNewPassword] = useState('') const [confirmPassword, setConfirmPassword] = useState('') const [loading, setLoading] = useState(false) const [message, setMessage] = useState('') const [dirty, setDirty] = useState(false) const [disabled, setDisabled] = useState(false) const { setModalView, closeModal } = useUI() // // const login = useLogin() // const handleChangePassword = async (e: React.SyntheticEvent) => { console.log('handleChangePassword'); e.preventDefault() if (!dirty && !disabled) { setDirty(true) handleValidation() } try { setLoading(true) setMessage('') await changePassword({ email, currentPassword, newPassword }) setLoading(false) closeModal() } catch (error) { console.dir(error); const { errors } = error; console.dir(errors); setMessage(errors[0].message) setLoading(false) } } const handleValidation = useCallback(() => { // Test for Alphanumeric password const validPassword = /^(?=.*[a-zA-Z])(?=.*[0-9])/.test(newPassword) // Unable to send form unless fields are valid. if (dirty) { setDisabled( newPassword.length < 7 || !validPassword ||newPassword != confirmPassword ) } }, [newPassword, confirmPassword, dirty]) useEffect(() => { handleValidation() }, [handleValidation]) return (
{message && (
{message}. Did you {` `} setModalView('FORGOT_VIEW')} > forgot your password?
)}
) } export default ChangePassword