2024-08-01 13:01:03 -05:00
|
|
|
import type { ApiError } from "./client"
|
2025-02-17 19:33:00 +00:00
|
|
|
import useCustomToast from "./hooks/useCustomToast"
|
2024-08-01 13:01:03 -05:00
|
|
|
|
2024-03-28 19:49:02 -05:00
|
|
|
export const emailPattern = {
|
2024-04-15 16:19:40 -05:00
|
|
|
value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i,
|
2024-03-28 19:49:02 -05:00
|
|
|
message: "Invalid email address",
|
|
|
|
}
|
2024-04-01 22:53:33 -05:00
|
|
|
|
2024-04-15 16:19:40 -05:00
|
|
|
export const namePattern = {
|
|
|
|
value: /^[A-Za-z\s\u00C0-\u017F]{1,30}$/,
|
|
|
|
message: "Invalid name",
|
|
|
|
}
|
|
|
|
|
2024-04-01 22:53:33 -05:00
|
|
|
export const passwordRules = (isRequired = true) => {
|
|
|
|
const rules: any = {
|
|
|
|
minLength: {
|
|
|
|
value: 8,
|
|
|
|
message: "Password must be at least 8 characters",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isRequired) {
|
|
|
|
rules.required = "Password is required"
|
|
|
|
}
|
|
|
|
|
|
|
|
return rules
|
|
|
|
}
|
|
|
|
|
|
|
|
export const confirmPasswordRules = (
|
|
|
|
getValues: () => any,
|
|
|
|
isRequired = true,
|
|
|
|
) => {
|
|
|
|
const rules: any = {
|
2024-04-15 16:19:40 -05:00
|
|
|
validate: (value: string) => {
|
2024-04-15 16:34:50 -05:00
|
|
|
const password = getValues().password || getValues().new_password
|
|
|
|
return value === password ? true : "The passwords do not match"
|
|
|
|
},
|
2024-04-01 22:53:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isRequired) {
|
2024-05-20 13:30:18 -05:00
|
|
|
rules.required = "Password confirmation is required"
|
2024-04-01 22:53:33 -05:00
|
|
|
}
|
|
|
|
|
2024-05-20 13:30:18 -05:00
|
|
|
return rules
|
2024-04-01 22:53:33 -05:00
|
|
|
}
|
2024-08-01 13:01:03 -05:00
|
|
|
|
2025-02-17 19:33:00 +00:00
|
|
|
export const handleError = (err: ApiError) => {
|
|
|
|
const { showErrorToast } = useCustomToast()
|
2024-08-01 13:01:03 -05:00
|
|
|
const errDetail = (err.body as any)?.detail
|
|
|
|
let errorMessage = errDetail || "Something went wrong."
|
|
|
|
if (Array.isArray(errDetail) && errDetail.length > 0) {
|
|
|
|
errorMessage = errDetail[0].msg
|
|
|
|
}
|
2025-02-17 19:33:00 +00:00
|
|
|
showErrorToast(errorMessage)
|
2024-08-01 13:01:03 -05:00
|
|
|
}
|