From 3931f584722cfaa09f2a60e6ef8d0cbdf75510e4 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Thu, 6 Jul 2023 20:29:04 +0800 Subject: [PATCH] Finished onboarding page --- src/components/Api/Api.tsx | 23 ++++++++++- src/interfaces/Interfaces.tsx | 6 +++ src/routes/Onboarding/Onboarding.tsx | 52 +++++++++--------------- src/routes/Revalidation/Revalidation.tsx | 11 +++-- 4 files changed, 54 insertions(+), 38 deletions(-) diff --git a/src/components/Api/Api.tsx b/src/components/Api/Api.tsx index cae13ae..38d2a54 100644 --- a/src/components/Api/Api.tsx +++ b/src/components/Api/Api.tsx @@ -3,6 +3,7 @@ import AsyncStorage from "@react-native-async-storage/async-storage"; import { ActivationParams, LoginParams, + OnboardingParams, RegistrationParams, } from "../../interfaces/Interfaces"; @@ -113,7 +114,7 @@ export async function UserInfo() { }) .then((response) => { // console.log(JSON.stringify(response.data)); - return response.data; + return [true, response.data]; }) .catch((error) => { let error_message = ""; @@ -198,3 +199,23 @@ export async function GetYearLevels() { return false; }); } + +export async function OnboardingUpdateStudentInfo(info: OnboardingParams) { + const accessToken = await getAccessToken(); + const headers = { + Authorization: `Bearer ${accessToken}`, + }; + return instance + .patch("/api/v1/accounts/users/me/", info, { headers }) + .then((response) => { + console.log(JSON.stringify(response.data)); + return response.data; + }) + .catch((error) => { + let error_message = ""; + if (error.response) error_message = error.response.data; + else error_message = "Unable to reach servers"; + console.log("Error updating onboarding info", error_message); + return false; + }); +} diff --git a/src/interfaces/Interfaces.tsx b/src/interfaces/Interfaces.tsx index eae85db..d9671ec 100644 --- a/src/interfaces/Interfaces.tsx +++ b/src/interfaces/Interfaces.tsx @@ -64,3 +64,9 @@ export interface CourseParams { name: string; shortname: string; } + +export interface OnboardingParams { + year_level: string; + course: string; + semester: string; +} diff --git a/src/routes/Onboarding/Onboarding.tsx b/src/routes/Onboarding/Onboarding.tsx index a763653..16eb9f5 100644 --- a/src/routes/Onboarding/Onboarding.tsx +++ b/src/routes/Onboarding/Onboarding.tsx @@ -19,18 +19,18 @@ import { GetCourses, GetSemesters, GetYearLevels, + OnboardingUpdateStudentInfo, } from "../../components/Api/Api"; export default function Onboarding() { const navigation = useNavigation(); // const dispatch = useDispatch(); // const creds = useSelector((state: RootState) => state.auth.creds); - const [error, setError] = useState(false); // Semesters const [semester, setSemester] = useState(""); const [semesterOpen, setSemesterOpen] = useState(false); const [semesters, setSemesters] = useState([ - { label: "1st Semester", value: "1st Sem", id: "" }, - { label: "2nd Semester", value: "2nd Sem", id: "" }, + { label: "1st Semester", value: "1st Sem" }, + { label: "2nd Semester", value: "2nd Sem" }, ]); const semester_query = useQuery({ queryKey: ["semesters"], @@ -38,21 +38,17 @@ export default function Onboarding() { onSuccess: (data) => { let semesters = data.map((item: SemesterParams) => ({ label: item.name, - value: item.shortname, - id: item.id, + value: item.name, })); setSemesters(semesters); }, - onError: () => { - setError(true); - }, }); // Year Level const [year_level, setYearLevel] = useState(""); const [yearLevelOpen, setYearLevelOpen] = useState(false); const [year_levels, setYearLevels] = useState([ - { label: "1st Year", value: "1st Year", id: "" }, - { label: "2nd Year", value: "2nd Year", id: "" }, + { label: "1st Year", value: "1st Year" }, + { label: "2nd Year", value: "2nd Year" }, ]); const yearlevel_query = useQuery({ queryKey: ["year_levels"], @@ -60,14 +56,10 @@ export default function Onboarding() { onSuccess: (data) => { let year_levels = data.map((item: YearLevelParams) => ({ label: item.name, - value: item.shortname, - id: item.id, + value: item.name, })); setYearLevels(year_levels); }, - onError: () => { - setError(true); - }, }); // Course const [course, setCourse] = useState(""); @@ -76,34 +68,21 @@ export default function Onboarding() { { label: "Bachelor of Science in Information Technology", value: "BSIT", - id: "", }, - { label: "Bachelor of Science in Computer Science", value: "BSCS", id: "" }, + { label: "Bachelor of Science in Computer Science", value: "BSCS" }, ]); - const [complete, setComplete] = useState(false); const course_query = useQuery({ queryKey: ["courses"], queryFn: GetCourses, onSuccess: (data) => { let courses = data.map((item: CourseParams) => ({ label: item.name, - value: item.shortname, - id: item.id, + value: item.name, })); setCourses(courses); }, - onError: () => { - setError(true); - }, }); - useEffect(() => { - setComplete( - !isStringEmpty(year_level) && - !isStringEmpty(course) && - !isStringEmpty(semester) - ); - }, [year_level, course, semester, complete]); - if (error) { + if (yearlevel_query.error || semester_query.error || course_query.error) { return ( @@ -213,8 +192,15 @@ export default function Onboarding() { style={styles.button_template} >