Files
hounam-submit-form-frontend/core/utils/index.ts
2026-06-02 17:08:52 +03:30

62 lines
1.5 KiB
TypeScript

import axios from "axios";
import { AppRouterInstance } from "next/dist/shared/lib/app-router-context.shared-runtime";
export function handleAxiosError(error: unknown) {
if (axios.isAxiosError(error)) {
// اینجا می‌دونیم که خطا از axios است
return error.response?.data?.error?.message;
} else {
return "Unexpected error";
}
}
export const handleBack = (props: any, key: string) => {
// قبل از رفتن به عقب، مقادیر فعلی فرم را در استیت والد ذخیره کن
props?.update({ [key]: props?.values });
props.setStep(props?.step - 1);
};
type LoginData = {
token?: string;
applicant?: {
id?: string;
fullname?: string;
formStep?: number;
centerId?: string;
} | null;
};
export const handleLoginRedirect = (
router: AppRouterInstance,
data: {data:LoginData},
) => {
const applicant = data?.data?.applicant;
console.log(applicant)
// اگر کاربر قبلاً applicant داشته باشد
if (applicant?.id) {
const targetStep = applicant.formStep || 1;
localStorage.setItem(
"applicationDraft",
JSON.stringify({
applicantId: applicant.id,
centerId: applicant.centerId,
formStep: targetStep,
}),
);
if (targetStep === 1) {
router.push("/form");
} else {
router.push(`/form?step=${targetStep}`);
}
return;
}
// اگر کاربر جدید باشد و هنوز applicant نداشته باشد
localStorage.removeItem("applicationDraft");
router.push("/form");
};