You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
spa_instat_tv/src/features/AuthServiceApp/components/RecoveryPopup/hooks.tsx

74 lines
1.6 KiB

import {
MouseEvent,
ChangeEvent,
useState,
} from 'react'
import { isValidEmail } from 'features/AuthServiceApp/helpers/isValidEmail'
import { loginCheckChangePass } from 'features/AuthServiceApp/requests/loginCheck'
import { useParamsUrl } from '../../hooks/useParamsUrl'
export const useRecovery = (setIsModalOpen: (value: boolean) => void) => {
const { client_id, lang } = useParamsUrl()
const [isSendBtnDisabled, setIsSendBtnDisabled] = useState(true)
const [error, setError] = useState('')
const [email, setEmail] = useState('')
const [isFetching, setIsFetching] = useState(false)
const [isSendMessage, setIsSendMessage] = useState(false)
const onEmailChange = ({
target: { value },
}: ChangeEvent<HTMLInputElement>) => {
setError('')
setEmail(value)
if (isValidEmail(value)) {
setIsSendBtnDisabled(false)
} else {
setIsSendBtnDisabled(true)
}
}
const closePopup = () => {
setIsSendMessage(false)
setIsFetching(false)
setError('')
setEmail('')
setIsSendBtnDisabled(true)
setIsModalOpen(false)
}
const handleError = (err: string) => {
setError(err)
setIsFetching(false)
}
const handleSubmit = (event: MouseEvent) => {
event?.preventDefault()
event?.stopPropagation()
setIsFetching(true)
setIsSendBtnDisabled(true)
loginCheckChangePass(
client_id,
email,
lang,
)
.then(() => {
setIsFetching(false)
setIsSendMessage(true)
})
.catch(handleError)
}
return {
closePopup,
email,
error,
handleSubmit,
isFetching,
isSendBtnDisabled,
isSendMessage,
onEmailChange,
}
}