feat: admin sticker

deskripsi:
- pagination di tamble utama sticker
This commit is contained in:
2025-05-16 17:45:56 +08:00
parent e7858a2812
commit 2a8c79fd09
3 changed files with 149 additions and 79 deletions

View File

@@ -1,7 +1,9 @@
import { NextResponse } from "next/server";
import { prisma } from "@/lib"; import { prisma } from "@/lib";
import { data } from "autoprefixer";
import _ from "lodash";
import { NextResponse } from "next/server";
export { POST, GET }; export { GET, POST };
interface IPostSticker { interface IPostSticker {
fileId: string; fileId: string;
@@ -49,7 +51,6 @@ async function POST(request: Request) {
} }
} }
async function GET(request: Request) { async function GET(request: Request) {
const method = request.method; const method = request.method;
if (method !== "GET") { if (method !== "GET") {
@@ -59,18 +60,66 @@ async function GET(request: Request) {
); );
} }
let fixData;
const { searchParams } = new URL(request.url);
const search = searchParams.get("search");
const page = searchParams.get("page");
const dataTake = 10
const dataSkip = Number(page) * dataTake - dataTake;
try { try {
const sticker = await prisma.sticker.findMany({ if (!page) {
orderBy: { fixData = await prisma.sticker.findMany({
createdAt: "desc", orderBy: {
}, createdAt: "desc",
include: { },
MasterEmotions: true, include: {
}, MasterEmotions: true,
}); },
});
} else {
const data = await prisma.sticker.findMany({
skip: dataSkip,
take: dataTake,
where: {
MasterEmotions: {
some: {
value: {
contains: search ? search : "",
mode: "insensitive",
},
},
},
},
orderBy: {
createdAt: "desc",
},
include: {
MasterEmotions: true,
},
});
const nCount = await prisma.sticker.count({
where: {
MasterEmotions: {
some: {
value: {
contains: search ? search : "",
mode: "insensitive",
},
},
},
},
});
fixData = {
data: data,
nPage: _.ceil(nCount / dataTake),
};
}
return NextResponse.json( return NextResponse.json(
{ success: true, message: "Success get data sticker", data: sticker }, { success: true, message: "Success get data sticker", data: fixData },
{ status: 200 } { status: 200 }
); );
} catch (error) { } catch (error) {

View File

@@ -32,7 +32,7 @@ export const apiAdminCreateSticker = async ({ data }: { data: any }) => {
} }
}; };
export const apiAdminGetSticker = async () => { export const apiAdminGetSticker = async ({ page }: { page: number }) => {
try { try {
// Fetch token from cookie // Fetch token from cookie
const { token } = await fetch("/api/get-cookie").then((res) => res.json()); const { token } = await fetch("/api/get-cookie").then((res) => res.json());
@@ -41,7 +41,7 @@ export const apiAdminGetSticker = async () => {
return null; return null;
} }
const response = await fetch(`/api/sticker`, { const response = await fetch(`/api/sticker?page=${page}`, {
method: "GET", method: "GET",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",

View File

@@ -22,9 +22,10 @@ import {
Switch, Switch,
Table, Table,
Text, Text,
TextInput,
} from "@mantine/core"; } from "@mantine/core";
import { useShallowEffect } from "@mantine/hooks"; import { useShallowEffect } from "@mantine/hooks";
import { IconPencil, IconPlus } from "@tabler/icons-react"; import { IconFilter, IconPencil, IconPlus } from "@tabler/icons-react";
import { AppRouterInstance } from "next/dist/shared/lib/app-router-context.shared-runtime"; import { AppRouterInstance } from "next/dist/shared/lib/app-router-context.shared-runtime";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
import { useState } from "react"; import { useState } from "react";
@@ -37,6 +38,7 @@ import {
apiAdminGetSticker, apiAdminGetSticker,
apiAdminUpdateStatusStickerById, apiAdminUpdateStatusStickerById,
} from "../lib/api_fetch_stiker"; } from "../lib/api_fetch_stiker";
import { Admin_V3_ComponentPaginationBreakpoint } from "../../_components_v3/comp_pagination_breakpoint";
export default function AdminAppInformation_ViewSticker() { export default function AdminAppInformation_ViewSticker() {
const router = useRouter(); const router = useRouter();
@@ -49,21 +51,31 @@ export default function AdminAppInformation_ViewSticker() {
}); });
const [loadingUpdate, setLoadingUpdate] = useState(false); const [loadingUpdate, setLoadingUpdate] = useState(false);
const [opened, setOpened] = useState(false); const [opened, setOpened] = useState(false);
const [nPage, setNPage] = useState(1);
const [activePage, setActivePage] = useState(1);
useShallowEffect(() => { useShallowEffect(() => {
const fetchData = async () => { handleLoadData();
try { }, [activePage]);
const response = await apiAdminGetSticker();
if (response.success) {
setDataSticker(response.data);
}
} catch (error) {
console.error("Error fetching data", error);
}
};
fetchData(); const handleLoadData = async () => {
}, []); try {
const response = await apiAdminGetSticker({ page: activePage });
if (response.success) {
setDataSticker(response.data.data);
setNPage(response.data.nPage || 1);
} else {
setDataSticker([]);
}
} catch (error) {
console.error("Error fetching data", error);
setDataSticker([]);
}
};
const onPageClick = (page: number) => {
setActivePage(page);
};
const handleUpdateActivation = async ({ const handleUpdateActivation = async ({
id, id,
@@ -77,7 +89,6 @@ export default function AdminAppInformation_ViewSticker() {
isActive: value, isActive: value,
}; };
try { try {
setLoadingUpdate(true); setLoadingUpdate(true);
const updt = await apiAdminUpdateStatusStickerById({ const updt = await apiAdminUpdateStatusStickerById({
@@ -108,63 +119,73 @@ export default function AdminAppInformation_ViewSticker() {
<Stack> <Stack>
<ComponentAdminGlobal_TitlePage name="Stiker " /> <ComponentAdminGlobal_TitlePage name="Stiker " />
<Button <Group position="right">
loading={loadingCreate} {/* <Button radius={"xl"} leftIcon={<IconFilter size={20} />}> Filter</Button> */}
loaderPosition="center" <Button
w={120} loading={loadingCreate}
radius={"xl"} loaderPosition="center"
leftIcon={<IconPlus size={20} />} w={120}
onClick={() => { radius={"xl"}
router.push(RouterAdminAppInformation.createSticker); leftIcon={<IconPlus size={20} />}
setLoadingCreate(true); onClick={() => {
}} router.push(RouterAdminAppInformation.createSticker);
> setLoadingCreate(true);
Tambah }}
</Button> >
Tambah
</Button>
</Group>
{!dataSticker ? ( {!dataSticker ? (
<CustomSkeleton height={"65dvh"} /> <CustomSkeleton height={"65dvh"} />
) : ( ) : (
<Admin_ComponentBoxStyle <Admin_ComponentBoxStyle
style={{ height: "65dvh", overflow: "hidden" }} // style={{ height: "65dvh", overflow: "hidden" }}
> >
<ScrollArea w={"100%"} h={"100%"} scrollbarSize={"md"}> <Stack>
<Table <ScrollArea w={"100%"} scrollbarSize={"md"} h={"65dvh"}>
verticalSpacing={"md"} <Table
horizontalSpacing={"md"} verticalSpacing={"md"}
p={"md"} horizontalSpacing={"md"}
w={"100%"} p={"md"}
> w={"100%"}
<thead> >
<tr> <thead>
<th> <tr>
<Center c={AdminColor.white}>Aksi</Center> <th>
</th> <Center c={AdminColor.white}>Aksi</Center>
<th> </th>
<Center c={AdminColor.white}>Status</Center> <th>
</th> <Center c={AdminColor.white}>Status</Center>
</th>
<th> <th>
<Center c={AdminColor.white}>Stiker</Center> <Center c={AdminColor.white}>Stiker</Center>
</th> </th>
<th> <th>
<Text c={AdminColor.white}>Kategori</Text> <Text c={AdminColor.white}>Kategori</Text>
</th> </th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{rowTable({ {rowTable({
dataSticker, dataSticker,
router, router,
loadingDetail, loadingDetail,
setLoadingDetail, setLoadingDetail,
setOpened, setOpened,
dataUpdate, dataUpdate,
setDataUpdate, setDataUpdate,
})} })}
</tbody> </tbody>
</Table> </Table>
</ScrollArea> </ScrollArea>
<Admin_V3_ComponentPaginationBreakpoint
value={activePage}
total={nPage}
onChange={onPageClick}
/>
</Stack>
</Admin_ComponentBoxStyle> </Admin_ComponentBoxStyle>
)} )}
</Stack> </Stack>
@@ -296,7 +317,7 @@ const rowTable = ({
</Center> </Center>
</td> </td>
<td> <td>
<Box maw={300}> <Box maw={300} miw={200}>
<Spoiler maxHeight={70} hideLabel="Sembunyikan" showLabel="Tampilkan"> <Spoiler maxHeight={70} hideLabel="Sembunyikan" showLabel="Tampilkan">
<Group> <Group>
{e.MasterEmotions.map((e) => ( {e.MasterEmotions.map((e) => (