diff --git a/backend/app/app.py b/backend/app/app.py index 5c862ff0..ef3605ab 100644 --- a/backend/app/app.py +++ b/backend/app/app.py @@ -345,7 +345,20 @@ def sign_in(): return response else: - return jsonify({"message": "Invalid login credentials"}), 401 + return jsonify({"message": "Invalid login credentials"}), 401 + +@app.route('/api/get-user/', methods=['GET']) +def get_user_by_id_route(user_id): + try: + user = get_user_by_id(user_id) + if user: + return jsonify({'user': user}), 200 + else: + return jsonify({'error': 'User not found'}), 404 + except Exception as e: + print(f"Error fetching user by ID: {e}") + return jsonify({'error': str(e)}), 500 + # @app.route('/api/update-account/', methods=['PUT']) # def update_account(user_id): # try: diff --git a/backend/db/init.sql b/backend/db/init.sql index c99be7de..1ece8042 100644 --- a/backend/db/init.sql +++ b/backend/db/init.sql @@ -38,19 +38,19 @@ CREATE TABLE QR_Codes ( ); --- INSERT INTO Users (GoogleID, Username, Email, Password, FirstName, LastName, PhoneNumber, UName, Birthday, Gender, School, Occupation, Level) --- VALUES --- ('google_id_1', 'user_1', 'user1@example.com', 'password1', 'John', 'Doe', '1234567890', 'UName1', '1990-01-01', 'Male', 'School1', 'Occupation1', 'User'), --- ('google_id_2', 'admin', 'user2@example.com', 'password', 'Jane', 'Doe', '9876543210', 'UName2', '1985-05-15', 'Female', 'School2', 'Occupation2', 'Admin'), +INSERT INTO Users (GoogleID, Username, Email, Password, FirstName, LastName, PhoneNumber, UName, Birthday, Gender, School, Occupation, Level) +VALUES +('google_id_1', 'user_1', 'user1@example.com', 'password1', 'John', 'Doe', '1234567890', 'UName1', '1990-01-01', 'Male', 'School1', 'Occupation1', 'User'), +('google_id_2', 'admin', 'user2@example.com', 'password', 'Jane', 'Doe', '9876543210', 'UName2', '1985-05-15', 'Female', 'School2', 'Occupation2', 'Admin'), --- ('google_id_3', 'user_3', 'user3@example.com', 'password3', 'Alice', 'Johnson', '5555555555', 'UName3', '1988-08-22', 'Female', 'School3', 'Occupation3', 'User'), --- ('google_id_4', 'user_4', 'user4@example.com', 'password4', 'Bob', 'Smith', '6666666666', 'UName4', '1975-12-10', 'Male', 'School4', 'Occupation4', 'User'); +('google_id_3', 'user_3', 'user3@example.com', 'password3', 'Alice', 'Johnson', '5555555555', 'UName3', '1988-08-22', 'Female', 'School3', 'Occupation3', 'User'), +('google_id_4', 'user_4', 'user4@example.com', 'password4', 'Bob', 'Smith', '6666666666', 'UName4', '1975-12-10', 'Male', 'School4', 'Occupation4', 'User'); --- INSERT INTO Reservations (UserID, StartTime, EndTime, Seat) --- VALUES --- (1, '2023-01-01 08:00:00', '2023-01-01 12:00:00', 'A1'), --- (2, '2023-01-02 09:00:00', '2023-01-02 13:00:00', 'B2'), +INSERT INTO Reservations (UserID, StartTime, EndTime, Seat) +VALUES +(1, '2023-01-01 08:00:00', '2023-01-01 12:00:00', 'A1'), +(2, '2023-01-02 09:00:00', '2023-01-02 13:00:00', 'B2'), --- (3, '2023-01-03 10:00:00', '2023-01-03 14:00:00', 'C3'), --- (4, '2023-01-04 11:00:00', '2023-01-04 15:00:00', 'D4'); +(3, '2023-01-03 10:00:00', '2023-01-03 14:00:00', 'C3'), +(4, '2023-01-04 11:00:00', '2023-01-04 15:00:00', 'D4'); diff --git a/backend/db/warehouse_init.sql b/backend/db/warehouse_init.sql index 4e738057..419d74cc 100644 --- a/backend/db/warehouse_init.sql +++ b/backend/db/warehouse_init.sql @@ -23,8 +23,8 @@ CREATE TABLE UserSummary ( -- Create an index on the UserID column CREATE INDEX idx_UserSummary_UserID ON UserSummary(UserID); --- INSERT INTO UserSummary (UserID, ReservationID, School, Occupation, StartTime, EndTime, Seat, Gender) --- VALUES --- (1, 101, 'University of the Philippines', 'Student', '2023-01-01 08:00:00', '2023-01-01 10:00:00', 'A1', 'Male'), --- (2, 102, 'Tech Institute', 'Engineer', '2023-01-02 09:00:00', '2023-01-02 12:00:00', 'B2', 'Female'), --- (3, 103, 'Business School', 'Entrepreneur', '2023-01-03 10:00:00', '2023-01-03 11:30:00', 'C3', 'Other'); \ No newline at end of file +INSERT INTO UserSummary (UserID, ReservationID, School, Occupation, StartTime, EndTime, Seat, Gender) +VALUES + (1, 101, 'University of the Philippines', 'Student', '2023-01-01 08:00:00', '2023-01-01 10:00:00', 'A1', 'Male'), + (2, 102, 'Tech Institute', 'Engineer', '2023-01-02 09:00:00', '2023-01-02 12:00:00', 'B2', 'Female'), + (3, 103, 'Business School', 'Entrepreneur', '2023-01-03 10:00:00', '2023-01-03 11:30:00', 'C3', 'Other'); \ No newline at end of file diff --git a/frontend/app/admin_accounts/[userId]/page.tsx b/frontend/app/admin_accounts/[userId]/page.tsx index b5390103..b67f6524 100644 --- a/frontend/app/admin_accounts/[userId]/page.tsx +++ b/frontend/app/admin_accounts/[userId]/page.tsx @@ -1,5 +1,4 @@ -"use client"; - +'use client'; import React, { useState, useEffect } from "react"; import Teste from "@/app/components/account"; import CloseIcon from "@mui/icons-material/Close"; @@ -9,47 +8,60 @@ import TextInput from "@/app/components/text_input"; import Drop from "@/app/components/dropdown_button"; import Butt from "@/app/components/button"; -import { useRouter } from "next/navigation"; +import { useRouter } from "next/router"; + function Page() { const router = useRouter(); const handleBackButtonClick = () => { router.back(); }; + const userId = 1; + + const [formData, setFormData] = useState({ + userName: "", + email: "", + phoneNumber: "", + gender: "", + occupation: "", + }); useEffect(() => { document.title = "Edit Profile"; - }, []); + + const fetchUserData = async () => { + try { + const response = await fetch(`http://localhost:5000/api/get-user/${userId}`); + if (response.ok) { + const userData = await response.json(); + setFormData({ + userName: userData.Username, + email: userData.Email, + phoneNumber: userData.PhoneNumber, + gender: userData.Gender, + occupation: userData.Occupation, + }); + } else { + console.error("Error fetching user data:", await response.json()); + } + } catch (error) { + console.error("Error fetching user data:", error); + } + }; + + fetchUserData(); + }, [userId]); const options = ["Male", "Female", "Others"]; const options1 = ["Student", "Worker"]; - // Fetch user data from local storage - const storedUserData = localStorage.getItem("user"); - const initialFormData = storedUserData ? JSON.parse(storedUserData) : null; - - const [formData, setFormData] = useState<{ - userName: string; - email: string; - phoneNumber: string; - gender: string; - occupation: string; - }>({ - userName: initialFormData ? initialFormData.Username : "", - email: initialFormData ? initialFormData.Email : "", - phoneNumber: initialFormData ? initialFormData.PhoneNumber : "", - gender: initialFormData ? initialFormData.Gender : options[0], - occupation: initialFormData ? initialFormData.Occupation : options1[0], - }); - const userId = initialFormData ? initialFormData.UserID : null; // Adjust this line based on your actual property name - console.log(userId); const handleInputChange = (field: string, value: string) => { setFormData({ ...formData, [field]: value, }); }; - console.log(formData); + const handleUpdateProfile = async () => { try { const response = await fetch( @@ -65,9 +77,7 @@ function Page() { if (response.ok) { const updatedUserData = await response.json(); - localStorage.setItem("user", JSON.stringify(updatedUserData)); console.log("Profile updated successfully:", updatedUserData); - // Optionally, you can update the local state or perform other actions } else { console.error("Error updating profile:", await response.json()); } @@ -144,12 +154,6 @@ function Page() { onSelect={(value) => handleInputChange("occupation", value)} /> - {/* handleInputChange("school", value)} - /> */}