Merge pull request #100 from bipproduction/amalia/8-agustus-24
upd: api group
This commit is contained in:
@@ -1,6 +0,0 @@
|
||||
import { apiGroup } from "@/module/group";
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
return apiGroup(req, "GET")
|
||||
}
|
||||
@@ -5,42 +5,41 @@ import { FaCheck } from "react-icons/fa6";
|
||||
import { WARNA } from "../fun/WARNA";
|
||||
import LayoutNavbarNew from "../layout/layout_navbar_new";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { API_ADDRESS } from "../bin/api_address";
|
||||
import { funGetAllGroup, IDataGroup } from "@/module/group";
|
||||
import { useShallowEffect } from "@mantine/hooks";
|
||||
import toast from "react-hot-toast";
|
||||
|
||||
interface dataGroup {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export default function ViewFilter({linkFilter}: {linkFilter: string}) {
|
||||
export default function ViewFilter({ linkFilter }: { linkFilter: string }) {
|
||||
const [selectedFilter, setSelectedFilter] = useState<string | null>(null);
|
||||
const [checked, setChecked] = useState<dataGroup[]>([]);
|
||||
const [checked, setChecked] = useState<IDataGroup[]>([]);
|
||||
const [searchParams, setSearchParams] = useState({ groupId: '' });
|
||||
|
||||
const handleFilterClick = (id: string) => {
|
||||
setSelectedFilter(id);
|
||||
setSelectedFilter(id);
|
||||
};
|
||||
|
||||
|
||||
async function getAllGroupFilter() {
|
||||
try {
|
||||
const response = await fetch(`${API_ADDRESS.apiGetAllGroup}&active=true&groupId=${searchParams.groupId}`);
|
||||
const data = await response.json();
|
||||
console.log("mana data", response);
|
||||
setChecked(data);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
try {
|
||||
const response = await funGetAllGroup('?active=true')
|
||||
if (response.success) {
|
||||
setChecked(response.data);
|
||||
} else {
|
||||
toast.error(response.message);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
toast.error("Gagal mendapatkan grup, coba lagi nanti");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
if (selectedFilter) {
|
||||
setSearchParams({ groupId: selectedFilter });
|
||||
}
|
||||
if (selectedFilter) {
|
||||
setSearchParams({ groupId: selectedFilter });
|
||||
}
|
||||
}, [selectedFilter]);
|
||||
|
||||
useEffect(() => {
|
||||
getAllGroupFilter();
|
||||
|
||||
useShallowEffect(() => {
|
||||
getAllGroupFilter();
|
||||
}, [searchParams.groupId]);
|
||||
|
||||
const router = useRouter()
|
||||
|
||||
@@ -24,7 +24,7 @@ import CreateAdminDivision from "./create_admin_division";
|
||||
import CreateUsers from "./create_users";
|
||||
import NavbarCreateUsers from "./ui/navbar_create_users";
|
||||
import NavbarAdminDivision from "./ui/navbar_admin_division";
|
||||
import { IDataGroup } from "@/module/group";
|
||||
import { funGetAllGroup, IDataGroup } from "@/module/group";
|
||||
|
||||
export default function CreateDivision() {
|
||||
const router = useRouter();
|
||||
@@ -40,9 +40,12 @@ export default function CreateDivision() {
|
||||
});
|
||||
|
||||
async function loadData() {
|
||||
const loadGroup = await fetch(API_ADDRESS.apiGetAllGroup + '&active=true');
|
||||
const dataGroup = await loadGroup.json();
|
||||
setDataGroup(dataGroup);
|
||||
const loadGroup = await funGetAllGroup('?active=true')
|
||||
if (loadGroup.success) {
|
||||
setDataGroup(loadGroup.data);
|
||||
} else {
|
||||
toast.error(loadGroup.message);
|
||||
}
|
||||
|
||||
const loadUser = await funGetUserByCookies();
|
||||
setRoleUser(loadUser.idUserRole)
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
import { NextRequest } from "next/server";
|
||||
import { API_INDEX_GROUP } from "./api_index";
|
||||
|
||||
type Method = "GET" | "POST";
|
||||
export async function apiGroup(req: NextRequest, method: Method) {
|
||||
const { searchParams } = new URL(req.url);
|
||||
const path = searchParams.get("path");
|
||||
const act = API_INDEX_GROUP.find((v) => v.path === path && v.method === method);
|
||||
if (!path)
|
||||
return Response.json({ success: false, message: "page not found" }, { status: 404 });
|
||||
if (act) return act.bin(req);
|
||||
|
||||
return Response.json({ success: false, message: "404" });
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
import { getOneGroup } from "./get/getOneGroup";
|
||||
import { listGroups } from "./get/listGroup";
|
||||
|
||||
export const API_INDEX_GROUP = [
|
||||
{
|
||||
path: "get-all-group",
|
||||
method: "GET",
|
||||
bin: listGroups,
|
||||
},
|
||||
{
|
||||
path: "get-one-group",
|
||||
method: "GET",
|
||||
bin: getOneGroup,
|
||||
},
|
||||
];
|
||||
@@ -1,33 +0,0 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
export async function getOneGroup(req: NextRequest): Promise<Response> {
|
||||
try {
|
||||
const searchParams = req.nextUrl.searchParams
|
||||
const groupId = searchParams.get('groupId');
|
||||
const getOne = await prisma.group.findUnique({
|
||||
where: {
|
||||
id: String(groupId),
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!getOne) {
|
||||
return Response.json(
|
||||
{ message: "Grup tidak ditemukan", success: false },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
return Response.json(getOne);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return Response.json(
|
||||
{ message: "Internal Server Error", success: false },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { funGetUserByCookies } from "@/module/auth";
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
export async function listGroups(req: NextRequest): Promise<Response> {
|
||||
try {
|
||||
const user = await funGetUserByCookies()
|
||||
const searchParams = req.nextUrl.searchParams
|
||||
const villaId = user.idVillage
|
||||
const active = searchParams.get('active');
|
||||
const name = searchParams.get('name');
|
||||
const groups = await prisma.group.findMany({
|
||||
where: {
|
||||
isActive: (active == "true" ? true : false),
|
||||
idVillage: String(villaId),
|
||||
name: {
|
||||
contains: (name == undefined || name == null) ? "" : name,
|
||||
mode: "insensitive"
|
||||
}
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
isActive: true
|
||||
},
|
||||
});
|
||||
|
||||
return Response.json(groups);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return Response.json({ success: false, message: "Internal Server Error" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,8 @@
|
||||
import { IDataGroup, IFormGroup, IStatusGroup } from './lib/type_group';
|
||||
import { apiGroup } from "./api/api_group";
|
||||
import { funCreateGroup, funEditGroup, funEditStatusGroup, funGetAllGroup, funGetGroupById } from './lib/api_group';
|
||||
import NavbarGroup from './ui/navbar_group';
|
||||
import TabListGroup from './ui/tab_list_group';
|
||||
|
||||
export { apiGroup };
|
||||
export type { IDataGroup, IFormGroup, IStatusGroup }
|
||||
export { funGetAllGroup, funGetGroupById, funCreateGroup, funEditStatusGroup, funEditGroup }
|
||||
export { NavbarGroup }
|
||||
|
||||
@@ -17,7 +17,7 @@ import React, { useEffect, useState } from "react";
|
||||
import toast from "react-hot-toast";
|
||||
import { FaPencil, FaToggleOff } from "react-icons/fa6";
|
||||
import { IoAddCircle, IoCloseCircleOutline } from "react-icons/io5";
|
||||
import { funEditGroup, funEditStatusGroup } from "../lib/api_group";
|
||||
import { funEditGroup, funEditStatusGroup, funGetGroupById } from "../lib/api_group";
|
||||
|
||||
export default function EditDrawerGroup({ onUpdated, id, isActive, }: { onUpdated: (val: boolean) => void; id: string; isActive: boolean; }) {
|
||||
const [openDrawerGroup, setOpenDrawerGroup] = useState(false);
|
||||
@@ -27,11 +27,17 @@ export default function EditDrawerGroup({ onUpdated, id, isActive, }: { onUpdate
|
||||
|
||||
async function getOneGroup() {
|
||||
try {
|
||||
const res = await fetch(`${API_ADDRESS.apiGetOneGroup}&groupId=${id}`);
|
||||
const data = await res.json();
|
||||
setName(data.name);
|
||||
const res = await funGetGroupById(id);
|
||||
console.log("amalia", res)
|
||||
if (res.success) {
|
||||
setName(res.data.name);
|
||||
} else {
|
||||
toast.error(res.message);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
toast.error("Gagal mendapatkan grup, coba lagi nanti");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,17 +1,12 @@
|
||||
import { API_ADDRESS, LayoutDrawer, WARNA } from "@/module/_global"
|
||||
import LayoutModal from "@/module/_global/layout/layout_modal"
|
||||
import { funGetAllGroup, IDataGroup } from "@/module/group"
|
||||
import { Box, Stack, SimpleGrid, Flex, Text, Select, TextInput, Button } from "@mantine/core"
|
||||
import { useShallowEffect } from "@mantine/hooks"
|
||||
import { useEffect, useState } from "react"
|
||||
import toast from "react-hot-toast"
|
||||
import { FaPencil, FaToggleOff } from "react-icons/fa6"
|
||||
|
||||
type dataGroup = {
|
||||
id: string;
|
||||
name: string;
|
||||
idGroup: string
|
||||
};
|
||||
|
||||
export default function DrawerDetailPosition({ onUpdated, id, isActive }: {
|
||||
onUpdated: (val: boolean) => void, id: string | null, isActive: boolean | null;
|
||||
}) {
|
||||
@@ -22,7 +17,7 @@ export default function DrawerDetailPosition({ onUpdated, id, isActive }: {
|
||||
name: "",
|
||||
idGroup: ""
|
||||
})
|
||||
const [listGroup, setListGorup] = useState<dataGroup[]>([])
|
||||
const [listGroup, setListGorup] = useState<IDataGroup[]>([])
|
||||
|
||||
function onCLose() {
|
||||
onUpdated(true)
|
||||
@@ -31,11 +26,15 @@ export default function DrawerDetailPosition({ onUpdated, id, isActive }: {
|
||||
|
||||
async function getAllGroup() {
|
||||
try {
|
||||
const res = await fetch(`${API_ADDRESS.apiGetAllGroup}&villageId=121212&active=true`)
|
||||
const data = await res.json()
|
||||
setListGorup(data)
|
||||
const response = await funGetAllGroup('?active=true')
|
||||
if (response.success) {
|
||||
setListGorup(response.data);
|
||||
} else {
|
||||
toast.error(response.message);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
toast.error("Gagal mendapatkan grup, coba lagi nanti");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { WARNA, LayoutDrawer, API_ADDRESS } from "@/module/_global";
|
||||
import { funGetAllGroup, IDataGroup } from "@/module/group";
|
||||
import { Box, Stack, SimpleGrid, Flex, TextInput, Button, Text, Select } from "@mantine/core";
|
||||
import { useShallowEffect } from "@mantine/hooks";
|
||||
import { useRouter } from "next/navigation";
|
||||
@@ -7,15 +8,11 @@ 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[]>([])
|
||||
const [listGroup, setListGorup] = useState<IDataGroup[]>([])
|
||||
|
||||
const [listData, setListData] = useState({
|
||||
name: "",
|
||||
@@ -24,11 +21,15 @@ export default function DrawerListPosition({ onCreated }: { onCreated: (val: boo
|
||||
|
||||
async function getAllGroup() {
|
||||
try {
|
||||
const res = await fetch(`${API_ADDRESS.apiGetAllGroup}&villageId=121212&active=true`)
|
||||
const data = await res.json()
|
||||
setListGorup(data)
|
||||
const response = await funGetAllGroup('?active=true')
|
||||
if (response.success) {
|
||||
setListGorup(response.data);
|
||||
} else {
|
||||
toast.error(response.message);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
toast.error("Gagal mendapatkan grup, coba lagi nanti");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,33 +40,33 @@ export default function DrawerListPosition({ onCreated }: { onCreated: (val: boo
|
||||
|
||||
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)
|
||||
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')
|
||||
toast.error('Error')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Box>
|
||||
@@ -135,7 +136,7 @@ export default function DrawerListPosition({ onCreated }: { onCreated: (val: boo
|
||||
onChange={(event: any) => setListData({
|
||||
...listData,
|
||||
name: event.target.value
|
||||
})}
|
||||
})}
|
||||
radius={10}
|
||||
placeholder="Nama Jabatan"
|
||||
/>
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
'use client'
|
||||
import { API_ADDRESS, WARNA } from "@/module/_global";
|
||||
import LayoutModal from "@/module/_global/layout/layout_modal";
|
||||
import { funGetAllGroup, IDataGroup } from "@/module/group";
|
||||
import { Box, Button, Select, Stack, TextInput } from "@mantine/core";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEffect, useState } from "react";
|
||||
import toast from "react-hot-toast";
|
||||
import { HiUser } from "react-icons/hi2";
|
||||
|
||||
type dataGroup = {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
|
||||
type dataPosition = {
|
||||
id: string;
|
||||
name: string;
|
||||
@@ -24,7 +22,7 @@ type dataROleUser = {
|
||||
export default function CreateMember() {
|
||||
const router = useRouter()
|
||||
const [isModal, setModal] = useState(false)
|
||||
const [listGroup, setListGorup] = useState<dataGroup[]>([])
|
||||
const [listGroup, setListGorup] = useState<IDataGroup[]>([])
|
||||
const [listPosition, setListPosition] = useState<dataPosition[]>([])
|
||||
const [listUserRole, setListUserRole] = useState<dataROleUser[]>([])
|
||||
|
||||
@@ -42,11 +40,15 @@ export default function CreateMember() {
|
||||
|
||||
async function getAllGroup() {
|
||||
try {
|
||||
const res = await fetch(`${API_ADDRESS.apiGetAllGroup}&villageId=desa1&active=true`)
|
||||
const data = await res.json()
|
||||
setListGorup(data)
|
||||
const response = await funGetAllGroup('?active=true')
|
||||
if (response.success) {
|
||||
setListGorup(response.data);
|
||||
} else {
|
||||
toast.error(response.message);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
toast.error("Gagal mendapatkan grup, coba lagi nanti");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
'use client'
|
||||
import { API_ADDRESS, WARNA } from "@/module/_global";
|
||||
import LayoutModal from "@/module/_global/layout/layout_modal";
|
||||
import { funGetAllGroup, IDataGroup } from "@/module/group";
|
||||
import { Box, Button, Select, Stack, TextInput } from "@mantine/core";
|
||||
import { useShallowEffect } from "@mantine/hooks";
|
||||
import { useRouter } from "next/navigation";
|
||||
@@ -20,10 +21,7 @@ type dataMember = {
|
||||
idUserRole: string;
|
||||
}
|
||||
|
||||
type dataGroup = {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
|
||||
type dataPosition = {
|
||||
id: string;
|
||||
name: string;
|
||||
@@ -37,7 +35,7 @@ type dataROleUser = {
|
||||
export default function EditMember({ id }: { id: string | undefined }) {
|
||||
const [isModal, setModal] = useState(false)
|
||||
const router = useRouter()
|
||||
const [listGroup, setListGorup] = useState<dataGroup[]>([])
|
||||
const [listGroup, setListGorup] = useState<IDataGroup[]>([])
|
||||
const [listPosition, setListPosition] = useState<dataPosition[]>([])
|
||||
const [listUserRole, setListUserRole] = useState<dataROleUser[]>([])
|
||||
const [data, setData] = useState<dataMember>({
|
||||
@@ -55,11 +53,15 @@ export default function EditMember({ id }: { id: string | undefined }) {
|
||||
|
||||
async function getAllGroup() {
|
||||
try {
|
||||
const res = await fetch(`${API_ADDRESS.apiGetAllGroup}&villageId=desa1&active=true`)
|
||||
const data = await res.json()
|
||||
setListGorup(data)
|
||||
const response = await funGetAllGroup('?active=true')
|
||||
if (response.success) {
|
||||
setListGorup(response.data);
|
||||
} else {
|
||||
toast.error(response.message);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
toast.error("Gagal mendapatkan grup, coba lagi nanti");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user