diff --git a/app/grouping/GroupCreator.tsx b/app/grouping/GroupCreator.tsx new file mode 100644 index 0000000..5ed7710 --- /dev/null +++ b/app/grouping/GroupCreator.tsx @@ -0,0 +1,73 @@ +"use client"; + +import { LocationContext } from "@/components/locationContenxt"; +import { useContext, useEffect, useState } from "react"; +import Grouping from "./Grouping"; +import { unique } from "drizzle-orm/mysql-core"; + +type Props = { + allJobs: JobCategory[]; +}; + +export type JobCategory = { + id: number; + name: string; +}; + +type Group = { + id: number; + jobs: number[]; +}; +export default function GroupCreator({ allJobs }: Props) { + let locationContext = useContext(LocationContext); + let [usedJobs, setUsedJobs] = useState([]); + let [groups, setGroup] = useState( + [...Array(4).keys()].map((i) => ({ id: i + 1, jobs: [] })) + ); + function useJob(id: number) { + setUsedJobs((u) => [...u, id]); + } + + function freeJob(id: number) { + setUsedJobs((u) => u.filter((j) => j != id)); + } + + function updateGroup(id: number, jobs: number[]) { + setGroup((groups) => + groups.map((g) => { + if (g.id != id) return g; + else return { id, jobs }; + }) + ); + } + + useEffect(() => { + console.log(groups); + }, [groups]); + + if ( + locationContext?.zone[0] == undefined || + locationContext?.province[0] == undefined + ) { + return undefined; + } + return ( +
+ {groups.map((g) => ( +
+

สายที่: {g.id}

+ !usedJobs.includes(j.id))} + selectJob={useJob} + removeJob={freeJob} + updateGroup={(ids) => updateGroup(g.id, ids)} + /> +
+ ))} + +
+ +
+
+ ); +} diff --git a/app/grouping/Grouping.tsx b/app/grouping/Grouping.tsx new file mode 100644 index 0000000..cabe94a --- /dev/null +++ b/app/grouping/Grouping.tsx @@ -0,0 +1,59 @@ +import { useEffect, useState } from "react"; +import { JobCategory } from "./GroupCreator"; + +type Props = { + availableJobs: JobCategory[]; + selectJob: (id: number) => void; + removeJob: (id: number) => void; + updateGroup: (id: number[]) => void; +}; + +export default function Grouping({ + availableJobs, + selectJob, + removeJob, + updateGroup, +}: Props) { + let [selectedJob, setSelectedJob] = useState([]); + function addJob(id: string) { + let _id = parseInt(id); + let job = availableJobs.find((j) => j.id == _id); + if (job == undefined) return; + setSelectedJob((old) => [...old, job]); + selectJob(_id); + } + function removeJobFromGroup(id: number) { + setSelectedJob((old) => old.filter((j) => j.id != id)); + removeJob(id); + } + useEffect(() => { + updateGroup(selectedJob.map((j) => j.id)); + }, [selectedJob]); + return ( +
+ {selectedJob.map((j) => ( +
+

{j.name}

+ +
+ ))} + {selectedJob.length < 5 && ( + + )} +
+ ); +} diff --git a/app/grouping/page.tsx b/app/grouping/page.tsx index 7920bd4..ac95c5b 100644 --- a/app/grouping/page.tsx +++ b/app/grouping/page.tsx @@ -1,14 +1,17 @@ import { db } from "@/src/db"; import LocationSelector from "../../components/LocationSelector"; import LocationContextProvider from "@/components/locationContenxt"; +import GroupCreator from "./GroupCreator"; export default async function Page() { let provinces = await db.query.province .findMany({ with: { zones: true } }) .execute(); + let jobList = await db.query.group.findMany().execute(); return ( + ); } diff --git a/app/layout.tsx b/app/layout.tsx index 56819d7..793de03 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -6,8 +6,11 @@ export default function RootLayout({ }) { return ( + + + -
{children}
+
{children}
); diff --git a/components/LocationSelector/index.tsx b/components/LocationSelector/index.tsx index b5be462..9160e81 100644 --- a/components/LocationSelector/index.tsx +++ b/components/LocationSelector/index.tsx @@ -46,30 +46,42 @@ export default function LocationSelector({ provinces }: Props) { }, [amphurId]); return (
- - - {amphurList && ( +
+ จังหวัด: +
+ + {amphurList && ( +
+ อำเภอ: + +
)}
);