upd: division

Deskripsi:
- update load pagination

No Issues
This commit is contained in:
amel
2024-09-19 12:12:54 +08:00
parent fe5155ee37
commit c4da324ad5
3 changed files with 53 additions and 13 deletions

View File

@@ -20,6 +20,8 @@ export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const idGroup = searchParams.get("group");
const name = searchParams.get('search');
const page = searchParams.get('page');
const dataSkip = Number(page) * 10 - 10;
if (idGroup == "null" || idGroup == undefined) {
grup = user.idGroup
@@ -56,7 +58,13 @@ export async function GET(request: Request) {
}
}
const totalData = await prisma.division.count({
where: kondisi
})
const data = await prisma.division.findMany({
skip: dataSkip,
take: 10,
where: kondisi,
select: {
id: true,
@@ -90,7 +98,7 @@ export async function GET(request: Request) {
})
return NextResponse.json({ success: true, message: "Berhasil mendapatkan divisi", data: allData, filter }, { status: 200 });
return NextResponse.json({ success: true, message: "Berhasil mendapatkan divisi", data: allData, total: totalData, filter }, { status: 200 });
} catch (error) {
console.error(error);

View File

@@ -31,13 +31,13 @@ export default function ListAnnouncement() {
setLoading(true)
const response = await funGetAllAnnouncement('?search=' + searchQuery + '&page=' + isPage)
if (response.success) {
if (response.data.length > 0) {
// if (response.data.length > 0) {
if (isPage == 1) {
setIsData(response?.data)
} else {
setIsData([...isData, ...response?.data])
}
}
// }
} else {
toast.error(response.message);
}

View File

@@ -1,8 +1,8 @@
'use client'
import { globalRole, LayoutDrawer, LayoutNavbarNew, SkeletonSingle, TEMA } from '@/module/_global';
import { currentScroll, globalRole, LayoutDrawer, LayoutNavbarNew, SkeletonSingle, TEMA } from '@/module/_global';
import { ActionIcon, Avatar, Box, Card, Center, Divider, Flex, Grid, Group, Skeleton, Text, TextInput, Title } from '@mantine/core';
import { useRouter, useSearchParams } from 'next/navigation';
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';
import { HiMenu } from 'react-icons/hi';
import { HiMagnifyingGlass, HiMiniUserGroup, HiOutlineListBullet, HiSquares2X2 } from 'react-icons/hi2';
import { MdAccountCircle } from 'react-icons/md';
@@ -26,6 +26,8 @@ export default function ListDivision() {
const [nameGroup, setNameGroup] = useState('')
const roleLogin = useHookstate(globalRole)
const tema = useHookstate(TEMA)
const { value: containerRef } = useHookstate(currentScroll);
const [isPage, setPage] = useState(1)
const paddingLift = useMediaQuery('(max-width: 505px)')
@@ -34,15 +36,19 @@ export default function ListDivision() {
setIsList(!isList)
}
const fetchData = async (search: string) => {
const fetchData = async (loading: boolean) => {
try {
setData([]);
setLoading(true);
const response = await funGetAllDivision('?search=' + search + '&group=' + group)
if (loading)
setLoading(true);
const response = await funGetAllDivision('?search=' + searchQuery + '&group=' + group + '&page=' + isPage)
if (response.success) {
setData(response.data)
setJumlah(response.data.length)
setJumlah(response.total)
setNameGroup(response.filter.name)
if (isPage == 1) {
setData(response.data)
}else{
setData([...data, ...response.data])
}
} else {
toast.error(response.message);
}
@@ -57,14 +63,40 @@ export default function ListDivision() {
function searchDivision(search: string) {
setSearchQuery(search)
fetchData(search)
setPage(1)
}
useShallowEffect(() => {
fetchData(searchQuery)
fetchData(true)
}, [searchQuery])
useShallowEffect(() => {
fetchData(false)
}, [isPage])
useEffect(() => {
const handleScroll = async () => {
if (containerRef && containerRef.current) {
const scrollTop = containerRef.current.scrollTop;
const containerHeight = containerRef.current.clientHeight;
const scrollHeight = containerRef.current.scrollHeight;
if (scrollTop + containerHeight >= scrollHeight) {
setPage(isPage + 1)
}
}
};
const container = containerRef?.current;
container?.addEventListener("scroll", handleScroll);
return () => {
container?.removeEventListener("scroll", handleScroll);
};
}, [containerRef, isPage]);
return (
<Box>
<LayoutNavbarNew back='/home' title='Divisi'