diff --git a/backend/src/online_classroom/session.rs b/backend/src/online_classroom/session.rs index 1d09662..3e8f0aa 100644 --- a/backend/src/online_classroom/session.rs +++ b/backend/src/online_classroom/session.rs @@ -1,7 +1,7 @@ use chrono::{DateTime, Local, TimeZone, Utc}; use lazy_static::lazy_static; -use urlencoding::encode; use std::time::{SystemTime, UNIX_EPOCH}; +use urlencoding::encode; use isahc::{cookies::CookieJar, prelude::*, Request}; use std::sync::RwLock; @@ -106,7 +106,7 @@ pub async fn create_user_request(url: String, body: String) -> Result Err(format!("Error getting text from response: {:?}", err)), } } @@ -134,19 +134,7 @@ pub async fn register_courses_request(url: String, body: String) -> Result { - // Get current time and date in iso - let now: DateTime = Local::now(); - let now = now.to_rfc3339(); - - // Write html to file - let r = std::fs::write(format!("request-logs/{}.html", now), &t); - if let Err(err) = r { - eprintln!("Error writing request html to file: {:?}", err) - } - - Ok(t) - }, + Ok(t) => Ok(t), Err(err) => Err(format!("Error getting text from response: {:?}", err)), } } @@ -196,10 +184,36 @@ async fn login() -> Result<(), String> { .send(); match response { - Ok(_) => { - SESSION_COOKIE.write().unwrap().jar = jar.clone(); - Ok(()) + Ok(mut r) => { + if r.status() == isahc::http::StatusCode::FOUND { + SESSION_COOKIE.write().unwrap().jar = jar.clone(); + Ok(()) + } else { + // Write html to file + match r.text() { + Ok(t) => { + log_html(t); + } + Err(err) => { + return Err(format!("Error getting text from login response: {:?}", err)) + } + }; + + Err(format!("Error connecting to online classroom: not 302")) + } } Err(error) => Err(format!("Error connecting to online classroom: {:?}", error)), } } + +fn log_html(html: String) { + // Get current time and date in iso + let now: DateTime = Local::now(); + let now = now.to_rfc3339(); + + // Write html to file + let r = std::fs::write(format!("request-logs/{}.html", now), &html); + if let Err(err) = r { + eprintln!("Error writing request html to file: {:?}", err) + } +} diff --git a/frontend/src/OnlineClassroom/ClassroomVinculation.tsx b/frontend/src/OnlineClassroom/ClassroomVinculation.tsx index 31e1569..1417610 100644 --- a/frontend/src/OnlineClassroom/ClassroomVinculation.tsx +++ b/frontend/src/OnlineClassroom/ClassroomVinculation.tsx @@ -6,30 +6,37 @@ import { LoadingStatus, backend, useLoading, wait } from "../utils/functions"; import { LinkIcon } from "../icons/LinkIcon"; import { For, Show, createSignal, onMount } from "solid-js"; import { LoadingIcon } from "../icons/LoadingIcon"; +import { AxiosError } from "axios"; -type Status = "Init" | "Ok" | "Loading" | "Error"; export function ClassroomVinculation(props: { person_surname: string, personId: number, onLink: (classroom_id: number, classroom_username: string) => void, }) { const [classroomUsers, setClassroomUsers] = createSignal([]); - const [error, setError] = createSignal(""); - const [status, setStatus] = createSignal("Init"); + const {status, setStatus, error, setError} = useLoading(); const loadUsers = async() => { - setStatus("Loading"); - const response = await fetch(`${import.meta.env.VITE_BACKEND_URL}/api/classroom/users/${encodeURIComponent(props.person_surname)}`); - const json: JsonResult> = await response.json(); + setClassroomUsers([]); + setStatus(LoadingStatus.Loading); - if (response.ok) { - setClassroomUsers(json.Ok); - setStatus("Ok"); - } else { - console.error("Error loading users", json); - setError(json.Err.reason); - setStatus("Error"); - } + backend.get>>(`/api/classroom/users/${encodeURIComponent(props.person_surname)}`) + .then((response) => { + if (response.status === 200) { + setClassroomUsers(response.data.Ok); + setStatus(LoadingStatus.Ok); + } else { + setClassroomUsers([]); + setStatus(LoadingStatus.Error); + console.error(response.data); + setError(response.data.Err.reason); + } + }) + .catch((err: AxiosError>>) => { + console.error(err); + setError(`Error: ${err.response?.data.Err.reason ?? err.message}`); + setStatus(LoadingStatus.Error); + }); }; onMount(loadUsers); @@ -40,13 +47,13 @@ export function ClassroomVinculation(props: { Vincule un usuario existente:

- +
- +
@@ -66,7 +73,7 @@ export function ClassroomVinculation(props: { - +