extract message
This commit is contained in:
@@ -13,6 +13,7 @@ import { useOtpRequest } from '@/app/auth/login/hooks/useAuthData'
|
||||
import { useParams } from 'next/navigation'
|
||||
import { toast } from '@/components/Toast'
|
||||
import StepOtp from './StepOtp'
|
||||
import { extractErrorMessage } from '@/lib/func'
|
||||
|
||||
|
||||
|
||||
@@ -22,7 +23,7 @@ function StepEnterNumber() {
|
||||
const { name } = useParams()
|
||||
const [step, setStep] = useState(AUTH_STEP.ENTER_NUMBER)
|
||||
|
||||
const { mutate: mutateOtpRequest } = useOtpRequest()
|
||||
const { mutate: mutateOtpRequest, isPending } = useOtpRequest()
|
||||
|
||||
const formik = useFormik<LoginOTPRequestType>({
|
||||
initialValues: {
|
||||
@@ -35,15 +36,13 @@ function StepEnterNumber() {
|
||||
onSubmit: (values) => {
|
||||
if (name) {
|
||||
values.slug = name as string
|
||||
values.phone = 'qtest'
|
||||
mutateOtpRequest(values, {
|
||||
onSuccess: () => {
|
||||
toast(t('auth.otp_sent'))
|
||||
setStep(AUTH_STEP.ENTER_OTP)
|
||||
},
|
||||
onError: (error) => {
|
||||
// toast(t('auth.otp_sent_error'))
|
||||
console.log(error);
|
||||
toast(extractErrorMessage(error), 'error')
|
||||
|
||||
}
|
||||
})
|
||||
@@ -100,7 +99,7 @@ function StepEnterNumber() {
|
||||
</div>
|
||||
<Button
|
||||
disabled={!formik.isValid}
|
||||
pending={false}
|
||||
pending={isPending}
|
||||
type='button'
|
||||
onClick={() => formik.handleSubmit()}
|
||||
>
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
interface IDanakError {
|
||||
status?: number;
|
||||
statusCode?: number;
|
||||
success?: boolean;
|
||||
error?: string | { message?: string[] | string };
|
||||
message?: string[] | string;
|
||||
}
|
||||
|
||||
const resolveMessage = (
|
||||
messages: string[] | string | undefined
|
||||
): string | null => {
|
||||
if (Array.isArray(messages) && messages.length > 0) {
|
||||
return messages[0];
|
||||
}
|
||||
if (typeof messages === "string" && messages.trim().length > 0) {
|
||||
return messages;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
export const extractErrorMessage = (
|
||||
error: Error | unknown,
|
||||
fallbackMessage: string = "خطایی رخ داده است"
|
||||
): string => {
|
||||
try {
|
||||
const axiosError = error as { response?: { data?: IDanakError } };
|
||||
const data = axiosError?.response?.data;
|
||||
|
||||
if (!data) {
|
||||
return fallbackMessage;
|
||||
}
|
||||
|
||||
const directMessage = resolveMessage(data.message);
|
||||
if (directMessage) {
|
||||
return directMessage;
|
||||
}
|
||||
|
||||
if (typeof data.error === "object" && data.error !== null) {
|
||||
const nestedMessage = resolveMessage(data.error.message);
|
||||
if (nestedMessage) {
|
||||
return nestedMessage;
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof data.error === "string" && data.error.trim().length > 0) {
|
||||
return data.error;
|
||||
}
|
||||
|
||||
return fallbackMessage;
|
||||
} catch {
|
||||
return fallbackMessage;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user