Merge pull request #83 from bipproduction/lukman/31-juli-2024
feat : update position
This commit is contained in:
@@ -12,6 +12,7 @@ export async function getOnePosition(req: NextRequest) {
|
|||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
name: true,
|
name: true,
|
||||||
|
idGroup: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -3,22 +3,37 @@ import { prisma } from "@/module/_global";
|
|||||||
export async function createlPosition(req: Request) {
|
export async function createlPosition(req: Request) {
|
||||||
try {
|
try {
|
||||||
const data = await req.json();
|
const data = await req.json();
|
||||||
|
const cek = await prisma.position.count({
|
||||||
const positions = await prisma.position.create({
|
where: {
|
||||||
data: {
|
|
||||||
name: data.name,
|
name: data.name,
|
||||||
isActive: true,
|
|
||||||
idGroup: data.idGroup,
|
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) {
|
} catch (error) {
|
||||||
console.error(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) {
|
export async function updatePosition(req: Request) {
|
||||||
try {
|
try {
|
||||||
const data = await req.json()
|
const data = await req.json();
|
||||||
|
const cek = await prisma.position.count({
|
||||||
|
where: {
|
||||||
|
name: data.name,
|
||||||
|
idGroup: data.idGroup,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const update = await prisma.position.update({
|
if (cek == 0) {
|
||||||
where: {
|
const update = await prisma.position.update({
|
||||||
id: data.id
|
where: {
|
||||||
},
|
id: data.id,
|
||||||
data: {
|
},
|
||||||
name: data.name,
|
data: {
|
||||||
idGroup: data.idGroup
|
name: data.name,
|
||||||
}
|
idGroup: data.idGroup,
|
||||||
})
|
},
|
||||||
|
});
|
||||||
return Response.json({ success: true, message: "Sukses Update Position" }, { status: 200 });
|
|
||||||
} catch (error) {
|
return Response.json({ success: true, message: "Sukses Update Position" }, { status: 200 });
|
||||||
console.error(error);
|
} else {
|
||||||
return Response.json({ message: "Internal Server Error", success: false }, { status: 500 });
|
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 { API_ADDRESS, LayoutDrawer, WARNA } from "@/module/_global"
|
||||||
import LayoutModal from "@/module/_global/layout/layout_modal"
|
import LayoutModal from "@/module/_global/layout/layout_modal"
|
||||||
import { Box, Stack, SimpleGrid, Flex, Text, Select, TextInput, Button } from "@mantine/core"
|
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 toast from "react-hot-toast"
|
||||||
import { FaPencil, FaToggleOff } from "react-icons/fa6"
|
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,
|
onUpdated: (val: boolean) => void, id: string | null,
|
||||||
isActive: boolean | null;
|
|
||||||
}) {
|
}) {
|
||||||
const [openDrawerGroup, setOpenDrawerGroup] = useState(false)
|
const [openDrawerGroup, setOpenDrawerGroup] = useState(false)
|
||||||
const [isModal, setModal] = useState(false)
|
const [isModal, setModal] = useState(false)
|
||||||
|
const [data, setData] = useState<any>({
|
||||||
|
id: id,
|
||||||
|
name: "",
|
||||||
|
idGroup: ""
|
||||||
|
})
|
||||||
|
const [listGroup, setListGorup] = useState<dataGroup[]>([])
|
||||||
|
|
||||||
function onCLose() {
|
function onCLose() {
|
||||||
onUpdated(true)
|
onUpdated(true)
|
||||||
@@ -23,35 +34,88 @@ export default function DrawerDetailPosition({ onUpdated, id, isActive, }: {
|
|||||||
}
|
}
|
||||||
setModal(false)
|
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) {
|
async function nonActive(val: boolean) {
|
||||||
try {
|
try {
|
||||||
if (val) {
|
if (val) {
|
||||||
const res = await fetch(API_ADDRESS.apiDeletePosition, {
|
const res = await fetch(API_ADDRESS.apiDeletePosition, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
id,
|
data
|
||||||
isActive,
|
}),
|
||||||
}),
|
});
|
||||||
});
|
|
||||||
|
if (res.status == 200) {
|
||||||
if (res.status == 200) {
|
onUpdated(true);
|
||||||
onUpdated(true);
|
} else {
|
||||||
} else {
|
onUpdated(false);
|
||||||
onUpdated(false);
|
}
|
||||||
}
|
}
|
||||||
}
|
setModal(false);
|
||||||
setModal(false);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
setModal(false);
|
setModal(false);
|
||||||
toast.error("Terjadi kesalahan");
|
toast.error("Terjadi kesalahan");
|
||||||
onUpdated(false);
|
onUpdated(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -91,10 +155,19 @@ export default function DrawerDetailPosition({ onUpdated, id, isActive, }: {
|
|||||||
<Select
|
<Select
|
||||||
label="Grup"
|
label="Grup"
|
||||||
placeholder="Pilih grup"
|
placeholder="Pilih grup"
|
||||||
data={['Dinas', 'Adat', 'LPD']}
|
|
||||||
size="md"
|
size="md"
|
||||||
radius={10}
|
radius={10}
|
||||||
|
data={
|
||||||
|
listGroup
|
||||||
|
? listGroup.map((data) => ({
|
||||||
|
value: data.id,
|
||||||
|
label: data.name,
|
||||||
|
}))
|
||||||
|
: []
|
||||||
|
}
|
||||||
|
value={String(data.idGroup)}
|
||||||
mb={5}
|
mb={5}
|
||||||
|
onChange={(val) => setData({ ...data, idGroup: val })}
|
||||||
withAsterisk
|
withAsterisk
|
||||||
styles={{
|
styles={{
|
||||||
input: {
|
input: {
|
||||||
@@ -115,6 +188,8 @@ export default function DrawerDetailPosition({ onUpdated, id, isActive, }: {
|
|||||||
}}
|
}}
|
||||||
my={15}
|
my={15}
|
||||||
size="md"
|
size="md"
|
||||||
|
value={String(data.name)}
|
||||||
|
onChange={(e) => setData({ ...data, name: e.target.value })}
|
||||||
radius={10}
|
radius={10}
|
||||||
placeholder="Nama Jabatan"
|
placeholder="Nama Jabatan"
|
||||||
/>
|
/>
|
||||||
@@ -125,7 +200,7 @@ export default function DrawerDetailPosition({ onUpdated, id, isActive, }: {
|
|||||||
size="lg"
|
size="lg"
|
||||||
radius={30}
|
radius={30}
|
||||||
fullWidth
|
fullWidth
|
||||||
onClick={onCLose}
|
onClick={onSubmit}
|
||||||
>
|
>
|
||||||
EDIT
|
EDIT
|
||||||
</Button>
|
</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 { Box, Stack, SimpleGrid, Flex, TextInput, Button, Text, Select } from "@mantine/core";
|
||||||
import { useRouter } from "next/navigation";
|
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 { IoAddCircle } from "react-icons/io5";
|
||||||
import { RiFilter2Line } from "react-icons/ri";
|
import { RiFilter2Line } from "react-icons/ri";
|
||||||
|
|
||||||
|
type dataGroup = {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
};
|
||||||
|
|
||||||
export default function DrawerListPosition({ onCreated }: { onCreated: (val: boolean) => void }) {
|
export default function DrawerListPosition({ onCreated }: { onCreated: (val: boolean) => void }) {
|
||||||
const [openDrawerGroup, setOpenDrawerGroup] = useState(false)
|
const [openDrawerGroup, setOpenDrawerGroup] = useState(false)
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
const [listGroup, setListGorup] = useState<dataGroup[]>([])
|
||||||
|
|
||||||
function onCLose() {
|
const [listData, setListData] = useState({
|
||||||
setOpenDrawerGroup(false)
|
name: "",
|
||||||
onCreated(true)
|
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 (
|
return (
|
||||||
<Box>
|
<Box>
|
||||||
<Stack pt={10}>
|
<Stack pt={10}>
|
||||||
@@ -43,11 +96,22 @@ export default function DrawerListPosition({ onCreated }: { onCreated: (val: boo
|
|||||||
<Select
|
<Select
|
||||||
label="Grup"
|
label="Grup"
|
||||||
placeholder="Pilih grup"
|
placeholder="Pilih grup"
|
||||||
data={['Dinas', 'Adat', 'LPD']}
|
data={
|
||||||
|
listGroup
|
||||||
|
? listGroup.map((data) => ({
|
||||||
|
value: data.id,
|
||||||
|
label: data.name,
|
||||||
|
}))
|
||||||
|
: []
|
||||||
|
}
|
||||||
size="md"
|
size="md"
|
||||||
radius={10}
|
radius={10}
|
||||||
mb={5}
|
mb={5}
|
||||||
withAsterisk
|
withAsterisk
|
||||||
|
onChange={(val: any) => setListData({
|
||||||
|
...listData,
|
||||||
|
idGroup: val
|
||||||
|
})}
|
||||||
styles={{
|
styles={{
|
||||||
input: {
|
input: {
|
||||||
color: WARNA.biruTua,
|
color: WARNA.biruTua,
|
||||||
@@ -67,6 +131,10 @@ export default function DrawerListPosition({ onCreated }: { onCreated: (val: boo
|
|||||||
}}
|
}}
|
||||||
my={15}
|
my={15}
|
||||||
size="md"
|
size="md"
|
||||||
|
onChange={(event: any) => setListData({
|
||||||
|
...listData,
|
||||||
|
name: event.target.value
|
||||||
|
})}
|
||||||
radius={10}
|
radius={10}
|
||||||
placeholder="Nama Jabatan"
|
placeholder="Nama Jabatan"
|
||||||
/>
|
/>
|
||||||
@@ -77,7 +145,7 @@ export default function DrawerListPosition({ onCreated }: { onCreated: (val: boo
|
|||||||
size="lg"
|
size="lg"
|
||||||
radius={30}
|
radius={30}
|
||||||
fullWidth
|
fullWidth
|
||||||
onClick={onCLose}
|
onClick={onSubmit}
|
||||||
>
|
>
|
||||||
MASUK
|
MASUK
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
@@ -110,10 +110,8 @@ export default function ListPositionActive({ status }: { status: boolean }) {
|
|||||||
>
|
>
|
||||||
<DrawerDetailPosition
|
<DrawerDetailPosition
|
||||||
id={selectId}
|
id={selectId}
|
||||||
isActive={active}
|
|
||||||
onUpdated={() => {
|
onUpdated={() => {
|
||||||
setOpenDrawer(false);
|
setOpenDrawer(false);
|
||||||
toast.success("Sukses! data tersimpan");
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</LayoutDrawer>
|
</LayoutDrawer>
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ export default function NavbarListPosition() {
|
|||||||
<LayoutDrawer opened={isOpen} title={'Menu'} onClose={() => setOpen(false)}>
|
<LayoutDrawer opened={isOpen} title={'Menu'} onClose={() => setOpen(false)}>
|
||||||
<DrawerListPosition onCreated={() => {
|
<DrawerListPosition onCreated={() => {
|
||||||
setOpen(false)
|
setOpen(false)
|
||||||
toast.success('Sukses! data tersimpan')
|
// toast.success('Sukses! data tersimpan')
|
||||||
}} />
|
}} />
|
||||||
</LayoutDrawer>
|
</LayoutDrawer>
|
||||||
</>
|
</>
|
||||||
|
|||||||
Reference in New Issue
Block a user