added grouping creator
All checks were successful
backend-action / build-image (push) Successful in 1m5s

This commit is contained in:
2024-05-16 14:49:42 +07:00
parent 05f3e019a8
commit d125687536
5 changed files with 170 additions and 20 deletions

View File

@@ -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<number[]>([]);
let [groups, setGroup] = useState<Group[]>(
[...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 (
<div>
{groups.map((g) => (
<div className="flex flex-col items-center" key={g.id}>
<p>: {g.id}</p>
<Grouping
availableJobs={allJobs.filter((j) => !usedJobs.includes(j.id))}
selectJob={useJob}
removeJob={freeJob}
updateGroup={(ids) => updateGroup(g.id, ids)}
/>
</div>
))}
<div className="flex justify-center">
<button className="bg-green-300 rounded-md p-2"></button>
</div>
</div>
);
}