From 33c31bc2ece119f8f986004b36368da8a81ba2a7 Mon Sep 17 00:00:00 2001 From: Tim Grube <115377141+Plebysnacc@users.noreply.github.com> Date: Thu, 18 Jul 2024 15:22:17 +0200 Subject: [PATCH] Implement Table-Form (#39) * make it work * clean it up * move event-handling to seperate class --- frontend/app/actions.ts | 3 ++- frontend/app/form-tutor/ui/table/columns.tsx | 12 +++++++++++- frontend/lib/eventBroker.ts | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 frontend/lib/eventBroker.ts diff --git a/frontend/app/actions.ts b/frontend/app/actions.ts index 1c5d6e5..d78f225 100644 --- a/frontend/app/actions.ts +++ b/frontend/app/actions.ts @@ -11,6 +11,7 @@ import { TutorFormEventsQuery, TutorFormEventsDocument, } from "@/lib/gql/generated/graphql"; +import {eventBroker} from "@/lib/eventBroker"; export const getEvents = async (): Promise => { await new Promise((resolve) => setTimeout(resolve, 250)); @@ -28,7 +29,7 @@ export const addTutor = async (formState: FormState, formData: FormData) => { const firstName = formData.get("fn")?.toString() || ""; const lastName = formData.get("sn")?.toString() || ""; const email = formData.get("email")?.toString() || ""; - const eventsAvailable = [1, 2]; + const eventsAvailable = eventBroker.getEvents(); const vars: AddTutorMutationVariables = { firstName, diff --git a/frontend/app/form-tutor/ui/table/columns.tsx b/frontend/app/form-tutor/ui/table/columns.tsx index 5ba33bb..e11653a 100644 --- a/frontend/app/form-tutor/ui/table/columns.tsx +++ b/frontend/app/form-tutor/ui/table/columns.tsx @@ -3,6 +3,7 @@ import { ColumnDef } from "@tanstack/react-table"; import { Checkbox } from "@/components/ui/checkbox"; import { TutorFormEventsQuery } from "@/lib/gql/generated/graphql"; +import { eventBroker } from "@/lib/eventBroker"; export const columns: ColumnDef[] = [ { @@ -13,7 +14,16 @@ export const columns: ColumnDef[] = [ row.toggleSelected(!!value)} + onCheckedChange={(value) => { + row.toggleSelected(!!value) + + if(row.getIsSelected()){ + eventBroker.removeEvent(row.original.ID) + } else { + eventBroker.addEvent(row.original.ID) + } + + }} aria-label="Ich kann diese Vorlesung halten" /> diff --git a/frontend/lib/eventBroker.ts b/frontend/lib/eventBroker.ts new file mode 100644 index 0000000..ae12be0 --- /dev/null +++ b/frontend/lib/eventBroker.ts @@ -0,0 +1,18 @@ +class EventBroker { + selectedEvents: number[] = [] + + addEvent(eventID: number):void { + this.selectedEvents.push(eventID) + } + + removeEvent(eventID: number): void { + const index = this.selectedEvents.indexOf(eventID) + if (index > -1) this.selectedEvents.splice(index, 1) + } + + getEvents(): number[] { + return this.selectedEvents + } +} + +export let eventBroker = new EventBroker() \ No newline at end of file