upd: api group

Deskripsi:
- pengaplikasian api group metode terbaru pada beberapa fitur

No Issues
This commit is contained in:
amel
2024-08-08 17:20:08 +08:00
parent 7f18f1cb78
commit f83fae160c
13 changed files with 105 additions and 196 deletions

View File

@@ -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" });
}

View File

@@ -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,
},
];

View File

@@ -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 }
);
}
}

View File

@@ -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 });
}
}

View File

@@ -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 }

View File

@@ -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");
}
}