Skip to content

Commit

Permalink
fix: fix of createOnlinePlan abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
qamarq committed Dec 14, 2024
1 parent 519e6ed commit bbc454e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
14 changes: 13 additions & 1 deletion frontend/src/app/plans/edit/[id]/page.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,19 @@ export function CreateNewPlanPage({

const handleCreateOnlinePlan = async () => {
firstTime.current = false;
await createOnlinePlan(plan, setOfflineAlert);
const result = await createOnlinePlan(plan);
if ("error" in result) {
if (result.error === "NOT_LOGGED_IN") {
setOfflineAlert(true);
} else {
toast.error("Nie udało się utworzyć planu w wersji online", {
description: result.message,
duration: 10000,
});
}
} else if ("success" in result) {
toast.success("Utworzono plan");
}
};

const handleSyncPlan = async () => {
Expand Down
21 changes: 5 additions & 16 deletions frontend/src/lib/utils/createOnlinePlan.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { toast } from "sonner";

import { createNewPlan } from "@/actions/plans";
import type { PlanState } from "@/lib/usePlan";

export const createOnlinePlan = async (
plan: PlanState,
setOfflineAlert: (value: boolean) => void,
) => {
export const createOnlinePlan = async (plan: PlanState) => {
try {
const courses = plan.courses
.filter((c) => c.isChecked)
Expand All @@ -30,20 +25,14 @@ export const createOnlinePlan = async (
onlineId: res.schedule.id.toString(),
}));

toast.success("Utworzono plan");
return true;
return { success: true };
} catch (err) {
if (err instanceof Error && "message" in err) {
if (err.message === "Not logged in") {
setOfflineAlert(true);
} else {
toast.error("Nie udało się utworzyć planu w wersji online", {
description:
"Wystąpił nieoczekiwany błąd. Skontaktuj się z zespołem developerów.",
duration: 10000,
});
return { error: "NOT_LOGGED_IN", message: err.message };
}
return { error: "UNKNOWN", message: err.message };
}
return false;
return { error: "UNKNOWN", message: "Wystąpił nieoczekiwany błąd" };
}
};

0 comments on commit bbc454e

Please sign in to comment.