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' 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 mismatchConfirmation = (newPassword !== confirmPassword) && newPassword !== '' // // const login = useLogin() // const handleChangePassword = async (e: React.SyntheticEvent) => { console.log('handleChangePassword'); e.preventDefault() // if (!dirty && !disabled) { setDirty(true) handleValidation() // } if (newPassword !== confirmPassword) { setDisabled(true) setMessage('Passwords must match!') return; } else { } try { setLoading(true) setMessage('') await changePassword({ email, currentPassword, newPassword, confirmPassword }) setLoading(false) closeModal() } catch (error) { console.dir(error); const { errors } = error; console.dir(errors); setMessage(errors[0].message) setLoading(false) } } // Halt unless the password is valid, and both new password fields match! 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( !validPassword || newPassword != confirmPassword ) } }, [newPassword, confirmPassword, dirty]) useEffect(() => { handleValidation() }, [handleValidation]) return (
{message && (
{message}
)} { mismatchConfirmation && 'New password and confirmation must match.' }
) } export default ChangePassword