From f57c2001634a8039979904d8b918a9943530bf5d Mon Sep 17 00:00:00 2001 From: Araozu Date: Wed, 4 Oct 2023 17:15:37 -0500 Subject: [PATCH] [Classroom][FE] Request to enrollment to backend. Improve UI --- backend/src/controller/classroom/mod.rs | 2 + backend/src/main.rs | 1 + .../OnlineClassroom/ClassroomRegistration.tsx | 8 +++- .../ClassroomRegistrationPreview.tsx | 39 +++++++++++++++++-- .../OnlineClassroom/ClassroomUserCourses.tsx | 13 ++++++- frontend/src/OnlineClassroom/index.tsx | 13 ++++++- 6 files changed, 68 insertions(+), 8 deletions(-) diff --git a/backend/src/controller/classroom/mod.rs b/backend/src/controller/classroom/mod.rs index 0aa37ce..6ba46dc 100644 --- a/backend/src/controller/classroom/mod.rs +++ b/backend/src/controller/classroom/mod.rs @@ -27,6 +27,8 @@ pub async fn get_courses(user_id: i32) -> (Status, Json")] +pub fn register_course_contr_options(_u: i32) -> Status {Status::Ok} #[post("/classroom/course/", format = "json", data = "")] pub async fn register_course_contr(user_id: i32, data: Json) -> (Status, Json>) { diff --git a/backend/src/main.rs b/backend/src/main.rs index 23c4f9d..cf1efd2 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -62,6 +62,7 @@ async fn rocket() -> _ { controller::classroom::create_user_options, controller::classroom::create_user, controller::classroom::get_courses, + controller::classroom::register_course_contr_options, controller::classroom::register_course_contr, ], ) diff --git a/frontend/src/OnlineClassroom/ClassroomRegistration.tsx b/frontend/src/OnlineClassroom/ClassroomRegistration.tsx index e9e15fd..4438a04 100644 --- a/frontend/src/OnlineClassroom/ClassroomRegistration.tsx +++ b/frontend/src/OnlineClassroom/ClassroomRegistration.tsx @@ -4,7 +4,10 @@ import { ClassroomRegistrationPreview } from "./ClassroomRegistrationPreview"; import { ClassroomSearchableSelect } from "./ClassroomSearchableSelect"; import { ClassroomCourseValue } from "../types/ClassroomCourse"; -export function ClassroomRegistration() { +export function ClassroomRegistration(props: { + surname_first_letter: string, userId: number | null, + onSuccess: () => void, +}) { const [selections, setSelections] = createSignal>([]); return ( @@ -20,8 +23,11 @@ export function ClassroomRegistration() { setSelections((course) => course.filter((v) => v !== course_key))} + onSuccess={props.onSuccess} /> ); diff --git a/frontend/src/OnlineClassroom/ClassroomRegistrationPreview.tsx b/frontend/src/OnlineClassroom/ClassroomRegistrationPreview.tsx index 9710f7f..302d30f 100644 --- a/frontend/src/OnlineClassroom/ClassroomRegistrationPreview.tsx +++ b/frontend/src/OnlineClassroom/ClassroomRegistrationPreview.tsx @@ -1,19 +1,52 @@ import { For, createMemo } from "solid-js"; import { FilledCard } from "../components/FilledCard"; import { LoadingIcon } from "../icons/LoadingIcon"; -import { LoadingStatus, useLoading } from "../utils/functions"; +import { LoadingStatus, backend, useLoading, wait } from "../utils/functions"; import { ClassroomCourseValue, allClassrooomCourses } from "../types/ClassroomCourse"; import { XIcon } from "../icons/XIcon"; +import { JsonResult } from "../types/JsonResult"; +import { AxiosError } from "axios"; export function ClassroomRegistrationPreview(props: { + surname_first_letter: string, + userId: number | null, selections: Array, deleteRegister: (k: string) => void, + onSuccess: () => void, }) { - const {status} = useLoading(); + const {status, setStatus, setError} = useLoading(); const loading = createMemo(() => status() === LoadingStatus.Loading); const submit = async() => { + if (props.userId === null) return; + + setStatus(LoadingStatus.Loading); + + if (import.meta.env.DEV) await wait(1500); + + backend.post>(`/api/classroom/course/${props.userId}`, { + surname_first_letter: "A", + courses: props.selections, + }) + .then((response) => { + if (response.status === 200) { + setStatus(LoadingStatus.Ok); + props.onSuccess(); + } else { + console.error(response.data); + setError(response.data.Err.reason); + setStatus(LoadingStatus.Error); + } + }) + .catch((err: AxiosError>) => { + console.error(err); + if (err.response?.data?.Err?.reason) { + setError(err.response.data.Err.reason); + } + + setStatus(LoadingStatus.Error); + }); }; return ( @@ -40,7 +73,7 @@ export function ClassroomRegistrationPreview(props: { class="bg-c-primary text-c-on-primary px-4 py-2 rounded-full cursor-pointer mt-4 disabled:opacity-50 disabled:cursor-not-allowed relative" type="button" - disabled={props.selections.length === 0 || loading()} + disabled={props.userId === null || props.selections.length === 0 || loading()} onclick={submit} > >([]); const {setError, status, setStatus} = useLoading(); const loadCourses = async() => { setStatus(LoadingStatus.Loading); setError(""); + setCourses([]); if (import.meta.env.DEV) await wait(1500); @@ -29,6 +30,14 @@ export function ClassroomUserCourses(props: {userid: number}) { onMount(loadCourses); + createEffect(() => { + // Update courses when updateSignal changes + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const _ = props.updateSignal; + + loadCourses(); + }); + return (

diff --git a/frontend/src/OnlineClassroom/index.tsx b/frontend/src/OnlineClassroom/index.tsx index 70e2910..881b038 100644 --- a/frontend/src/OnlineClassroom/index.tsx +++ b/frontend/src/OnlineClassroom/index.tsx @@ -11,12 +11,18 @@ type TabType = "Vinculate" | "Create"; export function OnlineClassroom() { const [person, setPerson] = createSignal(null); + // Used to update ClassroomUserCourses + const [updateSignal, setUpdateSIgnal] = createSignal(0); return (
- + setUpdateSIgnal((s) => s + 1)} + /> - +