chore: better Prettier config for easier reviews
This commit is contained in:
@ -1,45 +1,45 @@
|
||||
import useTranslation from 'next-translate/useTranslation'
|
||||
import type { Error } from 'react-component-form'
|
||||
import useTranslation from "next-translate/useTranslation"
|
||||
import type { Error } from "react-component-form"
|
||||
|
||||
const knownErrorKeywords = ['minLength', 'maxLength', 'format']
|
||||
const knownErrorKeywords = ["minLength", "maxLength", "format"]
|
||||
|
||||
const getErrorTranslationKey = (error: Error): string => {
|
||||
if (knownErrorKeywords.includes(error?.keyword)) {
|
||||
if (
|
||||
error.keyword === 'minLength' &&
|
||||
typeof error.data === 'string' &&
|
||||
error.keyword === "minLength" &&
|
||||
typeof error.data === "string" &&
|
||||
error.data.length === 0
|
||||
) {
|
||||
return 'common:required'
|
||||
return "common:required"
|
||||
}
|
||||
if (error.keyword === 'format') {
|
||||
if (error.params['format'] === 'email') {
|
||||
return 'common:invalid-email'
|
||||
if (error.keyword === "format") {
|
||||
if (error.params["format"] === "email") {
|
||||
return "common:invalid-email"
|
||||
}
|
||||
return 'common:invalid'
|
||||
return "common:invalid"
|
||||
}
|
||||
return `common:${error.keyword}`
|
||||
}
|
||||
return 'common:invalid'
|
||||
return "common:invalid"
|
||||
}
|
||||
|
||||
export const useFormTranslation = () => {
|
||||
const { t } = useTranslation()
|
||||
|
||||
const getErrorTranslation = (
|
||||
error: Error | undefined
|
||||
error: Error | undefined,
|
||||
): string | undefined => {
|
||||
if (error != null) {
|
||||
return t(getErrorTranslationKey(error)).replace(
|
||||
'{expected}',
|
||||
error?.params?.['limit']
|
||||
"{expected}",
|
||||
error?.params?.["limit"],
|
||||
)
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
const getFirstErrorTranslation = (
|
||||
errors: Error[] | undefined
|
||||
errors: Error[] | undefined,
|
||||
): string | undefined => {
|
||||
if (errors != null) {
|
||||
return getErrorTranslation(errors[0])
|
||||
|
Reference in New Issue
Block a user