diff --git a/src/apps/code_editor.rs b/src/apps/code_editor.rs index 517cbf3..a4cf17e 100644 --- a/src/apps/code_editor.rs +++ b/src/apps/code_editor.rs @@ -1,6 +1,6 @@ use crate::helpers::{ refresh, - submission::{Submission, SubmissionResult}, + submission::{Submission, SubmissionPromise, SubmissionResult}, Challenges, Languages, }; use gloo_net::http; @@ -11,19 +11,19 @@ use web_sys::RequestCredentials; #[serde(default)] pub struct CodeEditor { #[serde(skip)] - promise: Option>>, + promise: SubmissionPromise, #[serde(skip)] url: String, #[serde(skip)] run: Submission, #[serde(skip)] + last_result: SubmissionResult, + #[serde(skip)] submit: bool, #[serde(skip)] code: String, #[serde(skip)] token_refresh_promise: refresh::RefreshPromise, - #[serde(skip)] - last_message: Option, } impl Default for CodeEditor { @@ -42,7 +42,7 @@ impl Default for CodeEditor { code: "#A very simple example\nprint(\"Hello world!\")".into(), submit: false, token_refresh_promise: None, - last_message: None, + last_result: SubmissionResult::NotStarted, } } } @@ -101,28 +101,6 @@ impl CodeEditor { self.run.code = Some(self.code.clone()); self.run.test = false; } - - fn check_fetch_promise(&mut self) -> SubmissionResult { - let mut result = SubmissionResult::NotStarted; - if let Some(promise) = &self.promise { - result = SubmissionResult::Busy; - if let Some(response) = promise.ready() { - match response { - Ok(submission_response) => { - result = submission_response.clone(); - } - Err(error) => { - log::error!("Failed to fetch scores: {}", error); - result = SubmissionResult::Failure { - message: error.to_string(), - }; - } - } - self.promise = None; - } - } - result - } } impl super::App for CodeEditor { @@ -146,6 +124,18 @@ impl super::App for CodeEditor { refresh::RefreshStatus::Failed(_) => {} _ => (), } + + let submission = Submission::check_submit_promise(&mut self.promise); + match submission { + SubmissionResult::NotStarted => {} + SubmissionResult::NotAuthorized => { + self.token_refresh_promise = refresh::submit_refresh(&self.url); + self.last_result = submission; + } + _ => { + self.last_result = submission; + } + } } } @@ -228,30 +218,7 @@ impl super::View for CodeEditor { } }); ui.separator(); - ui.vertical(|ui| match self.check_fetch_promise() { - SubmissionResult::Success { score: _, message } => { - ui.label(&message); - self.last_message = Some(message); - } - SubmissionResult::Failure { message } => { - self.last_message = Some(format!("Fail: {}", message)); - ui.label(format!("Fail: {}", message)); - } - SubmissionResult::NotAuthorized => { - self.last_message = Some("Not authorized".to_string()); - ui.label("Not authorized"); - self.token_refresh_promise = refresh::submit_refresh(&self.url); - } - SubmissionResult::Busy => { - ui.label("Running..."); - } - SubmissionResult::NotStarted => match &self.last_message { - Some(message) => { - ui.label(format!("Message: {}", message)); - } - None => {} - }, - }); + ui.vertical(|ui| ui.label(self.last_result.to_string())); }); } } diff --git a/src/helpers/submission.rs b/src/helpers/submission.rs index e04a013..d836ef0 100644 --- a/src/helpers/submission.rs +++ b/src/helpers/submission.rs @@ -1,3 +1,6 @@ +use std::fmt::Display; + +use poll_promise::Promise; use web_sys::FormData; use super::{Challenges, Languages}; @@ -14,6 +17,8 @@ pub struct Submission { pub binary: Option>, } +pub type SubmissionPromise = Option>>; + impl Submission { pub fn to_formdata(&self) -> FormData { let form = FormData::new().unwrap(); @@ -40,6 +45,27 @@ impl Submission { form } + + pub fn check_submit_promise(promise: &mut SubmissionPromise) -> SubmissionResult { + let mut result = SubmissionResult::NotStarted; + if let Some(p) = &promise { + result = SubmissionResult::Busy; + if let Some(response) = p.ready() { + match response { + Ok(submission_response) => { + result = submission_response.clone(); + } + Err(error) => { + result = SubmissionResult::Failure { + message: error.to_string(), + }; + } + } + *promise = None; + } + } + result + } } #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] @@ -50,3 +76,17 @@ pub enum SubmissionResult { NotAuthorized, Busy, } + +impl Display for SubmissionResult { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + SubmissionResult::NotStarted => write!(f, ""), + SubmissionResult::Success { score: _, message } => { + write!(f, "Success! {}", message) + } + SubmissionResult::Failure { message } => write!(f, "Failure: {}", message), + SubmissionResult::NotAuthorized => write!(f, "Not authorized"), + SubmissionResult::Busy => write!(f, "Busy"), + } + } +}