From cf4eedb1bd7ca0fcbacef87aa8e818615f739891 Mon Sep 17 00:00:00 2001 From: Bartosz Gotowski Date: Thu, 19 Sep 2024 19:52:07 +0200 Subject: [PATCH] fix: fix plans list and removing --- src/atoms/planFamily.ts | 6 +++--- src/atoms/plansIds.ts | 6 ++++++ src/components/Plan.tsx | 17 +++++++++-------- src/components/PlanDisplayLink.tsx | 2 +- src/lib/usePlan.ts | 5 ++++- src/pages/app/createplan/[id].tsx | 8 +++----- src/pages/app/plans.tsx | 4 +--- src/pages/app/preview/[id].tsx | 13 +++++-------- 8 files changed, 32 insertions(+), 29 deletions(-) create mode 100644 src/atoms/plansIds.ts diff --git a/src/atoms/planFamily.ts b/src/atoms/planFamily.ts index c786516..3b4e441 100644 --- a/src/atoms/planFamily.ts +++ b/src/atoms/planFamily.ts @@ -12,12 +12,12 @@ export interface ExtendedGroup extends ClassBlockProps { } export const planFamily = atomFamily( - ({ id }: { id: number }) => + ({ id }: { id: string }) => atomWithStorage( - `${id}-plan`, + `${id}-plan-v2`, { id, - name: `Nowy plan - ${id}`, + name: `Nowy plan`, courses: [] as ExtendedCourse[], registrations: [] as Registration[], }, diff --git a/src/atoms/plansIds.ts b/src/atoms/plansIds.ts new file mode 100644 index 0000000..f064cb6 --- /dev/null +++ b/src/atoms/plansIds.ts @@ -0,0 +1,6 @@ +import { atomWithStorage } from "jotai/utils"; + +export const plansIds = atomWithStorage>( + "plansIds-v2", + [], +); diff --git a/src/components/Plan.tsx b/src/components/Plan.tsx index d2f7fcf..8e5c183 100644 --- a/src/components/Plan.tsx +++ b/src/components/Plan.tsx @@ -4,14 +4,14 @@ import Link from "next/link"; import router from "next/router"; import React from "react"; +import { plansIds } from "@/atoms/plansIds"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; +import { usePlan } from "@/lib/usePlan"; import { cn } from "@/lib/utils"; -import { planFamily } from "@/pages/createplan/[id]"; -import { plansIds } from "@/pages/plans"; import { DeletePlanConfirmationResponsiveDialog } from "./DeletePlanConfirmationResponsiveDialog"; import { buttonVariants } from "./ui/button"; @@ -19,8 +19,8 @@ import { buttonVariants } from "./ui/button"; export const Plan = ({ id, name }: { id: string; name: string }) => { const uuid = React.useMemo(() => crypto.randomUUID(), []); const [plans, setPlans] = useAtom(plansIds); - const [plan] = useAtom(planFamily({ id })); - const [planToCopy, setPlanToCopy] = useAtom(planFamily({ id: uuid })); + const plan = usePlan({ planId: id }); + const planToCopy = usePlan({ planId: uuid }); const copyPlan = () => { const newPlan = { @@ -32,10 +32,9 @@ export const Plan = ({ id, name }: { id: string; name: string }) => { }); setPlans([...plans, newPlan]); - setPlanToCopy({ + planToCopy.setPlan({ ...planToCopy, courses: plan.courses, - groups: plan.groups, }); setTimeout(() => { @@ -43,10 +42,12 @@ export const Plan = ({ id, name }: { id: string; name: string }) => { }, 200); }; const deletePlan = () => { - planFamily.remove({ id }); + plan.remove(); setPlans(plans.filter((p) => p.id !== id)); }; - const groupCount = plan.groups.filter((group) => group.isChecked).length; + const groupCount = plan.courses + .flatMap((c) => c.groups) + .filter((group) => group.isChecked).length; return (
diff --git a/src/components/PlanDisplayLink.tsx b/src/components/PlanDisplayLink.tsx index 472ee67..4aa253f 100644 --- a/src/components/PlanDisplayLink.tsx +++ b/src/components/PlanDisplayLink.tsx @@ -4,7 +4,7 @@ import { cn } from "@/lib/utils"; import { buttonVariants } from "./ui/button"; -export function PlanDisplayLink({ id }: { id: number }) { +export function PlanDisplayLink({ id }: { id: string }) { return ( { +export const usePlan = ({ planId }: { planId: string }) => { const [plan, setPlan] = useAtom(planFamily({ id: planId })); return { ...plan, allGroups: plan.courses.filter((c) => c.isChecked).flatMap((c) => c.groups), setPlan, + remove: () => { + planFamily.remove({ id: planId }); + }, selectGroup: (groupId: string, isChecked?: boolean) => { void window.umami?.track("Change group"); setPlan({ diff --git a/src/pages/app/createplan/[id].tsx b/src/pages/app/createplan/[id].tsx index 773c598..09b40ef 100644 --- a/src/pages/app/createplan/[id].tsx +++ b/src/pages/app/createplan/[id].tsx @@ -119,10 +119,8 @@ export const getServerSideProps = (async (context) => { throw new Error(`Invalid id ${id?.toString()}`); } - const planId = parseInt(id); - - return { props: { planId } }; -}) satisfies GetServerSideProps<{ planId: number }>; + return { props: { planId: id } }; +}) satisfies GetServerSideProps<{ planId: string }>; const registrationReplacer = (name: string) => { const newName = name @@ -227,7 +225,7 @@ const CreatePlan = ({
diff --git a/src/pages/app/plans.tsx b/src/pages/app/plans.tsx index fb5fc08..e11f266 100644 --- a/src/pages/app/plans.tsx +++ b/src/pages/app/plans.tsx @@ -1,17 +1,15 @@ import { atom, useAtom } from "jotai"; -import { atomWithStorage } from "jotai/utils"; import Link from "next/link"; import { useRouter } from "next/router"; import { planFamily } from "@/atoms/planFamily"; +import { plansIds } from "@/atoms/plansIds"; import { Plan } from "@/components/Plan"; import { Seo } from "@/components/SEO"; import { SolvroLogo } from "@/components/SolvroLogo"; import { Button, buttonVariants } from "@/components/ui/button"; import { cn } from "@/lib/utils"; -export const plansIds = atomWithStorage>("plansIds", []); - const plansAtom = atom( (get) => get(plansIds).map((id) => get(planFamily(id))), (get, set, values: Array<{ id: string }>) => { diff --git a/src/pages/app/preview/[id].tsx b/src/pages/app/preview/[id].tsx index 217ab45..54f805f 100644 --- a/src/pages/app/preview/[id].tsx +++ b/src/pages/app/preview/[id].tsx @@ -6,6 +6,7 @@ import * as React from "react"; import { LuDownloadCloud } from "react-icons/lu"; import { planFamily } from "@/atoms/planFamily"; +import { plansIds } from "@/atoms/plansIds"; import { ClassSchedule } from "@/components/ClassSchedule"; import { SolvroLogo } from "@/components/SolvroLogo"; import { Button, buttonVariants } from "@/components/ui/button"; @@ -13,8 +14,6 @@ import { usePlan } from "@/lib/usePlan"; import { cn } from "@/lib/utils"; import { Day } from "@/services/usos/types"; -import { plansIds } from "../plans"; - // eslint-disable-next-line @typescript-eslint/require-await export const getServerSideProps = (async (context) => { const { id } = context.query; @@ -23,7 +22,7 @@ export const getServerSideProps = (async (context) => { throw new Error(`Invalid hash ${id?.toString()}`); } - return { props: { id: parseInt(id) } }; + return { props: { id } }; }) satisfies GetServerSideProps; const SharePlan = ({ @@ -32,15 +31,13 @@ const SharePlan = ({ const uuid = React.useMemo(() => crypto.randomUUID(), []); const [plans, setPlans] = useAtom(plansIds); const plan = usePlan({ planId: id }); - const [planToCopy, setPlanToCopy] = useAtom( - planFamily({ id: plans.length + 1 }), - ); + const [planToCopy, setPlanToCopy] = useAtom(planFamily({ id: uuid })); const router = useRouter(); const copyPlan = () => { const newPlan = { - id: plans.length + 1, + id: uuid, courses: plan.courses, }; @@ -66,7 +63,7 @@ const SharePlan = ({