-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathutils.ts
More file actions
73 lines (61 loc) · 1.84 KB
/
utils.ts
File metadata and controls
73 lines (61 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import type { ApiError } from './client'
import i18n, { i18nPromise } from './i18n'
let t: (key: string) => string = () => ''
i18nPromise.then(() => {
t = i18n.t.bind(i18n)
})
export const emailPattern = {
value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i,
message: t('general.errors.invalidEmail'),
}
export const namePattern = {
value: /^[A-Za-z\s\u00C0-\u017F]{1,30}$/,
message: t('general.errors.invalidName'),
}
export const passwordRules = (isRequired = true) => {
const rules: {
minLength: { value: number; message: string }
required?: string
} = {
minLength: {
value: 8,
message: t('general.errors.passwordMinCharacters'),
},
}
if (isRequired) {
rules.required = t('general.errors.passwordIsRequired')
}
return rules
}
export const confirmPasswordRules = (getValues: () => unknown, isRequired = true) => {
const rules: {
validate: (value: string) => boolean | string
required?: string
} = {
validate: (value: string) => {
const formValues = getValues() as {
password?: string
new_password?: string
}
const password = formValues.password || formValues.new_password
return value === password ? true : t('general.errors.passwordsDoNotMatch')
},
}
if (isRequired) {
rules.required = t('general.errors.passwordConfirmationIsRequired')
}
return rules
}
export const handleError = (
err: ApiError,
showToast: (title: string, message: string, type: string) => void,
) => {
const errDetail = (err.body as { detail?: string | { msg: string }[] })?.detail
let errorMessage = t('general.errors.default')
if (typeof errDetail === 'string') {
errorMessage = errDetail
} else if (Array.isArray(errDetail) && errDetail.length > 0) {
errorMessage = errDetail[0].msg
}
showToast(t('general.errors.error'), errorMessage, 'error')
}