From 3c37fbf59b048f0264a06e982d6688f2342ff435 Mon Sep 17 00:00:00 2001 From: Thanu Poptiphueng Date: Fri, 17 May 2024 15:33:15 +0700 Subject: [PATCH] temporary server action for group and inside --- app/grouping/GroupCreator.tsx | 27 +++++++++++++++++---------- app/grouping/Grouping.tsx | 13 +++++++------ app/grouping/action.ts | 20 ++++++++++++++++++++ app/inside/IdComponent.tsx | 11 ++++++----- app/inside/action.ts | 5 +++++ app/inside/page.tsx | 6 +++++- 6 files changed, 60 insertions(+), 22 deletions(-) create mode 100644 app/grouping/action.ts create mode 100644 app/inside/action.ts diff --git a/app/grouping/GroupCreator.tsx b/app/grouping/GroupCreator.tsx index 86aca62..972af14 100644 --- a/app/grouping/GroupCreator.tsx +++ b/app/grouping/GroupCreator.tsx @@ -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([]); @@ -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 || diff --git a/app/grouping/Grouping.tsx b/app/grouping/Grouping.tsx index 791001a..b15d405 100644 --- a/app/grouping/Grouping.tsx +++ b/app/grouping/Grouping.tsx @@ -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 (
{selectedJob.map((j) => ( diff --git a/app/grouping/action.ts b/app/grouping/action.ts new file mode 100644 index 0000000..1e03994 --- /dev/null +++ b/app/grouping/action.ts @@ -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 }); +} diff --git a/app/inside/IdComponent.tsx b/app/inside/IdComponent.tsx index 437ab72..4b4c4dd 100644 --- a/app/inside/IdComponent.tsx +++ b/app/inside/IdComponent.tsx @@ -7,14 +7,15 @@ type Props = { export default function IdComponent({ updateIdList }: Props) { const [idSet, setIdSet] = useState>(new Set()); const onValidId = (id: string) => { - setIdSet((prev) => new Set(prev.add(id))); + const newSet = new Set(idSet.add(id)); + setIdSet(newSet); + updateIdList([...newSet]); }; const removeCid = (id: string) => { - setIdSet((prev) => new Set([...prev].filter((x) => x !== id))); + const newSet = new Set([...idSet].filter((x) => x !== id)); + setIdSet(newSet); + updateIdList([...newSet]); }; - useEffect(() => { - updateIdList([...idSet]); - }, [idSet, updateIdList]); return (
diff --git a/app/inside/action.ts b/app/inside/action.ts new file mode 100644 index 0000000..14ace61 --- /dev/null +++ b/app/inside/action.ts @@ -0,0 +1,5 @@ +"use server"; + +export async function saveUser(ids: string[]) { + console.log(ids); +} //TODO: submit inside user diff --git a/app/inside/page.tsx b/app/inside/page.tsx index c94b4de..f1e73b7 100644 --- a/app/inside/page.tsx +++ b/app/inside/page.tsx @@ -1,10 +1,14 @@ "use client"; import { useState } from "react"; +import { saveUser } from "./action"; import IdComponent from "./IdComponent"; export default function Page() { const [idList, setIdList] = useState([]); - function submit() {} //TODO! submit inside user + async function submit() { + await saveUser(idList); + alert("อัพเดทสำเร็จ"); + } return (
setIdList(cids)} />