Update Schedule page (Status color, search bar)
This commit is contained in:
@@ -1,6 +1,10 @@
|
|||||||
import {
|
import {
|
||||||
|
Button,
|
||||||
Card,
|
Card,
|
||||||
CardBody,
|
CardBody,
|
||||||
|
Chip,
|
||||||
|
ChipProps,
|
||||||
|
Input,
|
||||||
Table,
|
Table,
|
||||||
TableBody,
|
TableBody,
|
||||||
TableCell,
|
TableCell,
|
||||||
@@ -12,6 +16,8 @@ import DefaultLayout from "../layouts/default";
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import config from "../config";
|
import config from "../config";
|
||||||
import instance from "../security/http";
|
import instance from "../security/http";
|
||||||
|
import { MagnifyingGlassIcon, XMarkIcon } from "../icons";
|
||||||
|
import React from "react";
|
||||||
|
|
||||||
interface Schedule {
|
interface Schedule {
|
||||||
id: number;
|
id: number;
|
||||||
@@ -21,28 +27,99 @@ interface Schedule {
|
|||||||
status: string;
|
status: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const statusColorMap: Record<string, ChipProps["color"]> = {
|
||||||
|
"On going": "success",
|
||||||
|
"Up coming": "danger",
|
||||||
|
"Ended": "default",
|
||||||
|
};
|
||||||
|
|
||||||
|
const getStatusColor = (status: string): ChipProps["color"] => {
|
||||||
|
return statusColorMap[status] || "default";
|
||||||
|
};
|
||||||
|
|
||||||
export default function SchedulePage() {
|
export default function SchedulePage() {
|
||||||
const [scheduleList, setScheduleList] = useState<Schedule[]>([]);
|
const [scheduleList, setScheduleList] = useState<Schedule[]>([]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
getSchedule();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const getSchedule = () => {
|
||||||
instance
|
instance
|
||||||
.get(config.serverAddress + "/schedule")
|
.get(config.serverAddress + "/schedule")
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
const schedules = res.data.map((schedule: Schedule) => ({
|
const schedules = res.data.map((schedule: Schedule) => ({
|
||||||
...schedule,
|
...schedule,
|
||||||
dateTime: new Date(schedule.dateTime), // Convert to Date object
|
dateTime: new Date(schedule.dateTime), // Ensure dateTime is a Date object
|
||||||
}));
|
}));
|
||||||
setScheduleList(schedules);
|
setScheduleList(schedules);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error("Error fetching schedules:", err);
|
console.error("Error fetching schedules:", err);
|
||||||
});
|
});
|
||||||
}, []);
|
};
|
||||||
|
|
||||||
|
// search
|
||||||
|
const [search, setSearch] = useState("");
|
||||||
|
const onSearchChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
|
setSearch(e.target.value);
|
||||||
|
};
|
||||||
|
|
||||||
|
const onSearchKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
|
||||||
|
if (e.key === "Enter") {
|
||||||
|
searchSchedule();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const onClickSearch = () => {
|
||||||
|
searchSchedule();
|
||||||
|
};
|
||||||
|
|
||||||
|
const onClickClear = () => {
|
||||||
|
setSearch("");
|
||||||
|
getSchedule();
|
||||||
|
};
|
||||||
|
|
||||||
|
const searchSchedule = () => {
|
||||||
|
console.log(`Searching for: ${search}`);
|
||||||
|
instance
|
||||||
|
.get(`${config.serverAddress}/schedule?search=${search}`)
|
||||||
|
.then((res) => {
|
||||||
|
const schedules = res.data.map((schedule: Schedule) => ({
|
||||||
|
...schedule,
|
||||||
|
dateTime: new Date(schedule.dateTime), // Ensure dateTime is a Date object
|
||||||
|
}));
|
||||||
|
setScheduleList(schedules);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error("Error fetching search results:", err);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DefaultLayout>
|
<DefaultLayout>
|
||||||
<section className="flex flex-col items-center justify-center gap-4 py-8 md:py-10">
|
<section className="flex flex-col items-center justify-center gap-4 py-8 md:py-10">
|
||||||
<h1>Karang Guni Schedule</h1>
|
<h1>Karang Guni Schedule</h1>
|
||||||
|
<div className="flex gap-4">
|
||||||
|
{/* Search Input */}
|
||||||
|
<Input
|
||||||
|
value={search}
|
||||||
|
className="w-[400px]"
|
||||||
|
placeholder="Search Address/Postal"
|
||||||
|
onChange={onSearchChange}
|
||||||
|
onKeyDown={onSearchKeyDown}
|
||||||
|
endContent={
|
||||||
|
<div className="flex flex-row -mr-3">
|
||||||
|
<Button isIconOnly variant="light" onPress={onClickSearch}>
|
||||||
|
<MagnifyingGlassIcon />
|
||||||
|
</Button>
|
||||||
|
<Button isIconOnly variant="light" onPress={onClickClear}>
|
||||||
|
<XMarkIcon />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
<div className="flex flex-col gap-8">
|
<div className="flex flex-col gap-8">
|
||||||
<Table aria-label="Schedule Table">
|
<Table aria-label="Schedule Table">
|
||||||
<TableHeader>
|
<TableHeader>
|
||||||
@@ -68,7 +145,11 @@ export default function SchedulePage() {
|
|||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell>{schedule.location}</TableCell>
|
<TableCell>{schedule.location}</TableCell>
|
||||||
<TableCell>{schedule.postalCode}</TableCell>
|
<TableCell>{schedule.postalCode}</TableCell>
|
||||||
<TableCell>{schedule.status}</TableCell>
|
<TableCell>
|
||||||
|
<Chip color={getStatusColor(schedule.status)}>
|
||||||
|
{schedule.status}
|
||||||
|
</Chip>
|
||||||
|
</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
))}
|
))}
|
||||||
</TableBody>
|
</TableBody>
|
||||||
|
|||||||
Reference in New Issue
Block a user