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 { searchParams } = new URL(request.url);
const idGroup = searchParams.get("group"); const idGroup = searchParams.get("group");
const name = searchParams.get('search'); const name = searchParams.get('search');
const page = searchParams.get('page');
const dataSkip = Number(page) * 10 - 10;
if (idGroup == "null" || idGroup == undefined) { if (idGroup == "null" || idGroup == undefined) {
grup = user.idGroup 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({ const data = await prisma.division.findMany({
skip: dataSkip,
take: 10,
where: kondisi, where: kondisi,
select: { select: {
id: true, 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) { } catch (error) {
console.error(error); console.error(error);

View File

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

View File

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