feat : update position
This commit is contained in:
@@ -12,6 +12,7 @@ export async function getOnePosition(req: NextRequest) {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
idGroup: true,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<any>({
|
||||
id: id,
|
||||
name: "",
|
||||
idGroup: ""
|
||||
})
|
||||
const [listGroup, setListGorup] = useState<dataGroup[]>([])
|
||||
|
||||
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, }: {
|
||||
<Select
|
||||
label="Grup"
|
||||
placeholder="Pilih grup"
|
||||
data={['Dinas', 'Adat', 'LPD']}
|
||||
size="md"
|
||||
radius={10}
|
||||
data={
|
||||
listGroup
|
||||
? listGroup.map((data) => ({
|
||||
value: data.id,
|
||||
label: data.name,
|
||||
}))
|
||||
: []
|
||||
}
|
||||
value={String(data.idGroup)}
|
||||
mb={5}
|
||||
onChange={(val) => setData({ ...data, idGroup: val })}
|
||||
withAsterisk
|
||||
styles={{
|
||||
input: {
|
||||
@@ -115,6 +188,8 @@ export default function DrawerDetailPosition({ onUpdated, id, isActive, }: {
|
||||
}}
|
||||
my={15}
|
||||
size="md"
|
||||
value={String(data.name)}
|
||||
onChange={(e) => setData({ ...data, name: e.target.value })}
|
||||
radius={10}
|
||||
placeholder="Nama Jabatan"
|
||||
/>
|
||||
@@ -125,7 +200,7 @@ export default function DrawerDetailPosition({ onUpdated, id, isActive, }: {
|
||||
size="lg"
|
||||
radius={30}
|
||||
fullWidth
|
||||
onClick={onCLose}
|
||||
onClick={onSubmit}
|
||||
>
|
||||
EDIT
|
||||
</Button>
|
||||
|
||||
@@ -1,18 +1,71 @@
|
||||
import { WARNA, LayoutDrawer } from "@/module/_global";
|
||||
import { WARNA, LayoutDrawer, API_ADDRESS } from "@/module/_global";
|
||||
import { Box, Stack, SimpleGrid, Flex, TextInput, Button, Text, Select } from "@mantine/core";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useState } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import toast from "react-hot-toast";
|
||||
import { IoAddCircle } from "react-icons/io5";
|
||||
import { RiFilter2Line } from "react-icons/ri";
|
||||
|
||||
type dataGroup = {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
|
||||
export default function DrawerListPosition({ onCreated }: { onCreated: (val: boolean) => void }) {
|
||||
const [openDrawerGroup, setOpenDrawerGroup] = useState(false)
|
||||
const router = useRouter()
|
||||
const [listGroup, setListGorup] = useState<dataGroup[]>([])
|
||||
|
||||
function onCLose() {
|
||||
setOpenDrawerGroup(false)
|
||||
onCreated(true)
|
||||
const [listData, setListData] = useState({
|
||||
name: "",
|
||||
idGroup: "",
|
||||
})
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
getAllGroup()
|
||||
}, [])
|
||||
|
||||
|
||||
async function onSubmit() {
|
||||
try {
|
||||
const res = await fetch(API_ADDRESS.apiCreatePosition, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
name: listData.name,
|
||||
idGroup: listData.idGroup
|
||||
})
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
const errorData = await res.json();
|
||||
if (errorData.message === "Position sudah ada") {
|
||||
toast.error('Gagal! Position sudah ada');
|
||||
} else {
|
||||
toast.error('Error');
|
||||
}
|
||||
} else {
|
||||
setOpenDrawerGroup(false)
|
||||
toast.success('Sukses! data tersimpan')
|
||||
}
|
||||
onCreated(true)
|
||||
} catch (error) {
|
||||
toast.error('Error')
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Box>
|
||||
<Stack pt={10}>
|
||||
@@ -43,11 +96,22 @@ export default function DrawerListPosition({ onCreated }: { onCreated: (val: boo
|
||||
<Select
|
||||
label="Grup"
|
||||
placeholder="Pilih grup"
|
||||
data={['Dinas', 'Adat', 'LPD']}
|
||||
data={
|
||||
listGroup
|
||||
? listGroup.map((data) => ({
|
||||
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
|
||||
</Button>
|
||||
|
||||
@@ -110,10 +110,8 @@ export default function ListPositionActive({ status }: { status: boolean }) {
|
||||
>
|
||||
<DrawerDetailPosition
|
||||
id={selectId}
|
||||
isActive={active}
|
||||
onUpdated={() => {
|
||||
setOpenDrawer(false);
|
||||
toast.success("Sukses! data tersimpan");
|
||||
}}
|
||||
/>
|
||||
</LayoutDrawer>
|
||||
|
||||
@@ -20,7 +20,7 @@ export default function NavbarListPosition() {
|
||||
<LayoutDrawer opened={isOpen} title={'Menu'} onClose={() => setOpen(false)}>
|
||||
<DrawerListPosition onCreated={() => {
|
||||
setOpen(false)
|
||||
toast.success('Sukses! data tersimpan')
|
||||
// toast.success('Sukses! data tersimpan')
|
||||
}} />
|
||||
</LayoutDrawer>
|
||||
</>
|
||||
|
||||
Reference in New Issue
Block a user