From 3ba348ad3829e0b00d5d18b1341ef0e3a5464eb0 Mon Sep 17 00:00:00 2001 From: amel Date: Mon, 12 Aug 2024 17:44:57 +0800 Subject: [PATCH] upd: edit divisi No Issues --- .../(application)/division/edit/[id]/page.tsx | 9 +- src/app/api/division/[id]/detail/route.ts | 54 ++++++++++- src/app/api/division/[id]/route.ts | 51 ++++++++++ .../components/create_anggota_division.tsx | 92 ++++++++++++------- .../division_new/components/edit_division.tsx | 87 ++++++++++++++++-- src/module/division_new/index.ts | 4 +- src/module/division_new/lib/api_division.ts | 27 +++++- .../division_new/view/view_edit_division.tsx | 14 --- 8 files changed, 275 insertions(+), 63 deletions(-) delete mode 100644 src/module/division_new/view/view_edit_division.tsx diff --git a/src/app/(application)/division/edit/[id]/page.tsx b/src/app/(application)/division/edit/[id]/page.tsx index 103f05f..2fa9367 100644 --- a/src/app/(application)/division/edit/[id]/page.tsx +++ b/src/app/(application)/division/edit/[id]/page.tsx @@ -1,8 +1,11 @@ -import { ViewEditDivision } from "@/module/division_new" +import { EditDivision } from "@/module/division_new" +import { Box } from "@mantine/core" -function Page() { +function Page({ params }: { params: { id: string } }) { return ( - + + + ) } diff --git a/src/app/api/division/[id]/detail/route.ts b/src/app/api/division/[id]/detail/route.ts index b307df2..d6a649f 100644 --- a/src/app/api/division/[id]/detail/route.ts +++ b/src/app/api/division/[id]/detail/route.ts @@ -207,7 +207,7 @@ export async function DELETE(request: Request, context: { params: { id: string } } -// MENGGANTIS STATUS ADMIN DIVISI +// MENGGANTI STATUS ADMIN DIVISI export async function PUT(request: Request, context: { params: { id: string } }) { try { const user = await funGetUserByCookies() @@ -255,4 +255,54 @@ export async function PUT(request: Request, context: { params: { id: string } }) console.log(error); return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, coba lagi nanti", reason: (error as Error).message, }, { status: 500 }); } -} \ No newline at end of file +} + + +// TAMBAH ANGGOTA DIVISI +export async function POST(request: Request, context: { params: { id: string } }) { + try { + const user = await funGetUserByCookies() + if (user.id == undefined) { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 }); + } + + const member = await request.json(); + const idDivision = context.params.id; + + console.log("amalia", member) + + + const data = await prisma.division.count({ + where: { + id: idDivision, + isActive: true + }, + }); + + if (data == 0) { + return NextResponse.json( + { + success: false, + message: "Tambah anggota divisi gagal, data tidak ditemukan", + }, + { status: 404 } + ); + } + + + const dataMember = member.map((v: any) => ({ + ..._.omit(v, ["name"]), + idUser: v.idUser, + idDivision: idDivision, + })) + + const insertMember = await prisma.divisionMember.createMany({ + data: dataMember + }) + + return NextResponse.json({ success: true, message: "Berhasil menambahkan anggota divisi" }, { status: 200 }); + } catch (error) { + console.log(error); + return NextResponse.json({ success: false, message: "Gagal menambahkan anggota divisi, coba lagi nanti", reason: (error as Error).message, }, { status: 500 }); + } +}; \ No newline at end of file diff --git a/src/app/api/division/[id]/route.ts b/src/app/api/division/[id]/route.ts index ce5b9b0..8cc0757 100644 --- a/src/app/api/division/[id]/route.ts +++ b/src/app/api/division/[id]/route.ts @@ -62,4 +62,55 @@ export async function GET(request: Request, context: { params: { id: string } }) console.log(error); return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, coba lagi nanti", reason: (error as Error).message, }, { status: 500 }); } +} + + + +// EDIT DATA DIVISI +export async function PUT(request: Request, context: { params: { id: string } }) { + try { + const user = await funGetUserByCookies() + if (user.id == undefined) { + return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 }); + } + + const { id } = context.params; + const { name, desc } = (await request.json()); + const data = await prisma.division.count({ + where: { + id: id, + }, + }); + + if (data == 0) { + return NextResponse.json( + { + success: false, + message: "Edit divisi gagal, data tidak ditemukan", + }, + { status: 404 } + ); + } + + const update = await prisma.division.update({ + where: { + id: id, + }, + data: { + name: name, + desc: desc + }, + }); + + return NextResponse.json( + { + success: true, + message: "Divisi berhasil diedit", + }, + { status: 200 } + ); + } catch (error) { + console.log(error); + return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, coba lagi nanti", reason: (error as Error).message, }, { status: 500 }); + } } \ No newline at end of file diff --git a/src/module/division_new/components/create_anggota_division.tsx b/src/module/division_new/components/create_anggota_division.tsx index 44d50ba..b0614d6 100644 --- a/src/module/division_new/components/create_anggota_division.tsx +++ b/src/module/division_new/components/create_anggota_division.tsx @@ -13,6 +13,8 @@ import { HiMagnifyingGlass } from 'react-icons/hi2'; import { globalMemberDivision } from '../lib/val_division'; import { useShallowEffect } from '@mantine/hooks'; import { funGetAllmember } from '@/module/user/member/lib/api_member'; +import { IDataMemberDivision } from '../lib/type_division'; +import { funAddDivisionMember, funGetDivisionById } from '../lib/api_division'; const dataUser = [ { @@ -51,46 +53,63 @@ export default function CreateAnggotaDivision() { const router = useRouter() const [selectedFiles, setSelectedFiles] = useState([]); const [dataMember, setDataMember] = useState([]) + const [memberDb, setMemberDb] = useState([]) + const [group, setGroup] = useState("") const [isOpen, setOpen] = useState(false) const param = useParams<{ id: string }>() - const member = useHookstate(globalMemberDivision) const handleFileClick = (index: number) => { - if (selectedFiles.some((i: any) => i.id == dataMember[index].id)) { - setSelectedFiles(selectedFiles.filter((i: any) => i.id != dataMember[index].id)) + if (selectedFiles.some((i: any) => i.idUser == dataMember[index].id)) { + setSelectedFiles(selectedFiles.filter((i: any) => i.idUser != dataMember[index].id)) } else { setSelectedFiles([...selectedFiles, { idUser: dataMember[index].id, name: dataMember[index].name }]) } }; - function onTrue(val: boolean) { - if (val) { - toast.success("Sukses! Data tersimpan"); - } - setOpen(false) - router.push("/division/info/1") - } - async function loadData() { - console.log("masuk") - const res = await funGetAllmember('?active=true&group=group1'); + async function loadMember(group: string, search: string) { + const res = await funGetAllmember('?active=true&group=' + group + '&search=' + search); const user = await funGetUserByCookies(); - console.log(res) - // if(res.success){ - // setDataMember(res.data.filter((i: any) => i.id != user.id)) - // }else{ - // toast.error(res.message) - // } - - // cek data member sebelumnya - if (member.length > 0) { - setSelectedFiles(JSON.parse(JSON.stringify(member.get()))) + if (res.success) { + setDataMember(res.data.filter((i: any) => i.id != user.id)) + } else { + toast.error(res.message) } } + async function loadFirst() { + const respon = await funGetDivisionById(param.id); + if (respon.success) { + setMemberDb(respon.data.member) + setGroup(respon.data.division.idGroup) + loadMember(respon.data.division.idGroup, "") + } else { + toast.error(respon.message); + } + } + + async function addMember() { + try { + const res = await funAddDivisionMember(param.id, selectedFiles) + if (res.success) { + toast.success(res.message) + router.push("/division/info/" + param.id) + } else { + toast.error(res.message) + } + setOpen(false) + } catch (error) { + setOpen(false) + console.log(error); + toast.error("Gagal menambahkan anggota divisi, coba lagi nanti"); + + } + } + + useShallowEffect(() => { - loadData() + loadFirst() }, []); return ( @@ -112,17 +131,22 @@ export default function CreateAnggotaDivision() { radius={30} leftSection={} placeholder="Pencarian" + onChange={(e: any) => loadMember(group, e.target.value)} /> - {dataUser.map((v, index) => { - const isSelected = selectedFiles[index]; + {dataMember.map((v: any, index: any) => { + const isSelected = selectedFiles.some((i: any) => i.idUser == dataMember[index].id) + const found = memberDb.some((i: any) => i.idUser == v.id) return ( - handleFileClick(index)}> + (!found) ? handleFileClick(index) : null}> - - {v.name} + + + {v.name} + {(found) ? "sudah menjadi anggota divisi" : ""} + {isSelected ? : null} @@ -147,8 +171,14 @@ export default function CreateAnggotaDivision() { setOpen(false)} - description="Apakah Anda yakin ingin menambahkan data?" - onYes={(val) => { onTrue(val) }} /> + description="Apakah Anda yakin ingin menambahkan anggota divisi?" + onYes={(val) => { + if (val) { + addMember() + } else { + setOpen(false) + } + }} /> ); } diff --git a/src/module/division_new/components/edit_division.tsx b/src/module/division_new/components/edit_division.tsx index 21d879b..8ff6084 100644 --- a/src/module/division_new/components/edit_division.tsx +++ b/src/module/division_new/components/edit_division.tsx @@ -2,44 +2,106 @@ import { LayoutNavbarNew, WARNA } from '@/module/_global'; import LayoutModal from '@/module/_global/layout/layout_modal'; import { Box, Button, Select, Stack, Textarea, TextInput } from '@mantine/core'; -import { useRouter } from 'next/navigation'; +import { useShallowEffect } from '@mantine/hooks'; +import { useParams, useRouter } from 'next/navigation'; import React, { useState } from 'react'; import toast from 'react-hot-toast'; +import { funEditDivision, funGetDivisionById } from '../lib/api_division'; +import { funGetAllGroup, IDataGroup } from '@/module/group'; +import { funGetUserByCookies } from '@/module/auth'; export default function EditDivision() { const [openModal, setOpenModal] = useState(false) const router = useRouter() + const param = useParams<{ id: string }>() + const [loading, setLoading] = useState(false) + const [body, setBody] = useState({ + idGroup: "", + name: "", + desc: "", + }); function onTrue(val: boolean) { if (val) { toast.success("Sukses! Data tersimpan"); } setOpenModal(false) - router.push('/division/info/1') } + + + async function getOneData() { + try { + setLoading(true); + const res = await funGetDivisionById(param.id); + if (res.success) { + setBody({ + ...body, + idGroup: res.data.division.idGroup, + name: res.data.division.name, + desc: res.data.division.desc + }) + } else { + toast.error(res.message); + } + setLoading(false); + + } catch (error) { + console.error(error); + toast.error("Gagal mendapatkan divisi, coba lagi nanti"); + } finally { + setLoading(false); + } + } + + + async function onUpdate() { + try { + const res = await funEditDivision(param.id, body) + if (res.success) { + toast.success(res.message) + } else { + toast.error(res.message) + } + setOpenModal(false) + } catch (error) { + console.log(error) + setOpenModal(false) + toast.error("Gagal mengedit divisi, coba lagi nanti"); + } + + } + + useShallowEffect(() => { + getOneData(); + }, [param.id]) + return ( - + + -