Skip to content

Instantly share code, notes, and snippets.

@Ben-PH
Created August 4, 2020 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ben-PH/8fe0c4e8e9046428d978d285e8deb41d to your computer and use it in GitHub Desktop.
Save Ben-PH/8fe0c4e8e9046428d978d285e8deb41d to your computer and use it in GitHub Desktop.
why is line 47 requiring 'static?
use seed::{prelude::*, *};
use serde::{Deserialize, Serialize};
fn init(_: Url, orders: &mut impl Orders<Message>) -> Model {
log!("I N I T I A L I Z E");
// orders.subscribe(Message::UrlChanged);
// orders.send_msg(Message::GoToUrl(Url::new().set_path(&["login"])));
Model::default()
}
#[derive(Serialize, Deserialize)]
struct Login {
email: String,
password: String,
}
// ------ ------
// Model
// ------ ------
#[derive(Default)]
struct Model {
email: String,
password: String,
pub response_data: Option<String>,
}
enum Message{
EmailChanged(String),
PasswordChanged(String),
LoginButton,
LogoutButton,
CreateButton,
Fetched(fetch::Result<String>),
}
// ------ ------
// Update
// ------ ------
fn update(msg: Message, model: &mut Model, orders: &mut impl Orders<Message>) {
match msg {
Message::EmailChanged(email) => model.email = email,
Message::EmailChanged(password) => model.password = password,
Message::LoginButton => {
orders.perform_cmd(async { Message::Fetched(send_login(model.email, model.password).await)});
},
Message::Fetched(Ok(response_data)) => {
model.response_data = Some(response_data);
}
}
}
async fn send_logout(name: String) -> fetch::Result<String> {
Request::new(format!("/api/auth/{}", &name))
.method(fetch::Method::Delete)
.fetch()
.await?
.text()
.await
}
async fn send_login(email: String, password: String) -> fetch::Result<String> {
Request::new("/api/auth/login")
.method(fetch::Method::Post)
.json(&Login { email, password })?
.fetch()
.await?
.check_status()?
.text()
.await
}
async fn send_create(email: String, password: String) -> fetch::Result<String> {
Request::new("/api/auth/create")
.method(fetch::Method::Post)
.json(&Login { email, password })?
.fetch()
.await?
.text()
.await
}
// ------ ------
// View
// ------ ------
fn view(model: &Model) -> Vec<Node<Message>> {
nodes![
input![
input_ev(Ev::Input, |_| Message::EmailChanged),
],
input![
input_ev(Ev::Input, |_| Message::PasswordChanged),
],
button![ev(Ev::Click, |_| Message::LoginButton), "login"],
button![ev(Ev::Click, |_| Message::LogoutButton), "logout"],
button![ev(Ev::Click, |_| Message::CreateButton), "create"],
]
}
// ------ ------
#[wasm_bindgen(start)]
pub fn start() {
App::start("app", init, update, view);
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment