Skip to content

Commit

Permalink
Implement Table-Form (#39)
Browse files Browse the repository at this point in the history
* make it work

* clean it up

* move event-handling to seperate class
  • Loading branch information
Plebysnacc authored Jul 18, 2024
1 parent d484e05 commit 33c31bc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion frontend/app/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
TutorFormEventsQuery,
TutorFormEventsDocument,
} from "@/lib/gql/generated/graphql";
import {eventBroker} from "@/lib/eventBroker";

export const getEvents = async (): Promise<TutorFormEventsQuery> => {
await new Promise((resolve) => setTimeout(resolve, 250));
Expand All @@ -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,
Expand Down
12 changes: 11 additions & 1 deletion frontend/app/form-tutor/ui/table/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<TutorFormEventsQuery['events'][0]>[] = [
{
Expand All @@ -13,7 +14,16 @@ export const columns: ColumnDef<TutorFormEventsQuery['events'][0]>[] = [
<Checkbox
className={"mx-auto"}
checked={row.getIsSelected()}
onCheckedChange={(value) => 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"
/>
</div>
Expand Down
18 changes: 18 additions & 0 deletions frontend/lib/eventBroker.ts
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 33c31bc

Please sign in to comment.