temporary server action for group and inside

This commit is contained in:
2024-05-17 15:33:15 +07:00
parent c1a019a461
commit 3c37fbf59b
6 changed files with 60 additions and 22 deletions

View File

@@ -3,20 +3,12 @@
import { LocationContext } from "@/components/locationContext";
import { useContext, useState } from "react";
import Grouping from "./Grouping";
import { Group, JobCategory, updateGroups } from "./action";
type Props = {
allJobs: JobCategory[];
};
export type JobCategory = {
id: number;
name: string;
};
type Group = {
id: number;
jobs: number[];
};
export default function GroupCreator({ allJobs }: Props) {
const locationContext = useContext(LocationContext);
const [usedJobs, setUsedJobs] = useState<number[]>([]);
@@ -40,7 +32,22 @@ export default function GroupCreator({ allJobs }: Props) {
);
}
function submit() {} //TODO! submit group
async function submit() {
if (
locationContext?.zone[0] == undefined ||
locationContext?.province[0] == undefined
) {
alert("ยังไม่ได้เลือกพื้นที่");
return;
}
await updateGroups(
locationContext.province[0],
locationContext.zone[0],
groups,
);
alert("อัพเดทสำเร็จ");
}
if (
locationContext?.zone[0] == undefined ||

View File

@@ -1,5 +1,5 @@
import { useEffect, useState } from "react";
import { JobCategory } from "./GroupCreator";
import { JobCategory } from "./action";
type Props = {
availableJobs: JobCategory[];
@@ -19,16 +19,17 @@ export default function Grouping({
const _id = parseInt(id);
const job = availableJobs.find((j) => j.id == _id);
if (job == undefined) return;
setSelectedJob((old) => [...old, job]);
const newSelectedJob = [...selectedJob, job];
setSelectedJob(newSelectedJob);
selectJob(_id);
updateGroup(newSelectedJob.map((j) => j.id));
}
function removeJobFromGroup(id: number) {
setSelectedJob((old) => old.filter((j) => j.id != id));
const newSelectedJob = selectedJob.filter((j) => j.id != id);
setSelectedJob(newSelectedJob);
removeJob(id);
updateGroup(newSelectedJob.map((j) => j.id));
}
useEffect(() => {
updateGroup(selectedJob.map((j) => j.id));
}, [selectedJob, updateGroup]);
return (
<div className="m-2 flex w-full flex-col gap-2 rounded-md border border-black p-2 shadow-md">
{selectedJob.map((j) => (

20
app/grouping/action.ts Normal file
View File

@@ -0,0 +1,20 @@
"use server";
export type JobCategory = {
id: number;
name: string;
};
export type Group = {
id: number;
jobs: number[];
};
//TODO: submit group
export async function updateGroups(
province: number,
zone: number,
groups: Group[],
) {
console.log({ province, zone, groups });
}