diff --git a/src/Pages/UserInfoPage/UserInfoPage.tsx b/src/Pages/UserInfoPage/UserInfoPage.tsx index 7f4e49c..90ccd5f 100644 --- a/src/Pages/UserInfoPage/UserInfoPage.tsx +++ b/src/Pages/UserInfoPage/UserInfoPage.tsx @@ -1,212 +1,218 @@ -import Header from "../../Components/Header/Header"; -import styles from "../../styles"; -import RestrictedComponent from "../../Components/RestrictedComponent/RestrictedComponent"; -import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; -import { UserAPI, UserUpdateAPI } from "../../Components/API/API"; -import CircularProgress from "@mui/material/CircularProgress/CircularProgress"; -import { useEffect, useState } from "react"; -import { toast } from "react-toastify"; -import { - FormControlLabel, - FormLabel, - Radio, - RadioGroup, - TextField, -} from "@mui/material"; -export default function UserInfoPage() { - const queryClient = useQueryClient(); - const User = useQuery({ queryKey: ["user"], queryFn: UserAPI }); - const [user, setUser] = useState({ - first_name: "", - last_name: "", - username: "", - email: "", - course: "", - section: "", - }); - const [error, setError] = useState(""); - const mutation = useMutation({ - mutationFn: async () => { - const data = await UserUpdateAPI(user); - if (data[0] != true) { - setError(JSON.stringify(data[1])); - return Promise.reject(new Error(JSON.stringify(data[1]))); - } - return data; - }, - onSuccess: (data) => { - queryClient.invalidateQueries({ - queryKey: ["user"], - }); - setError("Updated successfully"); - toast(`User info updated successfuly`, { - position: "top-right", - autoClose: 2000, - hideProgressBar: false, - closeOnClick: true, - pauseOnHover: true, - draggable: true, - progress: undefined, - theme: "light", - }); - if (data && User.data) { - setUser({ - first_name: User.data.first_name, - last_name: User.data.last_name, - username: User.data.username, - email: User.data.email, - course: User.data.course, - section: User.data.section, - }); - } - }, - }); - useEffect(() => { - if (User.data) { - setUser(User.data); - } - }, [User.data]); - - if (User.isLoading) { - return ( -
-
-
- -

- Loading -

-
-
- ); - } - return ( -
-
-
- ) => { - setUser({ ...user, first_name: e.target.value }); - setError(""); - }} - value={user.first_name} - placeholder={"Enter your first name"} - /> - ) => - setUser({ ...user, last_name: e.target.value }) - } - value={user.last_name} - placeholder={"Enter your last name"} - /> -

{JSON.stringify(User.data)}

-
- - - - Course - - ) => { - setUser({ ...user, course: e.target.value }); - setError(""); - }} - > -
- } - label="BS Chemistry" - style={styles.text_dark} - /> - } - label="BS Food Technology" - style={styles.text_dark} - /> - } - label="BS Applied Physics" - style={styles.text_dark} - /> - } - label="BS Environmental Science" - style={styles.text_dark} - /> -
-
- ) => { - setUser({ ...user, section: e.target.value }); - setError(""); - }} - value={user.username} - placeholder={"Enter current section"} - /> -
-

- {error} -

- -
- ); -} +import Header from "../../Components/Header/Header"; +import styles from "../../styles"; +import RestrictedComponent from "../../Components/RestrictedComponent/RestrictedComponent"; +import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; +import { UserAPI, UserUpdateAPI } from "../../Components/API/API"; +import CircularProgress from "@mui/material/CircularProgress/CircularProgress"; +import { useEffect, useState } from "react"; +import { toast } from "react-toastify"; +import { + FormControlLabel, + FormLabel, + Radio, + RadioGroup, + TextField, +} from "@mui/material"; +export default function UserInfoPage() { + const queryClient = useQueryClient(); + const User = useQuery({ queryKey: ["user"], queryFn: UserAPI }); + const [user, setUser] = useState({ + first_name: "", + last_name: "", + username: "", + email: "", + course: "", + section: "", + }); + const [error, setError] = useState(""); + const mutation = useMutation({ + mutationFn: async () => { + const data = await UserUpdateAPI(user); + if (data[0] != true) { + setError(JSON.stringify(data[1])); + return Promise.reject(new Error(JSON.stringify(data[1]))); + } + return data; + }, + onSuccess: (data) => { + queryClient.invalidateQueries({ + queryKey: ["user"], + }); + setError("Updated successfully"); + toast(`User info updated successfuly`, { + position: "top-right", + autoClose: 2000, + hideProgressBar: false, + closeOnClick: true, + pauseOnHover: true, + draggable: true, + progress: undefined, + theme: "light", + }); + if (data && User.data) { + setUser({ + first_name: User.data.first_name, + last_name: User.data.last_name, + username: User.data.username, + email: User.data.email, + course: User.data.course, + section: User.data.section, + }); + } + }, + }); + useEffect(() => { + if (User.data) { + setUser(User.data); + } + }, [User.data]); + + if (User.isLoading) { + return ( +
+
+
+ +

+ Loading +

+
+
+ ); + } + return ( +
+
+
+ ) => { + setUser({ ...user, first_name: e.target.value }); + setError(""); + }} + value={user.first_name} + placeholder={"Enter your first name"} + /> + ) => + setUser({ ...user, last_name: e.target.value }) + } + value={user.last_name} + placeholder={"Enter your last name"} + /> +

{JSON.stringify(User.data)}

+
+ + + + Course + + ) => { + setUser({ ...user, course: e.target.value }); + setError(""); + }} + > +
+ } + label="BS Chemistry" + style={styles.text_dark} + /> + } + label="BS Food Technology" + style={styles.text_dark} + /> + } + label="BS Applied Physics" + style={styles.text_dark} + /> + } + label="BS Environmental Science" + style={styles.text_dark} + /> + } + label="Not Applicable" + style={styles.text_dark} + /> +
+
+ ) => { + setUser({ ...user, section: e.target.value }); + setError(""); + }} + value={user.username} + placeholder={"Enter current section"} + /> +
+

+ {error} +

+ +
+ ); +}