diff --git a/client/src/pages/CreateSchedulePage.tsx b/client/src/pages/CreateSchedulePage.tsx index d95694c..e3cb963 100644 --- a/client/src/pages/CreateSchedulePage.tsx +++ b/client/src/pages/CreateSchedulePage.tsx @@ -1,7 +1,6 @@ import { Formik, Form } from "formik"; import * as yup from "yup"; -import { Button, Radio } from "@nextui-org/react"; -import { NextUIFormikRadioGroup } from "../components/NextUIFormikRadioButton"; +import { Button } from "@nextui-org/react"; import { NextUIFormikDatePicker } from "../components/NextUIFormikDatePicker"; import NextUIFormikInput from "../components/NextUIFormikInput"; import { useNavigate } from "react-router-dom"; @@ -18,14 +17,15 @@ const validationSchema = yup.object().shape({ status: yup.string().trim().required() }); -const initialValues: any = { +const initialValues = { date: "", time: "", location: "", postalCode: "", - status: "" + status: "Up coming" // Set the default status }; + export default function CreateSchedulePage() { const navigate = useNavigate(); @@ -55,14 +55,9 @@ export default function CreateSchedulePage() { } data.dateTime = dateTime.toISOString(); - data.location = data.location.trim(); - - if (typeof data.postalCode === 'string') { - data.postalCode = data.postalCode.trim(); - } - - data.status = data.status.trim(); + data.postalCode = data.postalCode.trim(); + data.status = "Up coming"; // Set status to "Up coming" explicitly console.log("Data to be sent:", data); @@ -74,8 +69,8 @@ export default function CreateSchedulePage() { }; return ( -
-
+
+
- ) + ); } - diff --git a/server/models/Schedule.js b/server/models/Schedule.js index 29ae8cc..b1becf5 100644 --- a/server/models/Schedule.js +++ b/server/models/Schedule.js @@ -1,22 +1,32 @@ -module.exports = (sequelize, DataTypes) => { - const Schedule = sequelize.define("Schedule", { - dateTime: { - type: DataTypes.DATE, - allowNull: false +const { DataTypes } = require("sequelize"); + +module.exports = (sequelize) => { + const Schedule = sequelize.define( + "Schedule", + { + id: { + type: DataTypes.UUID, + defaultValue: DataTypes.UUIDV4, + allowNull: false, + primaryKey: true, + }, + dateTime: { + type: DataTypes.DATE, + allowNull: false + }, + location: { + type: DataTypes.TEXT, + allowNull: false + }, + postalCode: { + type: DataTypes.INTEGER(6), + allowNull: false + }, + status: { + type: DataTypes.TEXT, + allowNull: false + }, }, - location: { - type: DataTypes.TEXT, - allowNull: false - }, - postalCode: { - type: DataTypes.INTEGER(6), - allowNull: false - }, - status: { - type: DataTypes.TEXT, - allowNull: false - }, - }, { tableName: "schedule" }); diff --git a/server/package.json b/server/package.json index 2056dc7..a7ac525 100644 --- a/server/package.json +++ b/server/package.json @@ -14,6 +14,7 @@ "axios": "^1.7.2", "bad-words": "^3.0.4", "cors": "^2.8.5", + "dayjs": "^1.11.12", "dotenv": "^16.4.5", "express": "^4.19.2", "jsonwebtoken": "^9.0.2", diff --git a/server/pnpm-lock.yaml b/server/pnpm-lock.yaml index 23f96c9..1e58a53 100644 --- a/server/pnpm-lock.yaml +++ b/server/pnpm-lock.yaml @@ -20,6 +20,9 @@ importers: cors: specifier: ^2.8.5 version: 2.8.5 + dayjs: + specifier: ^1.11.12 + version: 1.11.12 dotenv: specifier: ^16.4.5 version: 16.4.5 @@ -327,6 +330,9 @@ packages: resolution: {integrity: sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==} engines: {node: '>= 0.10'} + dayjs@1.11.12: + resolution: {integrity: sha512-Rt2g+nTbLlDWZTwwrIXjy9MeiZmSDI375FvZs72ngxx8PDC6YXOeR3q5LAuPzjZQxhiWdRKac7RKV+YyQYfYIg==} + debug@2.6.9: resolution: {integrity: sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==} peerDependencies: @@ -1229,6 +1235,8 @@ snapshots: object-assign: 4.1.1 vary: 1.1.2 + dayjs@1.11.12: {} + debug@2.6.9: dependencies: ms: 2.0.0 diff --git a/server/routes/schedule.js b/server/routes/schedule.js index 2d54503..3bc9d27 100644 --- a/server/routes/schedule.js +++ b/server/routes/schedule.js @@ -3,6 +3,7 @@ const router = express.Router(); const { Schedule } = require('../models'); const { Op } = require("sequelize"); const yup = require("yup"); +const dayjs = require('dayjs'); router.post("/", async (req, res) => { let data = req.body; @@ -104,5 +105,27 @@ router.delete("/:id", async (req, res) => { } }); +router.patch("/:id/status", async (req, res) => { + try { + const { id } = req.params; + const schedule = await Schedule.findByPk(id); + + if (!schedule) { + return res.status(404).json({ message: 'Schedule not found' }); + } + + const now = dayjs(); + const scheduleDateTime = dayjs(schedule.dateTime); + + const newStatus = scheduleDateTime.isAfter(now) ? "Up coming" : "Ended"; + schedule.status = newStatus; + + await schedule.save(); + res.status(200).json(schedule); + } catch (error) { + console.error("Error updating status:", error); // Log the error + res.status(500).json({ message: error.message }); + } +}); module.exports = router; \ No newline at end of file