From 9d4a86d9e81c7e33faaf668234d40f0b2ef364c0 Mon Sep 17 00:00:00 2001 From: Suraj Ramchandran <30753067+Suraj-Ram@users.noreply.github.com> Date: Sun, 4 Feb 2024 21:04:51 -0500 Subject: [PATCH] works except name edge case + LS quota --- .../components/Plan/DuplicatePlanButton.tsx | 64 +++++++++++++++---- 1 file changed, 50 insertions(+), 14 deletions(-) diff --git a/packages/frontend/components/Plan/DuplicatePlanButton.tsx b/packages/frontend/components/Plan/DuplicatePlanButton.tsx index bcdc068db..a73c0dd6b 100644 --- a/packages/frontend/components/Plan/DuplicatePlanButton.tsx +++ b/packages/frontend/components/Plan/DuplicatePlanButton.tsx @@ -2,12 +2,20 @@ import { CopyIcon } from "@chakra-ui/icons"; import { API } from "@graduate/api-client"; import { CreatePlanDto, PlanModel } from "@graduate/common"; import { useRouter } from "next/router"; -import { Dispatch, SetStateAction, useState } from "react"; +import { Dispatch, SetStateAction, useState, useContext } from "react"; import { toast } from "react-toastify"; import { mutate } from "swr"; -import { USE_STUDENT_WITH_PLANS_SWR_KEY } from "../../hooks"; -import { cleanDndIdsFromPlan, handleApiClientError } from "../../utils"; +import { + USE_STUDENT_WITH_PLANS_SWR_KEY, + useStudentWithPlans, +} from "../../hooks"; +import { + cleanDndIdsFromPlan, + cleanDndIdsFromStudent, + handleApiClientError, +} from "../../utils"; import { BlueButton } from "../Button"; +import { IsGuestContext } from "../../pages/_app"; interface DuplicatePlanButton { plan: PlanModel; @@ -19,12 +27,14 @@ export const DuplicatePlanButton: React.FC = ({ setSelectedPlanId, }) => { const router = useRouter(); + const { isGuest } = useContext(IsGuestContext); const [buttonLoading, setButtonLoading] = useState(false); + const { student } = useStudentWithPlans(); const duplicatePlan = async () => { - // TODO: figure out when to do with this + if (!student) return; + // TODO: figure out when to do with this -- debounce stuff setButtonLoading(true); - const updatedPlan: CreatePlanDto = { name: "Copy of " + plan.name, catalogYear: plan.catalogYear, @@ -32,16 +42,42 @@ export const DuplicatePlanButton: React.FC = ({ concentration: plan.concentration, schedule: cleanDndIdsFromPlan(plan).schedule, }; - try { - const createdPlan = await API.plans.create(updatedPlan); - mutate(USE_STUDENT_WITH_PLANS_SWR_KEY); - setSelectedPlanId(createdPlan.id); - toast.success("Plan duplicated successfully"); - setButtonLoading(false); - } catch (error) { - handleApiClientError(error as Error, router); - setButtonLoading(false); + + let createdPlanId: number; + if (isGuest) { + // Create the duplicated plan in local storage + createdPlanId = student.plans.length + 1; + const planLocalStorage: PlanModel = { + ...updatedPlan, + id: createdPlanId, + student: cleanDndIdsFromStudent(student), + createdAt: new Date(), + updatedAt: new Date(), + } as PlanModel; + + // TODO handle QuotaExceededError exception + window.localStorage.setItem( + "student", + JSON.stringify({ + ...student, + plans: [...student.plans, planLocalStorage], + }) + ); + } else { + try { + const createdPlan = await API.plans.create(updatedPlan); + createdPlanId = createdPlan.id; + } catch (error) { + handleApiClientError(error as Error, router); + // don't proceed further if POST failed + setButtonLoading(false); + return; + } } + mutate(USE_STUDENT_WITH_PLANS_SWR_KEY); + toast.success("Plan duplicated successfully"); + setSelectedPlanId(createdPlanId); + setButtonLoading(false); }; return (