Skip to content

Commit

Permalink
webapp: move editable_text
Browse files Browse the repository at this point in the history
  • Loading branch information
pnmadelaine committed Sep 26, 2023
1 parent 296df81 commit af1ad52
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 116 deletions.
113 changes: 113 additions & 0 deletions typhon-webapp/src/editable_text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use seed::{prelude::*, *};

#[derive(Clone)]
enum State {
Read,
Edit(String),
Sync(String),
}

pub struct Model {
pub text: String,
state: State,
}

#[derive(Clone, Debug)]
pub enum Msg {
Update(String),
Send,
Edit,
Synchronized,
}

#[derive(Clone)]
pub enum OutMsg {
NewValue(String),
}

pub fn value_synchronized() -> Msg {
Msg::Synchronized
}

pub fn init(text: String) -> Model {
Model {
state: State::Read,
text,
}
}

pub fn update(msg: Msg, model: &mut Model) -> Option<OutMsg> {
match (msg, &mut *model) {
(
Msg::Update(text),
Model {
state: State::Edit(_),
..
},
) => {
model.state = State::Edit(text);
None
}
(
Msg::Send,
Model {
state: State::Edit(text),
..
},
) => {
let text = text.clone();
model.state = State::Sync(text.clone());
Some(OutMsg::NewValue(text))
}
(Msg::Edit, _) => {
model.state = State::Edit(model.text.clone());
None
}
(
Msg::Synchronized,
Model {
state: State::Sync(text),
..
},
) => {
model.text = text.clone();
model.state = State::Read;
None
}
_ => None,
}
}

pub fn view(model: &Model, wrap: Box<dyn FnOnce(String) -> Node<Msg>>) -> Node<Msg> {
match &model.state {
State::Read => div![
wrap(model.text.clone()),
i![C!["ri-pencil-line"], ev(Ev::Click, |_| Msg::Edit)],
C!["editable-text", "read"],
if model.text.trim().is_empty() {
vec![C!["empty"]]
} else {
vec![]
}
],
State::Edit(text) => div![
input![
attrs! {At::Value => text},
input_ev(Ev::Input, Msg::Update),
keyboard_ev(Ev::KeyUp, |e| {
if e.key() == "Enter" {
Some(Msg::Send)
} else {
None
}
})
],
i![C!["ri-check-line"], ev(Ev::Click, |_| Msg::Send)],
C!["editable-text", "edit"]
],
State::Sync(text) => div![
input![attrs! {At::Value => text, At::Disabled => true}],
C!["editable-text", "sync"]
],
}
}
1 change: 1 addition & 0 deletions typhon-webapp/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod appurl;
mod build;
mod editable_text;
mod evaluation;
mod home;
mod job;
Expand Down
119 changes: 3 additions & 116 deletions typhon-webapp/src/project.rs
Original file line number Diff line number Diff line change
@@ -1,122 +1,9 @@
use crate::editable_text;
use crate::{appurl::AppUrl, perform_request, view_error, Urls};
use seed::{prelude::*, *};
use typhon_types::*;

mod editable_text {
use seed::{prelude::*, *};

#[derive(Clone)]
enum State {
Read,
Edit(String),
Sync(String),
}

pub struct Model {
pub text: String,
state: State,
}

#[derive(Clone, Debug)]
pub enum Msg {
Update(String),
Send,
Edit,
Synchronized,
}

#[derive(Clone)]
pub enum OutMsg {
NewValue(String),
}

pub fn value_synchronized() -> Msg {
Msg::Synchronized
}

pub fn init(text: String) -> Model {
Model {
state: State::Read,
text,
}
}

pub fn update(msg: Msg, model: &mut Model) -> Option<OutMsg> {
match (msg, &mut *model) {
(
Msg::Update(text),
Model {
state: State::Edit(_),
..
},
) => {
model.state = State::Edit(text);
None
}
(
Msg::Send,
Model {
state: State::Edit(text),
..
},
) => {
let text = text.clone();
model.state = State::Sync(text.clone());
Some(OutMsg::NewValue(text))
}
(Msg::Edit, _) => {
model.state = State::Edit(model.text.clone());
None
}
(
Msg::Synchronized,
Model {
state: State::Sync(text),
..
},
) => {
model.text = text.clone();
model.state = State::Read;
None
}
_ => None,
}
}
use seed::{prelude::*, *};

pub fn view(model: &Model, wrap: Box<dyn FnOnce(String) -> Node<Msg>>) -> Node<Msg> {
match &model.state {
State::Read => div![
wrap(model.text.clone()),
i![C!["ri-pencil-line"], ev(Ev::Click, |_| Msg::Edit)],
C!["editable-text", "read"],
if model.text.trim().is_empty() {
vec![C!["empty"]]
} else {
vec![]
}
],
State::Edit(text) => div![
input![
attrs! {At::Value => text},
input_ev(Ev::Input, Msg::Update),
keyboard_ev(Ev::KeyUp, |e| {
if e.key() == "Enter" {
Some(Msg::Send)
} else {
None
}
})
],
i![C!["ri-check-line"], ev(Ev::Click, |_| Msg::Send)],
C!["editable-text", "edit"]
],
State::Sync(text) => div![
input![attrs! {At::Value => text, At::Disabled => true}],
C!["editable-text", "sync"]
],
}
}
}
use typhon_types::*;

pub struct Model {
error: Option<responses::ResponseError>,
Expand Down

0 comments on commit af1ad52

Please sign in to comment.