This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
website/hooks/useFormTranslation.ts

61 lines
1.6 KiB
TypeScript
Raw Permalink Normal View History

import useTranslation from 'next-translate/useTranslation'
import type { Error } from 'react-component-form'
const knownErrorKeywords = ['minLength', 'maxLength', 'format']
const getErrorTranslationKey = (error: Error): string => {
if (knownErrorKeywords.includes(error?.keyword)) {
if (
error.keyword === 'minLength' &&
typeof error.data === 'string' &&
error.data.length === 0
) {
return 'errors:required'
}
if (error.keyword === 'format') {
2023-01-11 17:39:09 +01:00
if (error.params['format'] === 'email') {
return 'errors:invalid-email'
}
return 'errors:invalid'
}
return `errors:${error.keyword}`
}
return 'errors:invalid'
}
export type GetErrorTranslation = (
error: Error | undefined
) => string | undefined
export type GetFirstErrorTranslation = (
errors: Error[] | undefined
) => string | undefined
export interface UseFormTranslationResult {
getErrorTranslation: GetErrorTranslation
getFirstErrorTranslation: GetFirstErrorTranslation
}
export const useFormTranslation = (): UseFormTranslationResult => {
const { t } = useTranslation()
const getErrorTranslation: GetErrorTranslation = (error) => {
if (error != null) {
return t(getErrorTranslationKey(error)).replace(
'{expected}',
2023-01-11 17:39:09 +01:00
error?.params?.['limit']
)
}
return undefined
}
const getFirstErrorTranslation: GetFirstErrorTranslation = (errors) => {
if (errors != null) {
return getErrorTranslation(errors[0])
}
return undefined
}
return { getFirstErrorTranslation, getErrorTranslation }
}