Skip to content

Commit

Permalink
fix: fix plans list and removing
Browse files Browse the repository at this point in the history
  • Loading branch information
Rei-x committed Sep 19, 2024
1 parent 4674f90 commit cf4eedb
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/atoms/planFamily.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[],
},
Expand Down
6 changes: 6 additions & 0 deletions src/atoms/plansIds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { atomWithStorage } from "jotai/utils";

export const plansIds = atomWithStorage<Array<{ id: string }>>(
"plansIds-v2",
[],
);
17 changes: 9 additions & 8 deletions src/components/Plan.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ 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";

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 = {
Expand All @@ -32,21 +32,22 @@ export const Plan = ({ id, name }: { id: string; name: string }) => {
});

setPlans([...plans, newPlan]);
setPlanToCopy({
planToCopy.setPlan({
...planToCopy,
courses: plan.courses,
groups: plan.groups,
});

setTimeout(() => {
void router.push(`/createplan/${newPlan.id}`);
}, 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 (
<div className="flex h-[200px] w-[200px] flex-col items-center justify-between rounded-lg border-gray-400 bg-white p-4 text-center shadow-[0_0_5px_5px_rgba(0,0,0,0.10)]">
<div className="flex w-full justify-between">
Expand Down
2 changes: 1 addition & 1 deletion src/components/PlanDisplayLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Link
href={`/app/preview/${id}`}
Expand Down
5 changes: 4 additions & 1 deletion src/lib/usePlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import { type ExtendedCourse, planFamily } from "@/atoms/planFamily";

import type { Registration } from "./types";

export const usePlan = ({ planId }: { planId: number }) => {
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({
Expand Down
8 changes: 3 additions & 5 deletions src/pages/app/createplan/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -227,7 +225,7 @@ const CreatePlan = ({
</div>
<div className="mr-4 flex w-1/4 items-center justify-end">
<Link
href="/plans"
href="/app/plans"
data-umami-event="Back to plans"
className={cn(buttonVariants({ variant: "link" }), "text-white")}
>
Expand Down
4 changes: 1 addition & 3 deletions src/pages/app/plans.tsx
Original file line number Diff line number Diff line change
@@ -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<Array<{ id: number }>>("plansIds", []);

const plansAtom = atom(
(get) => get(plansIds).map((id) => get(planFamily(id))),
(get, set, values: Array<{ id: string }>) => {
Expand Down
13 changes: 5 additions & 8 deletions src/pages/app/preview/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ 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";
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;
Expand All @@ -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 = ({
Expand All @@ -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,
};

Expand All @@ -66,7 +63,7 @@ const SharePlan = ({
</div>
<div className="mr-4 flex items-center justify-end gap-4">
<Link
href="/plans"
href="/app/plans"
data-umami-event="Back to plans"
className={cn(
buttonVariants({ variant: "link" }),
Expand Down

0 comments on commit cf4eedb

Please sign in to comment.