feat : update history

This commit is contained in:
lukman
2024-08-20 14:46:47 +08:00
parent f090af8f7d
commit 6076de2051
4 changed files with 53 additions and 23 deletions

View File

@@ -15,6 +15,7 @@ export async function GET(request: Request) {
const { searchParams } = new URL(request.url); const { searchParams } = new URL(request.url);
const idDivision = searchParams.get("division"); const idDivision = searchParams.get("division");
const name = searchParams.get('search');
if (idDivision != "null" && idDivision != null && idDivision != undefined) { if (idDivision != "null" && idDivision != null && idDivision != undefined) {
const cekDivision = await prisma.division.count({ const cekDivision = await prisma.division.count({
@@ -32,6 +33,10 @@ export async function GET(request: Request) {
where: { where: {
isActive: true, isActive: true,
idDivision: idDivision, idDivision: idDivision,
title: {
contains: (name == undefined || name == "null") ? "" : name,
mode: "insensitive"
}
}, },
select: { select: {
id: true, id: true,
@@ -54,21 +59,28 @@ export async function GET(request: Request) {
}); });
const allOmit = data.map((v: any) => ({ const allOmit = data.map((v: any) => ({
..._.omit(v, ["title", "timeStart", "timeEnd", "id", ]), ..._.omit(v, [""]),
dateStart: v.dateStart, dateStart: v.dateStart,
data: [
{
id: v.id,
title: v.title,
timeEnd: moment.utc(v.timeEnd).format('HH:mm'),
timeStart: moment.utc(v.timeStart).format('HH:mm')
}
]
})) }))
// groupBy untuk dateStart
const groupByDateStart = _.groupBy(allOmit, 'dateStart');
const result = Object.keys(groupByDateStart).map(key => {
const obj = groupByDateStart[key];
const data = obj.map((v: any) => ({
id: v.id,
title: v.title,
timeEnd: moment.utc(v.timeEnd).format('HH:mm'),
timeStart: moment.utc(v.timeStart).format('HH:mm')
}))
return {
dateStart: key,
data: data
}
})
return NextResponse.json({ success: true, message: "Berhasil mendapatkan calender", data: allOmit }, { status: 200 }); return NextResponse.json({ success: true, message: "Berhasil mendapatkan calender", data: result }, { status: 200 });
} else { } else {
return NextResponse.json({ success: false, message: "Gagal mendapatkan calender, data tidak ditemukan" }, { status: 404 }); return NextResponse.json({ success: false, message: "Gagal mendapatkan calender, data tidak ditemukan" }, { status: 404 });

View File

@@ -16,6 +16,8 @@ export async function GET(request: Request) {
const { searchParams } = new URL(request.url); const { searchParams } = new URL(request.url);
const idDivision = searchParams.get("division"); const idDivision = searchParams.get("division");
const isDate = searchParams.get("date")
if (idDivision != "null" && idDivision != null && idDivision != undefined) { if (idDivision != "null" && idDivision != null && idDivision != undefined) {
const cekDivision = await prisma.division.count({ const cekDivision = await prisma.division.count({
@@ -33,6 +35,7 @@ export async function GET(request: Request) {
where: { where: {
isActive: true, isActive: true,
idDivision: idDivision, idDivision: idDivision,
dateStart: new Date(String(isDate))
}, },
select: { select: {
id: true, id: true,

View File

@@ -4,26 +4,36 @@ import { DatePicker, DatePickerProps } from '@mantine/dates';
import { useParams, useRouter } from 'next/navigation'; import { useParams, useRouter } from 'next/navigation';
import React, { useState } from 'react'; import React, { useState } from 'react';
import { funGetAllCalender } from '../lib/api_calender'; import { funGetAllCalender } from '../lib/api_calender';
import { useShallowEffect } from '@mantine/hooks'; import { useSetState, useShallowEffect } from '@mantine/hooks';
import { IDataCalender } from '../lib/type_calender'; import { IDataCalender } from '../lib/type_calender';
import moment from 'moment';
export default function DateEventDivision() { export default function DateEventDivision() {
const[isData, setData] = useState<IDataCalender[]>([]) const [isData, setData] = useState<IDataCalender[]>([])
const router = useRouter() const router = useRouter()
const param = useParams<{ id: string, detail: string }>() const param = useParams<{ id: string, detail: string }>()
const [isDate, setDate] = useSetState<any>(moment().format('YYYY-MM-DD'))
const getData = async () => { const getData = async (tgl: any) => {
try { try {
const response = await funGetAllCalender('?division=' + param.id) const response = await funGetAllCalender('?division=' + param.id + '&date=' + tgl)
setData(response.data) setData(response.data)
} catch (error) { } catch (error) {
console.log(error) console.log(error)
} }
} }
function change(val: Date) {
const a: any = moment(new Date(val)).format('YYYY-MM-DD')
console.log(val, a)
setDate(a)
getData(a)
}
useShallowEffect(() => { useShallowEffect(() => {
getData() getData(isDate)
}, []) }, [])
@@ -43,10 +53,12 @@ export default function DateEventDivision() {
py={20} py={20}
style={{ borderRadius: 10, border: `1px solid ${"#D6D8F6"}` }} style={{ borderRadius: 10, border: `1px solid ${"#D6D8F6"}` }}
> >
<DatePicker size='md' defaultValue={new Date()} renderDay={dayRenderer} /> <DatePicker size='md' defaultValue={new Date()} renderDay={dayRenderer}
onChange={(val: any) => { change(val) }}
/>
</Group> </Group>
<Box> <Box>
<Text mb={10} mt={20} fw={"bold"}>Hari Ini</Text> <Text mb={10} mt={20} fw={"bold"}>Hari Ini</Text>
{isData.map((event, index) => { {isData.map((event, index) => {
const bgColor = ['#D8D8F1', '#FED6C5'][index % 2] const bgColor = ['#D8D8F1', '#FED6C5'][index % 2]
const colorDivider = ['#535FCA', '#A7A7A7'][index % 2] const colorDivider = ['#535FCA', '#A7A7A7'][index % 2]

View File

@@ -58,10 +58,11 @@ export default function HistoryDivisionCalender() {
const [isData, setData] = useState<IHistoryCalender[]>([]) const [isData, setData] = useState<IHistoryCalender[]>([])
const router = useRouter() const router = useRouter()
const param = useParams<{ id: string, detail: string }>() const param = useParams<{ id: string, detail: string }>()
const [searchQuery, setSearchQuery] = useState('')
const getData = async () => { const getData = async () => {
try { try {
const response = await funGetHostory('?division=' + param.id) const response = await funGetHostory('?division=' + param.id + '&search=' + searchQuery)
setData(response.data) setData(response.data)
} catch (error) { } catch (error) {
console.log(error) console.log(error)
@@ -70,11 +71,10 @@ export default function HistoryDivisionCalender() {
useShallowEffect(() => { useShallowEffect(() => {
getData() getData()
}, []) }, [searchQuery])
return ( return (
<Box> <Box>
<LayoutNavbarNew back="/calender" title="History kalender" menu /> <LayoutNavbarNew back="/calender" title="Riwayat kalender" menu />
<pre>{JSON.stringify(isData, null, 1)}</pre>
<Box p={20}> <Box p={20}>
<TextInput <TextInput
styles={{ styles={{
@@ -88,10 +88,13 @@ export default function HistoryDivisionCalender() {
radius={30} radius={30}
leftSection={<HiMagnifyingGlass size={20} />} leftSection={<HiMagnifyingGlass size={20} />}
placeholder="Pencarian" placeholder="Pencarian"
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
/> />
<Box mt={30}> <Box mt={30}>
<Box bg={"#DBE9D8"} style={{ <Box bg={"#DBE9D8"} style={{
borderRadius: 10 borderRadius: 10,
padding: 20
}}> }}>
{isData.map((v, i) => { {isData.map((v, i) => {
return ( return (