upd: edit divisi
No Issues
This commit is contained in:
@@ -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 (
|
||||
<ViewEditDivision/>
|
||||
<Box>
|
||||
<EditDivision />
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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 });
|
||||
}
|
||||
};
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
@@ -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<any>([]);
|
||||
const [dataMember, setDataMember] = useState<TypeUser>([])
|
||||
const [memberDb, setMemberDb] = useState<IDataMemberDivision[]>([])
|
||||
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={<HiMagnifyingGlass size={20} />}
|
||||
placeholder="Pencarian"
|
||||
onChange={(e: any) => loadMember(group, e.target.value)}
|
||||
/>
|
||||
</Stack>
|
||||
<Box mt={20}>
|
||||
{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 (
|
||||
<Box my={10} key={index} onClick={() => handleFileClick(index)}>
|
||||
<Box my={10} key={index} onClick={() => (!found) ? handleFileClick(index) : null}>
|
||||
<Group justify='space-between' align='center'>
|
||||
<Group>
|
||||
<Avatar src={v.img} alt="it's me" size="lg" />
|
||||
<Text>{v.name}</Text>
|
||||
<Avatar src={"v.img"} alt="it's me" size="lg" />
|
||||
<Stack align="flex-start" justify="flex-start">
|
||||
<Text>{v.name}</Text>
|
||||
<Text c={"dimmed"}>{(found) ? "sudah menjadi anggota divisi" : ""}</Text>
|
||||
</Stack>
|
||||
</Group>
|
||||
{isSelected ? <FaCheck /> : null}
|
||||
</Group>
|
||||
@@ -147,8 +171,14 @@ export default function CreateAnggotaDivision() {
|
||||
</Box>
|
||||
</Box>
|
||||
<LayoutModal opened={isOpen} onClose={() => 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)
|
||||
}
|
||||
}} />
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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<any>({
|
||||
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 (
|
||||
<Box>
|
||||
<LayoutNavbarNew back="/division/info/1" title="Edit Divisi"
|
||||
menu
|
||||
/>
|
||||
<LayoutNavbarNew back="" title="Edit Divisi" menu />
|
||||
<Box p={20}>
|
||||
|
||||
<Stack>
|
||||
<Select
|
||||
{/* <Select
|
||||
placeholder="Grup"
|
||||
label="Grup"
|
||||
size="md"
|
||||
required
|
||||
radius={40}
|
||||
/>
|
||||
/> */}
|
||||
<TextInput
|
||||
placeholder="Judul"
|
||||
label="Judul"
|
||||
size="md"
|
||||
required
|
||||
radius={40}
|
||||
value={body.name}
|
||||
onChange={(e) => { setBody({ ...body, name: e.target.value }) }}
|
||||
/>
|
||||
<Textarea placeholder="Deskripsi" label="Deskripsi" size="md" radius={10}
|
||||
value={body.desc}
|
||||
onChange={(e) => { setBody({ ...body, desc: e.currentTarget.value }) }}
|
||||
/>
|
||||
<Textarea placeholder="Deskripsi" label="Deskripsi" size="md" radius={10} />
|
||||
<Box mt="xl">
|
||||
<Button
|
||||
color="white"
|
||||
@@ -54,7 +116,14 @@ export default function EditDivision() {
|
||||
</Box>
|
||||
</Stack>
|
||||
</Box>
|
||||
<LayoutModal opened={openModal} onClose={() => setOpenModal(false)} description='Apakah Anda yakin ingin edit data' onYes={(val) => { onTrue(val) }} />
|
||||
<LayoutModal opened={openModal} onClose={() => setOpenModal(false)} description='Apakah Anda yakin ingin edit data'
|
||||
onYes={(val) => {
|
||||
if (val) {
|
||||
onUpdate()
|
||||
} else {
|
||||
setOpenModal(false)
|
||||
}
|
||||
}} />
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ import { apiDivision } from "./api/api_division";
|
||||
import CreateAdminDivision from "./components/create_admin_division";
|
||||
import CreateUsers from "./components/create_users";
|
||||
import ViewCreateReport from "./view/view_create_report";
|
||||
import ViewEditDivision from "./view/view_edit_division";
|
||||
import ViewReportDivision from "./view/view_report_division";
|
||||
import ListDivision from './components/list_division';
|
||||
import CreateDivision from './components/create_division';
|
||||
@@ -34,6 +33,7 @@ import ListDocumentOnDetailDivision from './components/list_document';
|
||||
import ListDiscussionOnDetailDivision from './components/list_discussion';
|
||||
import InformationDivision from './components/information_division';
|
||||
import CreateAnggotaDivision from './components/create_anggota_division';
|
||||
import EditDivision from './components/edit_division';
|
||||
|
||||
export { CreateUsers };
|
||||
export { CreateAdminDivision };
|
||||
@@ -57,7 +57,6 @@ export { ViewDetailDiscussion };
|
||||
export { ViewEditDiscussion };
|
||||
export { ViewDocumentDivision };
|
||||
export { ViewReportDivision };
|
||||
export { ViewEditDivision };
|
||||
export { apiDivision }
|
||||
export { apiDiscussion }
|
||||
export type { IFormDivision, IFormMemberDivision, IFormFixDivision, IDataDivison, IDataMemberDivision }
|
||||
@@ -71,3 +70,4 @@ export { ListDocumentOnDetailDivision }
|
||||
export { ListDiscussionOnDetailDivision }
|
||||
export { InformationDivision }
|
||||
export { CreateAnggotaDivision }
|
||||
export { EditDivision }
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { IFormFixDivision, IFormMemberDivision } from "./type_division";
|
||||
import { IFormDivision, IFormFixDivision, IFormMemberDivision } from "./type_division";
|
||||
|
||||
export const funGetAllDivision = async (path?: string) => {
|
||||
const response = await fetch(`/api/division${(path) ? path : ''}`, { next: { tags: ['division'] } });
|
||||
@@ -26,6 +26,17 @@ export const funCreateDivision = async (data: IFormFixDivision) => {
|
||||
return await response.json().catch(() => null);
|
||||
}
|
||||
|
||||
export const funEditDivision = async (path: string, data: IFormDivision) => {
|
||||
const response = await fetch(`/api/division/${path}`, {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return await response.json().catch(() => null);
|
||||
}
|
||||
|
||||
export const funDeleteMemberDivision = async (path: string, data: { id: string }) => {
|
||||
const response = await fetch(`/api/division/${path}/detail`, {
|
||||
method: "DELETE",
|
||||
@@ -47,4 +58,16 @@ export const funEditStatusAdminDivision = async (path: string, data: { id: strin
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return await response.json().catch(() => null);
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
export const funAddDivisionMember = async (path: string, data: IFormMemberDivision) => {
|
||||
const response = await fetch(`/api/division/${path}/detail`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return await response.json().catch(() => null);
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
|
||||
import { LayoutNavbarNew, WARNA } from '@/module/_global';
|
||||
import { Box, Button, Select, Stack, Textarea, TextInput } from '@mantine/core';
|
||||
import React from 'react';
|
||||
import EditDivision from '../components/edit_division';
|
||||
|
||||
export default function ViewEditDivision() {
|
||||
return (
|
||||
<Box>
|
||||
<EditDivision/>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user