Skip to content

Commit

Permalink
edit profile with user id
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Sicat committed Dec 16, 2023
1 parent f1504f1 commit 1fa9b5b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 50 deletions.
15 changes: 14 additions & 1 deletion backend/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<int:user_id>', 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:
Expand Down
24 changes: 12 additions & 12 deletions backend/db/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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');
10 changes: 5 additions & 5 deletions backend/db/warehouse_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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');
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');
68 changes: 36 additions & 32 deletions frontend/app/admin_accounts/[userId]/page.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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(
Expand All @@ -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());
}
Expand Down Expand Up @@ -144,12 +154,6 @@ function Page() {
onSelect={(value) => handleInputChange("occupation", value)}
/>
</div>
{/* <TextInput
placeholder="School/Company"
width="335px"
height="35px"
onInputChange={(value) => handleInputChange("school", value)}
/> */}
</div>

<div className="mt-16"></div>
Expand Down

0 comments on commit 1fa9b5b

Please sign in to comment.