From 4a4da15998f6f29032cb76e2f45f70926a5c97eb Mon Sep 17 00:00:00 2001 From: lukman Date: Wed, 31 Jul 2024 17:03:44 +0800 Subject: [PATCH] feat : update position --- src/module/position/api/get/getOnePosition.ts | 1 + .../position/api/post/createPosition.ts | 35 +++-- .../position/api/post/updatePosition.ts | 59 +++++--- .../component/ui/drawer_detail_position.tsx | 133 ++++++++++++++---- .../component/ui/drawer_list_position.tsx | 82 ++++++++++- .../component/ui/list_position_active.tsx | 2 - .../component/ui/navbar_list_position.tsx | 2 +- 7 files changed, 246 insertions(+), 68 deletions(-) diff --git a/src/module/position/api/get/getOnePosition.ts b/src/module/position/api/get/getOnePosition.ts index 6246a05..e9e7d83 100644 --- a/src/module/position/api/get/getOnePosition.ts +++ b/src/module/position/api/get/getOnePosition.ts @@ -12,6 +12,7 @@ export async function getOnePosition(req: NextRequest) { select: { id: true, name: true, + idGroup: true, }, }); diff --git a/src/module/position/api/post/createPosition.ts b/src/module/position/api/post/createPosition.ts index 35ca42a..5b6a0bb 100644 --- a/src/module/position/api/post/createPosition.ts +++ b/src/module/position/api/post/createPosition.ts @@ -3,22 +3,37 @@ import { prisma } from "@/module/_global"; export async function createlPosition(req: Request) { try { const data = await req.json(); - - const positions = await prisma.position.create({ - data: { + const cek = await prisma.position.count({ + where: { name: data.name, - isActive: true, idGroup: data.idGroup, }, - select: { - id: true, - name: true, - }, }); + if (cek == 0) { + const positions = await prisma.position.create({ + data: { + name: data.name, + idGroup: data.idGroup, + }, + select: { + id: true, + name: true, + }, + }); - return Response.json(positions, { status: 201 }); + return Response.json(positions, { status: 201 }); + } else { + return Response.json( + { success: false, message: "Position sudah ada" }, + { status: 400 } + ); + } + } catch (error) { console.error(error); - return Response.json({ success: false, message: "Internal Server Error" }, { status: 500 }); + return Response.json( + { success: false, message: "Internal Server Error" }, + { status: 500 } + ); } } diff --git a/src/module/position/api/post/updatePosition.ts b/src/module/position/api/post/updatePosition.ts index 806de8f..3bce6dc 100644 --- a/src/module/position/api/post/updatePosition.ts +++ b/src/module/position/api/post/updatePosition.ts @@ -1,22 +1,43 @@ -import { prisma } from "@/module/_global" +import { prisma } from "@/module/_global"; export async function updatePosition(req: Request) { - try { - const data = await req.json() + try { + const data = await req.json(); + const cek = await prisma.position.count({ + where: { + name: data.name, + idGroup: data.idGroup, + }, + }); - const update = await prisma.position.update({ - where: { - id: data.id - }, - data: { - name: data.name, - idGroup: data.idGroup - } - }) - - return Response.json({ success: true, message: "Sukses Update Position" }, { status: 200 }); - } catch (error) { - console.error(error); - return Response.json({ message: "Internal Server Error", success: false }, { status: 500 }); - } -} \ No newline at end of file + if (cek == 0) { + const update = await prisma.position.update({ + where: { + id: data.id, + }, + data: { + name: data.name, + idGroup: data.idGroup, + }, + }); + + return Response.json({ success: true, message: "Sukses Update Position" }, { status: 200 }); + } else { + return Response.json( + { success: false, message: "Position sudah ada" }, + { status: 400 } + ); + } + + return Response.json( + { success: true, message: "Sukses Update Position" }, + { status: 200 } + ); + } catch (error) { + console.error(error); + return Response.json( + { message: "Internal Server Error", success: false }, + { status: 500 } + ); + } +} diff --git a/src/module/position/component/ui/drawer_detail_position.tsx b/src/module/position/component/ui/drawer_detail_position.tsx index 20bf8d7..b67050b 100644 --- a/src/module/position/component/ui/drawer_detail_position.tsx +++ b/src/module/position/component/ui/drawer_detail_position.tsx @@ -1,16 +1,27 @@ import { API_ADDRESS, LayoutDrawer, WARNA } from "@/module/_global" import LayoutModal from "@/module/_global/layout/layout_modal" import { Box, Stack, SimpleGrid, Flex, Text, Select, TextInput, Button } from "@mantine/core" -import { useState } from "react" +import { useEffect, useState } from "react" import toast from "react-hot-toast" import { FaPencil, FaToggleOff } from "react-icons/fa6" -export default function DrawerDetailPosition({ onUpdated, id, isActive, }: { +type dataGroup = { + id: string; + name: string; + idGroup: string +}; + +export default function DrawerDetailPosition({ onUpdated, id }: { onUpdated: (val: boolean) => void, id: string | null, - isActive: boolean | null; }) { const [openDrawerGroup, setOpenDrawerGroup] = useState(false) const [isModal, setModal] = useState(false) + const [data, setData] = useState({ + id: id, + name: "", + idGroup: "" + }) + const [listGroup, setListGorup] = useState([]) function onCLose() { onUpdated(true) @@ -23,35 +34,88 @@ export default function DrawerDetailPosition({ onUpdated, id, isActive, }: { } setModal(false) } + async function getAllGroup() { + try { + const res = await fetch(`${API_ADDRESS.apiGetAllGroup}&villageId=121212&active=true`) + const data = await res.json() + setListGorup(data) + } catch (error) { + console.error(error) + } + } + + async function getOneData() { + try { + const res = await fetch(`${API_ADDRESS.apiGetOnePosition}&positionId=${id}`) + const data = await res.json() + setData(data) + } catch (error) { + console.error(error) + } + } + + + async function onSubmit() { + try { + const res = await fetch(API_ADDRESS.apiUpdatePosition, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + id: data.id, + name: data.name, + idGroup: data.idGroup + }), + }) + + const respon = await res.json() + + if (res.status == 200) { + toast.success(respon.message) + } else { + toast.error(respon.message) + } + + onUpdated(true) + onCLose(); + } catch (error) { + toast.error('Error'); + } + } + + useEffect(() => { + getAllGroup() + getOneData() + }, []) async function nonActive(val: boolean) { try { - if (val) { - const res = await fetch(API_ADDRESS.apiDeletePosition, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - id, - isActive, - }), - }); - - if (res.status == 200) { - onUpdated(true); - } else { - onUpdated(false); - } - } - setModal(false); + if (val) { + const res = await fetch(API_ADDRESS.apiDeletePosition, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + data + }), + }); + + if (res.status == 200) { + onUpdated(true); + } else { + onUpdated(false); + } + } + setModal(false); } catch (error) { - console.log(error); - setModal(false); - toast.error("Terjadi kesalahan"); - onUpdated(false); + console.log(error); + setModal(false); + toast.error("Terjadi kesalahan"); + onUpdated(false); } - } + } return ( @@ -91,10 +155,19 @@ export default function DrawerDetailPosition({ onUpdated, id, isActive, }: { ({ + value: data.id, + label: data.name, + })) + : [] + } size="md" radius={10} mb={5} withAsterisk + onChange={(val: any) => setListData({ + ...listData, + idGroup: val + })} styles={{ input: { color: WARNA.biruTua, @@ -67,6 +131,10 @@ export default function DrawerListPosition({ onCreated }: { onCreated: (val: boo }} my={15} size="md" + onChange={(event: any) => setListData({ + ...listData, + name: event.target.value + })} radius={10} placeholder="Nama Jabatan" /> @@ -77,7 +145,7 @@ export default function DrawerListPosition({ onCreated }: { onCreated: (val: boo size="lg" radius={30} fullWidth - onClick={onCLose} + onClick={onSubmit} > MASUK diff --git a/src/module/position/component/ui/list_position_active.tsx b/src/module/position/component/ui/list_position_active.tsx index 1d6b1a8..b67a558 100644 --- a/src/module/position/component/ui/list_position_active.tsx +++ b/src/module/position/component/ui/list_position_active.tsx @@ -110,10 +110,8 @@ export default function ListPositionActive({ status }: { status: boolean }) { > { setOpenDrawer(false); - toast.success("Sukses! data tersimpan"); }} /> diff --git a/src/module/position/component/ui/navbar_list_position.tsx b/src/module/position/component/ui/navbar_list_position.tsx index 8034969..c25ad65 100644 --- a/src/module/position/component/ui/navbar_list_position.tsx +++ b/src/module/position/component/ui/navbar_list_position.tsx @@ -20,7 +20,7 @@ export default function NavbarListPosition() { setOpen(false)}> { setOpen(false) - toast.success('Sukses! data tersimpan') + // toast.success('Sukses! data tersimpan') }} />