From 3331ccb9745b52d2fea815991c4c988f6967e9e6 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Thu, 27 Jul 2023 00:01:44 +0800 Subject: [PATCH] Pray to the gods the duplicate subjects bug is fixed. Move irregular status toggle to user info page from subjects page --- src/components/Api/Api.tsx | 54 ++++----------- src/interfaces/Interfaces.tsx | 12 ++-- src/routes/Revalidation/Revalidation.tsx | 8 +-- src/routes/SubjectsPage/SubjectsPage.tsx | 88 +++++------------------- src/routes/UserInfoPage/UserInfoPage.tsx | 72 ++++++++++--------- 5 files changed, 79 insertions(+), 155 deletions(-) diff --git a/src/components/Api/Api.tsx b/src/components/Api/Api.tsx index e1eb813..58c3804 100644 --- a/src/components/Api/Api.tsx +++ b/src/components/Api/Api.tsx @@ -11,7 +11,7 @@ import { export let backendURL = ""; export let backendURLWebsocket = ""; -let use_production = true; +let use_production = false; if (__DEV__ && !use_production) { backendURL = "http://10.0.10.8:8000"; backendURLWebsocket = "ws://10.0.10.8:8000"; @@ -224,47 +224,19 @@ export async function GetYearLevels() { }); } -export async function GetSubjects( - byCourseOnly: boolean, - course: string, - year_level?: string, - semester?: string -) { +export async function GetSubjects() { const config = await GetConfig(); - console.log("by course only?", byCourseOnly); - // If year level and semester specified, - if (!byCourseOnly && year_level && semester) { - return instance - .get( - "/api/v1/subjects/" + course + "/" + year_level + "/" + semester, - config - ) - .then((response) => { - // console.log(JSON.stringify(response.data)); - return [true, response.data]; - }) - .catch((error) => { - let error_message = ""; - if (error.response) error_message = error.response.data; - else error_message = "Unable to reach servers"; - return [false, error_message]; - }); - } - // If only course is specified - else { - return instance - .get("/api/v1/subjects/" + course, config) - .then((response) => { - // console.log(JSON.stringify(response.data)); - return [true, response.data]; - }) - .catch((error) => { - let error_message = ""; - if (error.response) error_message = error.response.data; - else error_message = "Unable to reach servers"; - return [false, error_message]; - }); - } + return instance + .get("/api/v1/subjects/", config) + .then((response) => { + return [true, response.data]; + }) + .catch((error) => { + let error_message = ""; + if (error.response) error_message = error.response.data; + else error_message = "Unable to reach servers"; + return [false, error_message]; + }); } export async function OnboardingUpdateStudentInfo(info: OnboardingParams) { diff --git a/src/interfaces/Interfaces.tsx b/src/interfaces/Interfaces.tsx index 2b6e526..7c83a9b 100644 --- a/src/interfaces/Interfaces.tsx +++ b/src/interfaces/Interfaces.tsx @@ -85,11 +85,12 @@ export type CourseParams = [boolean, Courses]; // Subject export interface Subject { + id: number; name: string; code: string; - // courses: any[]; // To-do - // year_levels: any[]; // To-do - // semesters: any[]; // To-do + course: string; + year_level: string; + semester: string; } export type Subjects = Array; @@ -110,6 +111,7 @@ export interface PatchStudentData { semester?: string | null; subjects?: any[] | null; // To-do, replace 'any' with your actual type year_level?: string | null; + irregular?: boolean | null; } export interface StudentData { @@ -118,14 +120,14 @@ export interface StudentData { email: string; avatar: string; student_id_number: string; - is_banned: boolean; + irregular: boolean; semester: string; semester_shortname: string; course: string; course_shortname: string; year_level: string; yearlevel_shortname: string; - subjects: any[]; // To-do + subjects: Subject[]; // To-do username: string; } diff --git a/src/routes/Revalidation/Revalidation.tsx b/src/routes/Revalidation/Revalidation.tsx index ce5b1dc..c7d9869 100644 --- a/src/routes/Revalidation/Revalidation.tsx +++ b/src/routes/Revalidation/Revalidation.tsx @@ -28,11 +28,9 @@ export default function Revalidation() { dispatch(login()); dispatch(setUser(user_info[1])); if ( - !( - user_info[1].year_level || - user_info[1].course || - user_info[1].semester - ) + !user_info[1].year_level || + !user_info[1].course || + !user_info[1].semester ) { dispatch(setOnboarding()); await setTimeout(() => { diff --git a/src/routes/SubjectsPage/SubjectsPage.tsx b/src/routes/SubjectsPage/SubjectsPage.tsx index c95815b..1790663 100644 --- a/src/routes/SubjectsPage/SubjectsPage.tsx +++ b/src/routes/SubjectsPage/SubjectsPage.tsx @@ -33,9 +33,11 @@ import { import { colors } from "../../styles"; import DropDownPicker from "react-native-dropdown-picker"; import AnimatedContainerNoScroll from "../../components/AnimatedContainer/AnimatedContainerNoScroll"; -import BouncyCheckbox from "react-native-bouncy-checkbox"; +import { useSelector } from "react-redux"; +import { RootState } from "../../features/redux/Store/Store"; export default function SubjectsPage() { + const logged_in_user = useSelector((state: RootState) => state.user.user); const queryClient = useQueryClient(); // User Info const [user, setUser] = useState({ @@ -51,15 +53,10 @@ export default function SubjectsPage() { student_id_number: "", subjects: [] as Subjects, }); - const [displayName, setDisplayName] = useState({ - first_name: "", - last_name: "", - }); const StudentInfo = useQuery({ queryKey: ["user"], queryFn: UserInfo, onSuccess: (data: UserInfoParams) => { - // console.log(data[1]); setUser({ ...user, first_name: data[1].first_name, @@ -71,31 +68,18 @@ export default function SubjectsPage() { student_id_number: data[1].student_id_number, subjects: data[1].subjects, }); - setDisplayName({ - first_name: data[1].first_name, - last_name: data[1].last_name, - }); - setSelectedSubjects(user.subjects); + setSelectedSubjects(data[1].subjects); }, }); const mutation = useMutation({ mutationFn: PatchUserInfo, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["user"] }); - queryClient.invalidateQueries({ queryKey: ["subjects", viewAll] }); + queryClient.invalidateQueries({ queryKey: ["subjects"] }); setSelectedSubjects([]); }, }); - // View all Subjects or only view those under current course, year level, and semester - // This is for irregular students - const [viewAll, setViewAll] = useState(false); - - // If viewing all subjects, refresh the choices - useEffect(() => { - queryClient.invalidateQueries({ queryKey: ["subjects", viewAll] }); - }, [viewAll]); - // Subjects const [selected_subjects, setSelectedSubjects] = useState([]); @@ -104,42 +88,17 @@ export default function SubjectsPage() { const Subjects = useQuery({ enabled: StudentInfo.isFetched, - queryKey: ["subjects", viewAll], - queryFn: async () => { - let data; - if ( - StudentInfo.data && - StudentInfo.data[1].course_shortname && - StudentInfo.data[1].yearlevel_shortname && - StudentInfo.data[1].semester_shortname - ) { - data = await GetSubjects( - viewAll, - StudentInfo.data[1].course_shortname, - StudentInfo.data[1].yearlevel_shortname, - StudentInfo.data[1].semester_shortname - ); - console.log(JSON.stringify(data)); - } - if (data) { - if (!data[0]) { - throw new Error("Error with query" + data[1]); - } - if (!data[1]) { - throw new Error("User has no course, year level, or semester!"); - } - // console.log("Subjects available:", data[1]); - } - return data; - }, + queryKey: ["subjects"], + queryFn: GetSubjects, onSuccess: (data: SubjectParams) => { - let subjectsData = data[1].map((subject: Subject) => ({ - label: subject.name, - value: subject.name, - })); - // Update the 'subjects' state - setSelectedSubjects(user.subjects); - setSubjects(subjectsData); + if (data[1]) { + let subjects = data[1].map((subject: Subject) => ({ + label: subject.name, + value: subject.name, + })); + + setSubjects(subjects); + } }, }); @@ -163,9 +122,9 @@ export default function SubjectsPage() { - {(displayName.first_name || "Undefined") + + {(logged_in_user.first_name || "Undefined") + " " + - (displayName.last_name || "User") + + (logged_in_user.last_name || "User") + "\n" + user.student_id_number} @@ -211,22 +170,9 @@ export default function SubjectsPage() { - - - { - setViewAll(!viewAll); - setSubjectsOpen(false); - }} - fillColor={colors.secondary_3} - /> - Irregular -