From 8e2b806fbf29cd34d81bf5e96e9e35f7d487df6a Mon Sep 17 00:00:00 2001 From: ZacTohZY Date: Mon, 29 Jul 2024 02:56:30 +0800 Subject: [PATCH] Retrieving user.id to HBFormPage --- client/src/pages/HBFormPage.tsx | 45 ++++++++++++++++++++++++--------- server/models/HBCform.js | 7 ++++- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/client/src/pages/HBFormPage.tsx b/client/src/pages/HBFormPage.tsx index 5e36184..841d8a1 100644 --- a/client/src/pages/HBFormPage.tsx +++ b/client/src/pages/HBFormPage.tsx @@ -7,6 +7,8 @@ import config from "../config"; import NextUIFormikInput from "../components/NextUIFormikInput"; import axios from "axios"; import InsertImage from "../components/InsertImage"; +import { retrieveUserInformation } from "../security/users"; +import { useEffect, useState } from "react"; const validationSchema = Yup.object({ electricalBill: Yup.number() @@ -32,17 +34,8 @@ const validationSchema = Yup.object({ }); export default function HBFormPage() { - const navigate = useNavigate(); - - const initialValues: { - id: string; - electricalBill: string; - waterBill: string; - totalBill: string; - noOfDependents: string; - ebPicture: File | null; - wbPicture: File | null; - } = { + const [userId, setUserId] = useState(null); + const [initialValues, setInitialValues] = useState({ id: "", electricalBill: "", waterBill: "", @@ -50,7 +43,31 @@ export default function HBFormPage() { noOfDependents: "", ebPicture: null, wbPicture: null, - }; + userId: "", + }); + + useEffect(() => { + const getUserInformation = async () => { + try { + const user = await retrieveUserInformation(); // Get the user ID + setUserId(user.id); // Set the user ID in the state + } catch (error) { + console.error(error); + } + }; + getUserInformation(); + }, []); + + useEffect(() => { + if (userId) { + setInitialValues((prevInitialValues) => ({ + ...prevInitialValues, + userId, + })); + } + }, [userId]); + + const navigate = useNavigate(); const handleSubmit = async ( values: any, @@ -70,6 +87,10 @@ export default function HBFormPage() { formData.append("wbPicture", values.wbPicture); } + if (userId != null) { + formData.append("userId", userId); + } + try { const response = await axios.post( config.serverAddress + "/hbcform", diff --git a/server/models/HBCform.js b/server/models/HBCform.js index 5273b2f..65e7aec 100644 --- a/server/models/HBCform.js +++ b/server/models/HBCform.js @@ -34,9 +34,14 @@ module.exports = (sequelize, DataTypes) => { type: DataTypes.BLOB("long"), allowNull: true, }, + userId: { + type: DataTypes.UUID, + }, }, { - tableName: 'hbcform' + tableName: "hbcform", + timestamps: true, }); + return HBCform; } \ No newline at end of file