From cc95e13eb6a8eed2557d071fa8aad5dc5480f540 Mon Sep 17 00:00:00 2001 From: Kamil Marczak Date: Mon, 9 Dec 2024 08:16:55 +0100 Subject: [PATCH] fix: error handlingn fix --- .../[id]/_components/CreateNewPlanPage.tsx | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/frontend/src/app/plans/edit/[id]/_components/CreateNewPlanPage.tsx b/frontend/src/app/plans/edit/[id]/_components/CreateNewPlanPage.tsx index efd9029..fd50c57 100644 --- a/frontend/src/app/plans/edit/[id]/_components/CreateNewPlanPage.tsx +++ b/frontend/src/app/plans/edit/[id]/_components/CreateNewPlanPage.tsx @@ -89,26 +89,27 @@ export function CreateNewPlanPage({ const groups = plan.allGroups .filter((g) => g.isChecked) .map((g) => ({ id: g.groupOnlineId })); - const res = await createNewPlan({ - name: plan.name, - courses, - registrations, - groups, - }); - if (res === false) { + try { + const res = await createNewPlan({ + name: plan.name, + courses, + registrations, + groups, + }); + plan.setPlan((prev) => ({ + ...prev, + synced: true, + updatedAt: new Date(res.schedule.updatedAt), + onlineId: res.schedule.id.toString(), + })); + toast.success("Utworzono plan"); + return true; + } catch (error) { return toast.error("Nie udało się utworzyć planu w wersji online", { description: "Zaloguj się i spróbuj ponownie", duration: 5000, }); } - plan.setPlan((prev) => ({ - ...prev, - synced: true, - updatedAt: new Date(res.schedule.updatedAt), - onlineId: res.schedule.id.toString(), - })); - toast.success("Utworzono plan"); - return true; }; const handleSyncPlan = async () => { @@ -125,7 +126,7 @@ export function CreateNewPlanPage({ .filter((g) => g.isChecked) .map((g) => ({ id: g.groupOnlineId })), }); - if (res === false || !res.success) { + if (!res.success) { return toast.error("Nie udało się zaktualizować planu"); } await refetchOnlinePlan(); @@ -138,6 +139,8 @@ export function CreateNewPlanPage({ : new Date(), })); return true; + } catch (error) { + return toast.error("Nie udało się zaktualizować planu"); } finally { setSyncing(false); }