API Table Kategori Donasi
This commit is contained in:
44
src/app/api/admin/donasi/kategori/route.ts
Normal file
44
src/app/api/admin/donasi/kategori/route.ts
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import { prisma } from "@/app/lib";
|
||||||
|
import backendLogger from "@/util/backendLogger";
|
||||||
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
|
export async function GET(request: Request) {
|
||||||
|
const method = request.method;
|
||||||
|
if (method !== "GET") {
|
||||||
|
return NextResponse.json({
|
||||||
|
success: false,
|
||||||
|
message: "Method nol allowed"
|
||||||
|
},
|
||||||
|
{ status: 405 }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const data = await prisma.donasiMaster_Kategori.findMany({
|
||||||
|
orderBy: {
|
||||||
|
createdAt: 'asc'
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
|
||||||
|
active: true
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
return NextResponse.json({
|
||||||
|
success: true,
|
||||||
|
message: "Success get kategori",
|
||||||
|
data: data
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
backendLogger.error("Error get kategori", error);
|
||||||
|
return NextResponse.json({
|
||||||
|
success: false,
|
||||||
|
message: "Failed get kategori",
|
||||||
|
reason: (error as Error).message
|
||||||
|
},
|
||||||
|
{ status: 500 }
|
||||||
|
)
|
||||||
|
} finally {
|
||||||
|
await prisma.$disconnect
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,11 +2,11 @@ import { AdminDonasi_TableKategori } from "@/app_modules/admin/donasi";
|
|||||||
import adminDonasi_getMasterKategori from "@/app_modules/admin/donasi/fun/master/get_list_kategori";
|
import adminDonasi_getMasterKategori from "@/app_modules/admin/donasi/fun/master/get_list_kategori";
|
||||||
|
|
||||||
export default async function Page() {
|
export default async function Page() {
|
||||||
const listKategori = await adminDonasi_getMasterKategori();
|
// const listKategori = await adminDonasi_getMasterKategori();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<AdminDonasi_TableKategori listKategori={listKategori} />
|
<AdminDonasi_TableKategori />
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,9 +6,11 @@ import { update } from "lodash";
|
|||||||
export default async function adminDonasi_funUpdatekategoriById({
|
export default async function adminDonasi_funUpdatekategoriById({
|
||||||
kategoriId,
|
kategoriId,
|
||||||
name,
|
name,
|
||||||
|
|
||||||
}: {
|
}: {
|
||||||
kategoriId: string;
|
kategoriId: string;
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
}) {
|
}) {
|
||||||
const updt = await prisma.donasiMaster_Kategori.update({
|
const updt = await prisma.donasiMaster_Kategori.update({
|
||||||
where: {
|
where: {
|
||||||
@@ -16,6 +18,7 @@ export default async function adminDonasi_funUpdatekategoriById({
|
|||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
name: name,
|
name: name,
|
||||||
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
export {
|
export {
|
||||||
apiGetAdminDonasiStatusCountDashboard,
|
apiGetAdminDonasiStatusCountDashboard,
|
||||||
apiGetAdminDonasiKategoriCountDashboard,
|
apiGetAdminDonasiKategoriCountDashboard,
|
||||||
apiGetAdminDonasiByStatus
|
apiGetAdminDonasiByStatus,
|
||||||
|
apiGetAdminDonasiKategori
|
||||||
};
|
};
|
||||||
const apiGetAdminDonasiStatusCountDashboard = async ({ name }:
|
const apiGetAdminDonasiStatusCountDashboard = async ({ name }:
|
||||||
{ name: "Publish" | "Review" | "Reject" }) => {
|
{ name: "Publish" | "Review" | "Reject" }) => {
|
||||||
@@ -66,3 +67,20 @@ const apiGetAdminDonasiByStatus = async ({
|
|||||||
console.log("Ini response", response)
|
console.log("Ini response", response)
|
||||||
return await response.json().catch(() => null);
|
return await response.json().catch(() => null);
|
||||||
}
|
}
|
||||||
|
const apiGetAdminDonasiKategori = async () => {
|
||||||
|
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
|
||||||
|
if (!token) return await token.json().catch(() => null);
|
||||||
|
|
||||||
|
console.log("ini token", token)
|
||||||
|
const response = await fetch(`/api/admin/donasi/kategori`, {
|
||||||
|
method: "GET",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Accept: "application/json",
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
Authorization: `Bearer ${token}`
|
||||||
|
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return await response.json().catch(() => null);
|
||||||
|
}
|
||||||
@@ -32,25 +32,23 @@ import _ from "lodash";
|
|||||||
import { ComponentAdminGlobal_TitlePage } from "../../_admin_global/_component";
|
import { ComponentAdminGlobal_TitlePage } from "../../_admin_global/_component";
|
||||||
import { AccentColor } from "@/app_modules/_global/color";
|
import { AccentColor } from "@/app_modules/_global/color";
|
||||||
import { AdminColor } from "@/app_modules/_global/color/color_pallet";
|
import { AdminColor } from "@/app_modules/_global/color/color_pallet";
|
||||||
|
import { useShallowEffect } from "@mantine/hooks";
|
||||||
|
import { apiGetAdminDonasiKategori } from "../lib/api_fetch_admin_donasi";
|
||||||
|
import { clientLogger } from "@/util/clientLogger";
|
||||||
|
|
||||||
export default function AdminDonasi_TableKategori({
|
export default function AdminDonasi_TableKategori() {
|
||||||
listKategori,
|
|
||||||
}: {
|
|
||||||
listKategori: MODEL_NEW_DEFAULT_MASTER[];
|
|
||||||
}) {
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Stack h={"100%"}>
|
<Stack h={"100%"}>
|
||||||
<ComponentAdminGlobal_HeaderTamplate name="Donasi" />
|
<ComponentAdminGlobal_HeaderTamplate name="Donasi" />
|
||||||
<TableView list={listKategori} />
|
<TableView />
|
||||||
</Stack>
|
</Stack>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function TableView({ list }: { list: MODEL_NEW_DEFAULT_MASTER[] }) {
|
function TableView() {
|
||||||
const [data, setData] = useState(list);
|
const [data, setData] = useState<MODEL_NEW_DEFAULT_MASTER[] | null>(null);
|
||||||
|
|
||||||
const [create, setCreate] = useState("");
|
const [create, setCreate] = useState("");
|
||||||
const [isCreate, setIsCreate] = useState(false);
|
const [isCreate, setIsCreate] = useState(false);
|
||||||
|
|
||||||
@@ -67,6 +65,22 @@ function TableView({ list }: { list: MODEL_NEW_DEFAULT_MASTER[] }) {
|
|||||||
});
|
});
|
||||||
const [isChangeStatus, setIsChangeStatus] = useState(false);
|
const [isChangeStatus, setIsChangeStatus] = useState(false);
|
||||||
|
|
||||||
|
useShallowEffect(() => {
|
||||||
|
onLoadData();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
async function onLoadData() {
|
||||||
|
try {
|
||||||
|
const response = await apiGetAdminDonasiKategori();
|
||||||
|
if (response) {
|
||||||
|
console.log("ini response", response)
|
||||||
|
setData(response.data)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
clientLogger.error("Error get kategori" , error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function onCreateNewKategori() {
|
async function onCreateNewKategori() {
|
||||||
const tambahData = await adminDonasi_funCreateKategori({
|
const tambahData = await adminDonasi_funCreateKategori({
|
||||||
newKategori: create,
|
newKategori: create,
|
||||||
@@ -114,49 +128,63 @@ function TableView({ list }: { list: MODEL_NEW_DEFAULT_MASTER[] }) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const rowTable = data.map((e, i) => (
|
const renderTableBody = () => {
|
||||||
<tr key={i}>
|
if (!Array.isArray(data) || data.length === 0) {
|
||||||
<td>
|
return (
|
||||||
<Center c={AccentColor.white}>
|
<tr>
|
||||||
<Text>{e?.name}</Text>
|
<td colSpan={12}>
|
||||||
</Center>
|
<Center>
|
||||||
</td>
|
<Text color="gray">Tidak ada data</Text>
|
||||||
<td>
|
</Center>
|
||||||
<Center>
|
</td>
|
||||||
<Switch
|
</tr>
|
||||||
color="orange"
|
)
|
||||||
onLabel="ON"
|
}
|
||||||
offLabel="OFF"
|
return data.map((e, i) => (
|
||||||
checked={e?.active}
|
<tr key={i}>
|
||||||
onChange={(val) => {
|
<td>
|
||||||
const status = val.currentTarget.checked;
|
<Center c={AccentColor.white}>
|
||||||
setIsChangeStatus(true);
|
<Text>{e?.name}</Text>
|
||||||
setUpdateStatus({
|
</Center>
|
||||||
kategoriId: e?.id,
|
</td>
|
||||||
isActive: status as any,
|
<td>
|
||||||
});
|
<Center>
|
||||||
}}
|
<Switch
|
||||||
/>
|
color="orange"
|
||||||
</Center>
|
onLabel="ON"
|
||||||
</td>
|
offLabel="OFF"
|
||||||
<td>
|
checked={e?.active}
|
||||||
<Group position="center">
|
onChange={(val) => {
|
||||||
<ActionIcon
|
const status = val.currentTarget.checked;
|
||||||
onClick={() => {
|
setIsChangeStatus(true);
|
||||||
setIsUpdate(true);
|
setUpdateStatus({
|
||||||
setIsCreate(false);
|
kategoriId: e?.id,
|
||||||
setUpdateKategori({
|
isActive: status as any,
|
||||||
kategoriId: e?.id,
|
});
|
||||||
name: e?.name,
|
}}
|
||||||
});
|
/>
|
||||||
}}
|
</Center>
|
||||||
>
|
</td>
|
||||||
<IconEdit color={AdminColor.green} />
|
<td>
|
||||||
</ActionIcon>
|
<Group position="center">
|
||||||
</Group>
|
<ActionIcon
|
||||||
</td>
|
onClick={() => {
|
||||||
</tr>
|
setIsUpdate(true);
|
||||||
));
|
setIsCreate(false);
|
||||||
|
setUpdateKategori({
|
||||||
|
kategoriId: e?.id,
|
||||||
|
name: e?.name,
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<IconEdit color={AdminColor.green} />
|
||||||
|
</ActionIcon>
|
||||||
|
</Group>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@@ -167,16 +195,16 @@ function TableView({ list }: { list: MODEL_NEW_DEFAULT_MASTER[] }) {
|
|||||||
color={AdminColor.softBlue}
|
color={AdminColor.softBlue}
|
||||||
component={
|
component={
|
||||||
<Button
|
<Button
|
||||||
w={120}
|
w={120}
|
||||||
leftIcon={<IconCirclePlus />}
|
leftIcon={<IconCirclePlus />}
|
||||||
radius={"xl"}
|
radius={"xl"}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setIsCreate(true);
|
setIsCreate(true);
|
||||||
setIsUpdate(false);
|
setIsUpdate(false);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Tambah
|
Tambah
|
||||||
</Button>
|
</Button>
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
{/* <Group
|
{/* <Group
|
||||||
@@ -208,7 +236,7 @@ function TableView({ list }: { list: MODEL_NEW_DEFAULT_MASTER[] }) {
|
|||||||
horizontalSpacing={"md"}
|
horizontalSpacing={"md"}
|
||||||
p={"md"}
|
p={"md"}
|
||||||
w={"100%"}
|
w={"100%"}
|
||||||
|
|
||||||
>
|
>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
@@ -223,7 +251,7 @@ function TableView({ list }: { list: MODEL_NEW_DEFAULT_MASTER[] }) {
|
|||||||
</th>
|
</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>{rowTable}</tbody>
|
<tbody>{renderTableBody()}</tbody>
|
||||||
</Table>
|
</Table>
|
||||||
</ScrollArea>
|
</ScrollArea>
|
||||||
{/* <Center mt={"xl"}>
|
{/* <Center mt={"xl"}>
|
||||||
|
|||||||
Reference in New Issue
Block a user