upd: user
Deskripsi: - update scroll load page No Issues
This commit is contained in:
@@ -15,7 +15,7 @@ export async function GET(request: Request) {
|
|||||||
const idGroup = searchParams.get("group");
|
const idGroup = searchParams.get("group");
|
||||||
const active = searchParams.get("active");
|
const active = searchParams.get("active");
|
||||||
const page = searchParams.get('page');
|
const page = searchParams.get('page');
|
||||||
const dataSkip = Number(page) * 5 - 5;
|
const dataSkip = Number(page) * 10 - 10;
|
||||||
const user = await funGetUserByCookies()
|
const user = await funGetUserByCookies()
|
||||||
if (user.id == undefined) {
|
if (user.id == undefined) {
|
||||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
||||||
@@ -37,9 +37,10 @@ export async function GET(request: Request) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
if (page != undefined) {
|
||||||
const users = await prisma.user.findMany({
|
const users = await prisma.user.findMany({
|
||||||
skip: dataSkip,
|
skip: dataSkip,
|
||||||
take: 5,
|
take: 10,
|
||||||
where: {
|
where: {
|
||||||
isActive: active == 'false' ? false : true,
|
isActive: active == 'false' ? false : true,
|
||||||
idGroup: String(fixGroup),
|
idGroup: String(fixGroup),
|
||||||
@@ -77,6 +78,48 @@ export async function GET(request: Request) {
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
return NextResponse.json({ success: true, message: "Berhasil member", data: allData, filter }, { status: 200 });
|
return NextResponse.json({ success: true, message: "Berhasil member", data: allData, filter }, { status: 200 });
|
||||||
|
} else {
|
||||||
|
const users = await prisma.user.findMany({
|
||||||
|
where: {
|
||||||
|
isActive: active == 'false' ? false : true,
|
||||||
|
idGroup: String(fixGroup),
|
||||||
|
name: {
|
||||||
|
contains: (name == undefined || name == null) ? "" : name,
|
||||||
|
mode: "insensitive",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
isActive: true,
|
||||||
|
nik: true,
|
||||||
|
name: true,
|
||||||
|
phone: true,
|
||||||
|
email: true,
|
||||||
|
gender: true,
|
||||||
|
img: true,
|
||||||
|
Position: {
|
||||||
|
select: {
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Group: {
|
||||||
|
select: {
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const allData = users.map((v: any) => ({
|
||||||
|
..._.omit(v, ["Group", "Position"]),
|
||||||
|
group: v.Group.name,
|
||||||
|
position: v.Position.name
|
||||||
|
}))
|
||||||
|
|
||||||
|
return NextResponse.json({ success: true, message: "Berhasil member", data: allData, filter }, { status: 200 });
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return NextResponse.json({ success: false, message: "Gagal mendapatkan member, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
return NextResponse.json({ success: false, message: "Gagal mendapatkan member, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
|
import { currentScroll, globalRole, SkeletonSingle, TEMA } from "@/module/_global"
|
||||||
import { currentScroll, globalRole, SkeletonSingle, TEMA, WARNA } from "@/module/_global"
|
|
||||||
import { Box, Text, TextInput, Divider, Avatar, Grid } from "@mantine/core"
|
import { Box, Text, TextInput, Divider, Avatar, Grid } from "@mantine/core"
|
||||||
import { useShallowEffect } from "@mantine/hooks"
|
import { useShallowEffect } from "@mantine/hooks"
|
||||||
import { useRouter, useSearchParams } from "next/navigation"
|
import { useRouter, useSearchParams } from "next/navigation"
|
||||||
@@ -7,7 +6,6 @@ import { useEffect, useState } from "react"
|
|||||||
import { HiMagnifyingGlass } from "react-icons/hi2"
|
import { HiMagnifyingGlass } from "react-icons/hi2"
|
||||||
import { IListMember } from "../lib/type_member"
|
import { IListMember } from "../lib/type_member"
|
||||||
import { funGetAllmember } from "../lib/api_member"
|
import { funGetAllmember } from "../lib/api_member"
|
||||||
import { funGetAllGroup, IDataGroup } from "@/module/group"
|
|
||||||
import toast from "react-hot-toast"
|
import toast from "react-hot-toast"
|
||||||
import _ from "lodash"
|
import _ from "lodash"
|
||||||
import { useHookstate } from "@hookstate/core"
|
import { useHookstate } from "@hookstate/core"
|
||||||
@@ -24,23 +22,21 @@ export default function TabListMember() {
|
|||||||
const roleLogin = useHookstate(globalRole)
|
const roleLogin = useHookstate(globalRole)
|
||||||
const [nameGroup, setNameGroup] = useState('')
|
const [nameGroup, setNameGroup] = useState('')
|
||||||
const tema = useHookstate(TEMA)
|
const tema = useHookstate(TEMA)
|
||||||
|
|
||||||
//scroll
|
|
||||||
const { value: containerRef } = useHookstate(currentScroll);
|
const { value: containerRef } = useHookstate(currentScroll);
|
||||||
const [isPage, setPage] = useState(1)
|
const [isPage, setPage] = useState(1)
|
||||||
|
|
||||||
|
|
||||||
async function getAllUser(loading: boolean) {
|
async function getAllUser(loading: boolean) {
|
||||||
try {
|
try {
|
||||||
|
if (loading)
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
const res = await funGetAllmember('?active=' + status + '&group=' + group + '&search=' + searchQuery + '&page=' + isPage)
|
const res = await funGetAllmember('?active=' + status + '&group=' + group + '&search=' + searchQuery + '&page=' + isPage)
|
||||||
if (res.success) {
|
if (res.success) {
|
||||||
|
setNameGroup(res.filter.name)
|
||||||
if (isPage == 1) {
|
if (isPage == 1) {
|
||||||
setDataMember(res.data)
|
setDataMember(res.data)
|
||||||
setNameGroup(res.filter.name)
|
|
||||||
} else {
|
} else {
|
||||||
setDataMember([...dataMember, ...res.data])
|
setDataMember([...dataMember, ...res.data])
|
||||||
setNameGroup(res.filter.name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@@ -48,24 +44,20 @@ export default function TabListMember() {
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
throw new Error("Error")
|
toast.error("Gagal memuat data, coba lagi nanti")
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function onSearch(val:string){
|
|
||||||
setSearchQuery(val)
|
|
||||||
setPage(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
useShallowEffect(() => {
|
useShallowEffect(() => {
|
||||||
|
setPage(1)
|
||||||
getAllUser(true)
|
getAllUser(true)
|
||||||
}, [status, searchQuery])
|
}, [status, searchQuery])
|
||||||
|
|
||||||
useShallowEffect(() => {
|
useShallowEffect(() => {
|
||||||
getAllUser(false)
|
getAllUser(false)
|
||||||
}, [status, isPage])
|
}, [isPage])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleScroll = async () => {
|
const handleScroll = async () => {
|
||||||
@@ -103,7 +95,7 @@ export default function TabListMember() {
|
|||||||
radius={30}
|
radius={30}
|
||||||
leftSection={<HiMagnifyingGlass size={20} />}
|
leftSection={<HiMagnifyingGlass size={20} />}
|
||||||
placeholder="Pencarian"
|
placeholder="Pencarian"
|
||||||
onChange={(e) => onSearch(e.target.value)}
|
onChange={(e) => setSearchQuery(e.target.value)}
|
||||||
my={10}
|
my={10}
|
||||||
/>
|
/>
|
||||||
{loading
|
{loading
|
||||||
|
|||||||
Reference in New Issue
Block a user