Skip to content

Commit

Permalink
feat: edit dialogs for categories and disciplines
Browse files Browse the repository at this point in the history
  • Loading branch information
DJ1TJOO committed Aug 16, 2024
1 parent ca5a0e3 commit fd11af0
Show file tree
Hide file tree
Showing 18 changed files with 711 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"use server";

import { revalidatePath } from "next/cache";
import { z } from "zod";
import { updateCategory } from "~/lib/nwd";

export default async function update(
categoryId: string,
data: {
title: string;
description: string;
handle: string;
weight: number;
parentCategoryId: string;
},
) {
try {
await updateCategory(categoryId, data);

revalidatePath("/secretariaat/diplomalijn/categorieen");

return {
message: "Success",
errors: {},
};
} catch (error) {
if (error instanceof z.ZodError) {
return {
message: "Error",
errors: error.issues.reduce(
(acc, issue) => {
acc[issue.path.join(".")] = issue.message;
return acc;
},
{} as Record<string, string>,
),
};
}

return {
message: "Error",
errors: {},
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export default function ModuleTable({
: "",
)}
key={row.id}
href={`#TODO`}
href={`/secretariaat/diplomalijn/categorieen?bewerken=${row.id}`}
>
{row.getVisibleCells().map((cell, index) => (
<TableCell
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
"use client";
import { parseAsString, useQueryState } from "nuqs";
import { useFormState, useFormStatus } from "react-dom";
import { toast } from "sonner";
import { Button } from "~/app/(dashboard)/_components/button";
import {
Dialog,
DialogActions,
DialogBody,
DialogDescription,
DialogTitle,
} from "~/app/(dashboard)/_components/dialog";
import {
ErrorMessage,
Field,
FieldGroup,
Fieldset,
Label,
} from "~/app/(dashboard)/_components/fieldset";
import { Input } from "~/app/(dashboard)/_components/input";
import {
Listbox,
ListboxLabel,
ListboxOption,
} from "~/app/(dashboard)/_components/listbox";
import Spinner from "~/app/_components/spinner";
import type { listCategories, listParentCategories } from "~/lib/nwd";
import update from "../_actions/update";

export default function EditDialog({
editCategory,
parentCategories,
}: {
editCategory: Awaited<ReturnType<typeof listCategories>>[number] | null;
parentCategories: Awaited<ReturnType<typeof listParentCategories>>;
}) {
const [editCategoryId, setEditCategoryId] = useQueryState<string>(
"bewerken",
parseAsString.withDefault(""),
);

const [state, formAction] = useFormState(
async (_prevState: unknown, formData: FormData) => {
const title = formData.get("title") as string;
const description = formData.get("description") as string;
const handle = formData.get("handle") as string;
const weight = Number(formData.get("weight"));
const parentCategoryId = formData.get("parentCategoryId") as string;

const result = await update(editCategoryId, {
title,
handle,
weight,
description,
parentCategoryId,
});
if (result.message === "Success") {
toast.success("Categorie is geüpdatet.");
await setEditCategoryId(null);
}

return result;
},
null,
);

if (!editCategory) {
return null;
}

return (
<Dialog open={!!editCategoryId} onClose={() => setEditCategoryId(null)}>
<DialogTitle>Categorie wijzigen</DialogTitle>
<DialogDescription>
Pas de gegevens van de categorie aan.
</DialogDescription>
<form action={formAction}>
<DialogBody>
<Fieldset>
<FieldGroup>
<Field>
<Label>Naam</Label>
<Input
name="title"
invalid={!!state?.errors?.title}
defaultValue={editCategory.title ?? undefined}
required
/>
{state?.errors?.title ? (
<ErrorMessage>{state.errors.title}</ErrorMessage>
) : null}
</Field>
<Field>
<Label>Handle</Label>
<Input
name="handle"
invalid={!!state?.errors?.handle}
defaultValue={editCategory.handle}
required
/>
{state?.errors?.handle ? (
<ErrorMessage>{state.errors.handle}</ErrorMessage>
) : null}
</Field>
<Field>
<Label>Beschrijving</Label>
<Input
name="description"
invalid={!!state?.errors?.description}
defaultValue={editCategory.description ?? undefined}
/>
{state?.errors?.description ? (
<ErrorMessage>{state.errors.description}</ErrorMessage>
) : null}
</Field>
<FieldGroup className="sm:columns-2">
<Field>
<Label>Hoofdcategorie</Label>
<Listbox
name="parentCategoryId"
invalid={!!state?.errors?.parentCategoryId}
defaultValue={editCategory.parent?.id}
>
{parentCategories.map((category) => (
<ListboxOption value={category.id}>
<ListboxLabel>
{category.title ?? category.handle}
</ListboxLabel>
</ListboxOption>
))}
</Listbox>
{state?.errors?.parentCategoryId ? (
<ErrorMessage>{state.errors.parentCategoryId}</ErrorMessage>
) : null}
</Field>
<Field>
<Label>Sortering</Label>
<Input
type="number"
invalid={!!state?.errors?.weight}
name="weight"
defaultValue={editCategory.weight}
required
/>
{state?.errors?.weight ? (
<ErrorMessage>{state.errors.weight}</ErrorMessage>
) : null}
</Field>
</FieldGroup>
</FieldGroup>
</Fieldset>
</DialogBody>
<DialogActions>
<Button plain onClick={() => setEditCategoryId(null)}>
Annuleren
</Button>
<SubmitButton />
</DialogActions>
</form>
</Dialog>
);
}

function SubmitButton() {
const { pending } = useFormStatus();
return (
<Button color="branding-dark" disabled={pending} type="submit">
{pending ? <Spinner className="text-white" /> : null}
Opslaan
</Button>
);
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import FlexSearch from "flexsearch";
import { Heading } from "~/app/(dashboard)/_components/heading";
import { listCategories } from "~/lib/nwd";
import { listCategories, listParentCategories } from "~/lib/nwd";
import Search from "../../../_components/search";
import CategoryTableCLient from "./_components/category-table";
import EditDialog from "./_components/edit-dialog";

async function CategoryTable({
searchParams,
}: {
searchParams: Record<string, string | string[] | undefined>;
}) {
const categories = await listCategories();
const [categories, parentCategories] = await Promise.all([
listCategories(),
listParentCategories(),
]);
const searchQuery = searchParams?.query?.toString() ?? null;

const editCategoryId = searchParams?.bewerken?.toString() ?? null;
const editCategory =
categories.find((category) => category.id === editCategoryId) ?? null;

// Create a FlexSearch index
const index = new FlexSearch.Index({
tokenize: "full",
Expand Down Expand Up @@ -46,10 +54,17 @@ async function CategoryTable({
);

return (
<CategoryTableCLient
categories={paginatedCategories}
totalItems={filteredCategories.length}
/>
<>
<CategoryTableCLient
categories={paginatedCategories}
totalItems={filteredCategories.length}
/>
<EditDialog
key={editCategory?.id}
editCategory={editCategory}
parentCategories={parentCategories}
/>
</>
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import FlexSearch from "flexsearch";
import { Heading } from "~/app/(dashboard)/_components/heading";
import { listCompetencies } from "~/lib/nwd";
import Search from "../../../_components/search";
import CompetencyTableClient from "./_components/competencies-table";
import EditDialog from "./_components/edit-dialog";
import ProgramCompetencyClient from "./_components/program-table";

async function CompetencyTable({
searchParams,
Expand Down Expand Up @@ -53,11 +53,11 @@ async function CompetencyTable({

return (
<>
<ProgramCompetencyClient
<CompetencyTableClient
competencies={paginatedPrograms}
totalItems={filteredCompetencies.length}
/>
<EditDialog editCompetency={editCompetency} />
<EditDialog key={editCompetency?.id} editCompetency={editCompetency} />
</>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default function CourseTable({
{row.original.categories
.filter((c) => c.parent?.id === category.id)
.map((c) => (
<Badge>{c.title}</Badge>
<Badge key={c.id}>{c.title}</Badge>
))}
</div>
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use server";

import { revalidatePath } from "next/cache";
import { z } from "zod";
import { updateDiscipline } from "~/lib/nwd";

export default async function update(
disciplineId: string,
data: {
title: string;
handle: string;
weight: number;
},
) {
try {
await updateDiscipline(disciplineId, data);

revalidatePath("/secretariaat/diplomalijn/disciplines");

return {
message: "Success",
errors: {},
};
} catch (error) {
if (error instanceof z.ZodError) {
return {
message: "Error",
errors: error.issues.reduce(
(acc, issue) => {
acc[issue.path.join(".")] = issue.message;
return acc;
},
{} as Record<string, string>,
),
};
}

return {
message: "Error",
errors: {},
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default function ModuleTable({
: "",
)}
key={row.id}
href={`#TODO`}
href={`/secretariaat/diplomalijn/disciplines?bewerken=${row.id}`}
>
{row.getVisibleCells().map((cell, index) => (
<TableCell
Expand Down
Loading

0 comments on commit fd11af0

Please sign in to comment.