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 { useParams } from 'next/navigation'
|
||||||
import { toast } from '@/components/Toast'
|
import { toast } from '@/components/Toast'
|
||||||
import StepOtp from './StepOtp'
|
import StepOtp from './StepOtp'
|
||||||
|
import { extractErrorMessage } from '@/lib/func'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -22,7 +23,7 @@ function StepEnterNumber() {
|
|||||||
const { name } = useParams()
|
const { name } = useParams()
|
||||||
const [step, setStep] = useState(AUTH_STEP.ENTER_NUMBER)
|
const [step, setStep] = useState(AUTH_STEP.ENTER_NUMBER)
|
||||||
|
|
||||||
const { mutate: mutateOtpRequest } = useOtpRequest()
|
const { mutate: mutateOtpRequest, isPending } = useOtpRequest()
|
||||||
|
|
||||||
const formik = useFormik<LoginOTPRequestType>({
|
const formik = useFormik<LoginOTPRequestType>({
|
||||||
initialValues: {
|
initialValues: {
|
||||||
@@ -35,15 +36,13 @@ function StepEnterNumber() {
|
|||||||
onSubmit: (values) => {
|
onSubmit: (values) => {
|
||||||
if (name) {
|
if (name) {
|
||||||
values.slug = name as string
|
values.slug = name as string
|
||||||
values.phone = 'qtest'
|
|
||||||
mutateOtpRequest(values, {
|
mutateOtpRequest(values, {
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
toast(t('auth.otp_sent'))
|
toast(t('auth.otp_sent'))
|
||||||
setStep(AUTH_STEP.ENTER_OTP)
|
setStep(AUTH_STEP.ENTER_OTP)
|
||||||
},
|
},
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
// toast(t('auth.otp_sent_error'))
|
toast(extractErrorMessage(error), 'error')
|
||||||
console.log(error);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -100,7 +99,7 @@ function StepEnterNumber() {
|
|||||||
</div>
|
</div>
|
||||||
<Button
|
<Button
|
||||||
disabled={!formik.isValid}
|
disabled={!formik.isValid}
|
||||||
pending={false}
|
pending={isPending}
|
||||||
type='button'
|
type='button'
|
||||||
onClick={() => formik.handleSubmit()}
|
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