Fixed fetching data schedules page
This commit is contained in:
5572
client/pnpm-lock.yaml
generated
5572
client/pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -3,8 +3,8 @@ import DefaultLayout from "../layouts/default";
|
|||||||
import { SetStateAction, useEffect, useState } from 'react';
|
import { SetStateAction, useEffect, useState } from 'react';
|
||||||
import { Button, Avatar, Link, Dropdown, DropdownTrigger, DropdownMenu, DropdownItem, Input } from "@nextui-org/react";
|
import { Button, Avatar, Link, Dropdown, DropdownTrigger, DropdownMenu, DropdownItem, Input } from "@nextui-org/react";
|
||||||
import { Modal, ModalContent, ModalHeader, ModalBody, ModalFooter, useDisclosure } from "@nextui-org/react";
|
import { Modal, ModalContent, ModalHeader, ModalBody, ModalFooter, useDisclosure } from "@nextui-org/react";
|
||||||
import axios from "axios";
|
|
||||||
import config from "../config";
|
import config from "../config";
|
||||||
|
import instance from "../security/http";
|
||||||
|
|
||||||
interface Post {
|
interface Post {
|
||||||
title: string;
|
title: string;
|
||||||
@@ -30,14 +30,14 @@ export default function CommunityPage() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const getPosts = () => {
|
const getPosts = () => {
|
||||||
axios
|
instance
|
||||||
.get(config.serverAddress + '/post').then((res) => {
|
.get(config.serverAddress + '/post').then((res) => {
|
||||||
setCommunityList(res.data);
|
setCommunityList(res.data);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const searchPosts = () => {
|
const searchPosts = () => {
|
||||||
axios
|
instance
|
||||||
.get(config.serverAddress + `/post?search=${search}`).then((res) => {
|
.get(config.serverAddress + `/post?search=${search}`).then((res) => {
|
||||||
setCommunityList(res.data);
|
setCommunityList(res.data);
|
||||||
});
|
});
|
||||||
@@ -62,7 +62,7 @@ export default function CommunityPage() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
axios
|
instance
|
||||||
.get(config.serverAddress + '/post').then((res) => {
|
.get(config.serverAddress + '/post').then((res) => {
|
||||||
console.log(res.data);
|
console.log(res.data);
|
||||||
setCommunityList(res.data);
|
setCommunityList(res.data);
|
||||||
@@ -76,7 +76,7 @@ export default function CommunityPage() {
|
|||||||
const handleDeleteConfirm = async () => {
|
const handleDeleteConfirm = async () => {
|
||||||
if (selectedPost) {
|
if (selectedPost) {
|
||||||
try {
|
try {
|
||||||
await axios
|
await instance
|
||||||
.delete(config.serverAddress + `/post/${selectedPost.id}`);
|
.delete(config.serverAddress + `/post/${selectedPost.id}`);
|
||||||
setCommunityList((prevList) => prevList.filter(post => post.id !== selectedPost.id));
|
setCommunityList((prevList) => prevList.filter(post => post.id !== selectedPost.id));
|
||||||
onOpenChange();
|
onOpenChange();
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ export default function SchedulePage() {
|
|||||||
<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 flex-col gap-8">
|
<div className="flex flex-col gap-8">
|
||||||
<Table>
|
<Table aria-label="Schedule Table">
|
||||||
<TableHeader>
|
<TableHeader>
|
||||||
<TableColumn>Date</TableColumn>
|
<TableColumn>Date</TableColumn>
|
||||||
<TableColumn>Time</TableColumn>
|
<TableColumn>Time</TableColumn>
|
||||||
@@ -77,7 +77,7 @@ export default function SchedulePage() {
|
|||||||
<div>
|
<div>
|
||||||
<div className="flex flex-row gap-20 ">
|
<div className="flex flex-row gap-20 ">
|
||||||
<div>
|
<div>
|
||||||
<Card>
|
<Card aria-label="Paper Price Card 1">
|
||||||
<CardBody>
|
<CardBody>
|
||||||
<p className="text-lg">Paper</p>
|
<p className="text-lg">Paper</p>
|
||||||
<p className="text-xl">$0.05 to 0.20/KG</p>
|
<p className="text-xl">$0.05 to 0.20/KG</p>
|
||||||
@@ -90,7 +90,7 @@ export default function SchedulePage() {
|
|||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<Card>
|
<Card aria-label="Paper Price Card 2">
|
||||||
<CardBody>
|
<CardBody>
|
||||||
<p className="text-lg">Paper</p>
|
<p className="text-lg">Paper</p>
|
||||||
<p className="text-xl">$0.05 to 0.20/KG</p>
|
<p className="text-xl">$0.05 to 0.20/KG</p>
|
||||||
@@ -103,7 +103,7 @@ export default function SchedulePage() {
|
|||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<Card>
|
<Card aria-label="Paper Price Card 3">
|
||||||
<CardBody>
|
<CardBody>
|
||||||
<p className="text-lg">Paper</p>
|
<p className="text-lg">Paper</p>
|
||||||
<p className="text-xl">$0.05 to 0.20/KG</p>
|
<p className="text-xl">$0.05 to 0.20/KG</p>
|
||||||
|
|||||||
@@ -28,6 +28,9 @@ app.use("/users", usersRoute);
|
|||||||
const postRoute = require('./routes/post');
|
const postRoute = require('./routes/post');
|
||||||
app.use("/post", postRoute);
|
app.use("/post", postRoute);
|
||||||
|
|
||||||
|
const schedulesRoute = require("./routes/schedule");
|
||||||
|
app.use("/schedule", schedulesRoute)
|
||||||
|
|
||||||
db.sequelize
|
db.sequelize
|
||||||
.sync({ alter: true })
|
.sync({ alter: true })
|
||||||
.then(() => {
|
.then(() => {
|
||||||
|
|||||||
108
server/routes/schedule.js
Normal file
108
server/routes/schedule.js
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
const express = require('express');
|
||||||
|
const router = express.Router();
|
||||||
|
const { Schedule } = require('../models');
|
||||||
|
const { Op } = require("sequelize");
|
||||||
|
const yup = require("yup");
|
||||||
|
|
||||||
|
router.post("/", async (req, res) => {
|
||||||
|
let data = req.body;
|
||||||
|
// Validate request body
|
||||||
|
let validationSchema = yup.object().shape({
|
||||||
|
dateTime: yup.date().required(),
|
||||||
|
location: yup.string().trim().min(15).max(50).required(),
|
||||||
|
postalCode: yup.string().matches(/^\d{6}$/, 'Postal code must be exactly 6 digits').required(),
|
||||||
|
status: yup.string().trim().required()
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
data = await validationSchema.validate(data, { abortEarly: false });
|
||||||
|
// Process valid data
|
||||||
|
let result = await Schedule.create(data);
|
||||||
|
res.json(result);
|
||||||
|
} catch (err) {
|
||||||
|
res.status(400).json({ errors: err.errors });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/", async (req, res) => {
|
||||||
|
let condition = {};
|
||||||
|
let search = req.query.search;
|
||||||
|
if (search) {
|
||||||
|
condition[Op.or] = [
|
||||||
|
{ dateTime: { [Op.like]: `%${search}%` } },
|
||||||
|
{ location: { [Op.like]: `%${search}%` } },
|
||||||
|
{ postalCode: { [Op.like]: `%${search}%` } },
|
||||||
|
{ status: { [Op.like]: `%${search}%` } }
|
||||||
|
];
|
||||||
|
}
|
||||||
|
let list = await Schedule.findAll({
|
||||||
|
where: condition,
|
||||||
|
order: [['createdAt', 'ASC']]
|
||||||
|
});
|
||||||
|
res.json(list);
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get("/:id", async (req, res) => {
|
||||||
|
let id = req.params.id;
|
||||||
|
let schedule = await Schedule.findByPk(id);
|
||||||
|
if (!schedule) {
|
||||||
|
res.sendStatus(404);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res.json(schedule);
|
||||||
|
});
|
||||||
|
|
||||||
|
router.put("/:id", async (req, res) => { //update
|
||||||
|
let id = req.params.id;
|
||||||
|
let schedule = await Schedule.findByPk(id);
|
||||||
|
if (!schedule) {
|
||||||
|
res.sendStatus(404);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let data = req.body;
|
||||||
|
let validationSchema = yup.object().shape({
|
||||||
|
dateTime: yup.date().required(),
|
||||||
|
location: yup.string().trim().min(15).max(50).required(),
|
||||||
|
postalCode: yup.string().matches(/^\d{6}$/, 'Postal code must be exactly 6 digits').required(),
|
||||||
|
status: yup.string().trim().required()
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
data = await validationSchema.validate(data,
|
||||||
|
{ abortEarly: false });
|
||||||
|
let num = await Schedule.update(data, {
|
||||||
|
where: { id: id }
|
||||||
|
});
|
||||||
|
if (num == 1) {
|
||||||
|
res.json({
|
||||||
|
message: "Schedule was updated successfully."
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
res.status(400).json({
|
||||||
|
message: `Cannot update schedule with id ${id}.`
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
res.status(400).json({ errors: err.errors });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
router.delete("/:id", async (req, res) => {
|
||||||
|
let id = req.params.id;
|
||||||
|
let num = await Schedule.destroy({
|
||||||
|
where: { id: id }
|
||||||
|
})
|
||||||
|
if (num == 1) {
|
||||||
|
res.json({
|
||||||
|
message: "Schedule was deleted successfully."
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
res.status(400).json({
|
||||||
|
message: `Cannot delete schedule with id ${id}.`
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
Reference in New Issue
Block a user