Skip to content

Commit

Permalink
Refactors code editor for better reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbrain-za committed Nov 5, 2023
1 parent fbec3bf commit 7376757
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 51 deletions.
69 changes: 18 additions & 51 deletions src/apps/code_editor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::helpers::{
refresh,
submission::{Submission, SubmissionResult},
submission::{Submission, SubmissionPromise, SubmissionResult},
Challenges, Languages,
};
use gloo_net::http;
Expand All @@ -11,19 +11,19 @@ use web_sys::RequestCredentials;
#[serde(default)]
pub struct CodeEditor {
#[serde(skip)]
promise: Option<Promise<Result<SubmissionResult, String>>>,
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<String>,
}

impl Default for CodeEditor {
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
}
}
}

Expand Down Expand Up @@ -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()));
});
}
}
40 changes: 40 additions & 0 deletions src/helpers/submission.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::fmt::Display;

use poll_promise::Promise;
use web_sys::FormData;

use super::{Challenges, Languages};
Expand All @@ -14,6 +17,8 @@ pub struct Submission {
pub binary: Option<Vec<u8>>,
}

pub type SubmissionPromise = Option<Promise<Result<SubmissionResult, String>>>;

impl Submission {
pub fn to_formdata(&self) -> FormData {
let form = FormData::new().unwrap();
Expand All @@ -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)]
Expand All @@ -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"),
}
}
}

0 comments on commit 7376757

Please sign in to comment.