Merge pull request #160 from bipproduction/lukman/28-agustus-2024
Lukman/28 agustus 2024
This commit is contained in:
103
src/app/api/home/search/route.ts
Normal file
103
src/app/api/home/search/route.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
|
||||
|
||||
// SEARCH USER, DIVISION, PROJECT
|
||||
|
||||
import { prisma } from "@/module/_global";
|
||||
import { funGetUserByCookies } from "@/module/auth";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const search = searchParams.get("search");
|
||||
const userId = await funGetUserByCookies()
|
||||
if (userId.id == undefined) {
|
||||
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
||||
}
|
||||
|
||||
let kondisi: any, kondisiProject: any
|
||||
|
||||
// klo perbekel == semua grup
|
||||
if (userId.idUserRole == "supadmin") {
|
||||
kondisi = {
|
||||
isActive: true,
|
||||
idVillage: userId.idVillage,
|
||||
Group: {
|
||||
isActive: true,
|
||||
},
|
||||
name: {
|
||||
contains: (search == undefined || search == null) ? "" : search,
|
||||
mode: "insensitive"
|
||||
}
|
||||
}
|
||||
|
||||
kondisiProject = {
|
||||
isActive: true,
|
||||
idVillage: userId.idVillage,
|
||||
Group: {
|
||||
isActive: true,
|
||||
},
|
||||
title: {
|
||||
contains: (search == undefined || search == null) ? "" : search,
|
||||
mode: "insensitive"
|
||||
}
|
||||
}
|
||||
} else {
|
||||
kondisi = {
|
||||
idVillage: userId.idVillage,
|
||||
isActive: true,
|
||||
idGroup: userId.idGroup,
|
||||
name: {
|
||||
contains: (search == undefined || search == null) ? "" : search,
|
||||
mode: "insensitive"
|
||||
}
|
||||
}
|
||||
|
||||
kondisiProject = {
|
||||
idVillage: userId.idVillage,
|
||||
isActive: true,
|
||||
idGroup: userId.idGroup,
|
||||
title: {
|
||||
contains: (search == undefined || search == null) ? "" : search,
|
||||
mode: "insensitive"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const user = await prisma.user.findMany({
|
||||
where: kondisi,
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true
|
||||
}
|
||||
})
|
||||
|
||||
const divisions = await prisma.division.findMany({
|
||||
where: kondisi,
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
desc: true
|
||||
}
|
||||
})
|
||||
|
||||
const projects = await prisma.project.findMany({
|
||||
where: kondisiProject,
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
}
|
||||
})
|
||||
|
||||
const allDataSearch = {
|
||||
user: user,
|
||||
division: divisions,
|
||||
project: projects
|
||||
}
|
||||
return NextResponse.json({ success: true, data: allDataSearch }, { status: 200 });
|
||||
|
||||
} catch (error) {
|
||||
return NextResponse.json({ success: false, message: error }, { status: 500 });
|
||||
}
|
||||
}
|
||||
6
src/module/home/lib/api_search.ts
Normal file
6
src/module/home/lib/api_search.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
|
||||
export const funGetSearchAll = async (path?: string) => {
|
||||
const response = await fetch(`/api/home/search${(path) ? path : ''}`, { next: { tags: ['search'] } });
|
||||
return await response.json().catch(() => null);
|
||||
}
|
||||
0
src/module/home/lib/type_search.ts
Normal file
0
src/module/home/lib/type_search.ts
Normal file
@@ -1,9 +1,33 @@
|
||||
"use client"
|
||||
import { LayoutNavbarNew, WARNA } from '@/module/_global';
|
||||
import { Box, TextInput } from '@mantine/core';
|
||||
import React from 'react';
|
||||
import React, { useState } from 'react';
|
||||
import { HiMagnifyingGlass } from 'react-icons/hi2';
|
||||
import { funGetSearchAll } from '../lib/api_search';
|
||||
import { useShallowEffect } from '@mantine/hooks';
|
||||
|
||||
export default function ViewSearch() {
|
||||
const [search, setSearch] = useState('');
|
||||
const [dataUser, setDataUser] = useState([]);
|
||||
const [dataProject, setDataProject] = useState([]);
|
||||
const [dataDivision, setDataDivision] = useState([]);
|
||||
|
||||
async function featchSearch() {
|
||||
try {
|
||||
const res = await funGetSearchAll('?search=' + search);
|
||||
setDataUser(res.data.user);
|
||||
setDataProject(res.data.project);
|
||||
setDataDivision(res.data.division);
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
throw new Error("Error")
|
||||
}
|
||||
}
|
||||
|
||||
useShallowEffect(() => {
|
||||
featchSearch()
|
||||
}, [search])
|
||||
|
||||
return (
|
||||
<>
|
||||
<LayoutNavbarNew back='/home' title='Pencarian' menu={<></>} />
|
||||
@@ -20,7 +44,12 @@ export default function ViewSearch() {
|
||||
radius={30}
|
||||
leftSection={<HiMagnifyingGlass size={20} />}
|
||||
placeholder="Pencarian"
|
||||
onChange={(e) => setSearch(e.target.value)}
|
||||
/>
|
||||
{/* <pre>{JSON.stringify(dataUser, null, 1)}</pre>
|
||||
<pre>{JSON.stringify(dataProject, null, 1)}</pre>
|
||||
<pre>{JSON.stringify(dataDivision, null, 1)}</pre> */}
|
||||
|
||||
</Box>
|
||||
</>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user