Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: added toast warning when user enter incorrect data during login #31

Merged
merged 1 commit into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/i18n/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@
"done": "Done",
"showMore": "Show more",
"hide": "Hide",
"deviceForFastLoginAdded": "The device has been added"
"deviceForFastLoginAdded": "The device has been added",
"code": {
"shouldContainSixDigits": "The code must contain exactly 6 digits."
},
"email": {
"emailCannotBeEmpty": "Please enter your email address.",
"invalidEmail": "Please enter a valid email address."
}
}
}
9 changes: 8 additions & 1 deletion src/i18n/ru/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@
"done": "Готово",
"showMore": "Показать больше",
"hide": "Скрыть",
"deviceForFastLoginAdded": "Устройство добавлено"
"deviceForFastLoginAdded": "Устройство добавлено",
"code": {
"shouldContainSixDigits": "Код должен содержать ровно 6 цифр"
},
"email": {
"emailCannotBeEmpty": "Пожалуйста, введите адрес электронной почты.",
"invalidEmail": "Пожалуйста, введите корректный адрес электронной почты."
}
}
}
41 changes: 27 additions & 14 deletions src/widget/Login/ui/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ import { Routes } from '~/shared/constants'
import { useToast } from '~/shared/hooks'
import { Typography } from '~/shared/ui'

const emailSchema = z.string().email()
const emailSchema = z
.string()
.min(1, 'email.emailCannotBeEmpty')
.email('email.invalidEmail')

const codeSchema = z
.string()
.regex(/^\d{6}$/, { message: 'Код должен содержать ровно 6 цифр' })
.regex(/^\d{6}$/, { message: 'code.shouldContainSixDigits' })

export const LoginWidget = () => {
const { t } = useCustomTranslation()
Expand All @@ -33,8 +36,13 @@ export const LoginWidget = () => {

const hintRef = useRef(null)

const { showPromiseToast, dismissAllToasts, showErrorToast, showInfoToast } =
useToast()
const {
showPromiseToast,
dismissAllToasts,
showErrorToast,
showInfoToast,
showWarningToast,
} = useToast()

const codeInputHeight = new Map([
[true, '40px'],
Expand Down Expand Up @@ -95,16 +103,17 @@ export const LoginWidget = () => {
const safeParse = emailSchema.safeParse(email)

if (safeParse.error) {
throw new Error(JSON.stringify(safeParse.error))
showWarningToast(t(safeParse.error.errors[0].message))
throw new Error(safeParse.error.errors[0].message)
}

return safeParse.data
}, [email])
}, [email, showWarningToast])

const loginOTP = useCallback(async () => {
const emailValue = processEmailValue()
const safeEmail = processEmailValue()

showPromiseToast(tryLoginPromise(emailValue), {
return showPromiseToast(tryLoginPromise(safeEmail), {
pending: t('sendingEmail'),
success: t('emailSent'),
error: t('failedToSendEmail'),
Expand All @@ -121,20 +130,24 @@ export const LoginWidget = () => {
const codeSafeParse = codeSchema.safeParse(code)

if (codeSafeParse.error) {
throw new Error(JSON.stringify(codeSafeParse.error))
return showWarningToast(t(codeSafeParse.error.errors[0].message))
}

showPromiseToast(confirmLoginPromise(emailValue, codeSafeParse.data), {
pending: t('sendingCode'),
success: t('codeSent'),
error: t('failedToSendCode'),
})
return showPromiseToast(
confirmLoginPromise(emailValue, codeSafeParse.data),
{
pending: t('sendingCode'),
success: t('codeSent'),
error: t('failedToSendCode'),
},
)
}, [
code,
confirmLoginPromise,
isEmailSent,
processEmailValue,
showPromiseToast,
showWarningToast,
t,
])

Expand Down
Loading