Skip to content

Commit

Permalink
Add TOML format to dashboard example
Browse files Browse the repository at this point in the history
  • Loading branch information
therustmonk committed May 5, 2018
1 parent 4884825 commit c4320fd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 23 deletions.
2 changes: 1 addition & 1 deletion examples/dashboard/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ authors = ["Denis Kolodin <deniskolodin@gmail.com>"]
failure = "0.1"
serde = "1"
serde_derive = "1"
yew = { path = "../.." }
yew = { path = "../..", features = ["toml"] }
70 changes: 48 additions & 22 deletions examples/dashboard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ extern crate yew;

use failure::Error;
use yew::prelude::*;
use yew::format::{Nothing, Json};
use yew::format::{Nothing, Json, Toml};
use yew::services::Task;
use yew::services::fetch::{FetchService, FetchTask, Request, Response};
use yew::services::websocket::{WebSocketService, WebSocketTask, WebSocketStatus};

type AsBinary = bool;

pub enum Format {
Json,
Toml,
}

pub struct Model {
fetching: bool,
data: Option<u32>,
Expand All @@ -28,7 +33,7 @@ pub enum WsAction {
}

pub enum Msg {
FetchData(AsBinary),
FetchData(Format, AsBinary),
WsAction(WsAction),
FetchReady(Result<DataFromFile, Error>),
WsReady(Result<WsResponse, Error>),
Expand Down Expand Up @@ -78,25 +83,45 @@ where

fn update(&mut self, msg: Self::Message, env: &mut Env<CTX, Self>) -> ShouldRender {
match msg {
Msg::FetchData(binary) => {
Msg::FetchData(format, binary) => {
self.fetching = true;
let callback = env.send_back(|response: Response<Json<Result<DataFromFile, Error>>>| {
let (meta, Json(data)) = response.into_parts();
println!("META: {:?}, {:?}", meta, data);
if meta.status.is_success() {
Msg::FetchReady(data)
} else {
Msg::Ignore // FIXME: Handle this error accordingly.
}
});
let request = Request::get("/data.json").body(Nothing).unwrap();
let fetch_service: &mut FetchService = env.as_mut();
let task = {
if binary {
fetch_service.fetch_binary(request, callback)
} else {
fetch_service.fetch(request, callback)
}
let task = match format {
Format::Json => {
let callback = env.send_back(move |response: Response<Json<Result<DataFromFile, Error>>>| {
let (meta, Json(data)) = response.into_parts();
println!("META: {:?}, {:?}", meta, data);
if meta.status.is_success() {
Msg::FetchReady(data)
} else {
Msg::Ignore // FIXME: Handle this error accordingly.
}
});
let request = Request::get("/data.json").body(Nothing).unwrap();
let fetch_service: &mut FetchService = env.as_mut();
if binary {
fetch_service.fetch_binary(request, callback)
} else {
fetch_service.fetch(request, callback)
}
},
Format::Toml => {
let callback = env.send_back(move |response: Response<Toml<Result<DataFromFile, Error>>>| {
let (meta, Toml(data)) = response.into_parts();
println!("META: {:?}, {:?}", meta, data);
if meta.status.is_success() {
Msg::FetchReady(data)
} else {
Msg::Ignore // FIXME: Handle this error accordingly.
}
});
let request = Request::get("/data.toml").body(Nothing).unwrap();
let fetch_service: &mut FetchService = env.as_mut();
if binary {
fetch_service.fetch_binary(request, callback)
} else {
fetch_service.fetch(request, callback)
}
},
};
self.ft = Some(task);
}
Expand Down Expand Up @@ -155,8 +180,9 @@ where
html! {
<div>
<nav class="menu",>
<button onclick=|_| Msg::FetchData(false),>{ "Fetch Data" }</button>
<button onclick=|_| Msg::FetchData(true),>{ "Fetch Data [binary]" }</button>
<button onclick=|_| Msg::FetchData(Format::Json, false),>{ "Fetch Data" }</button>
<button onclick=|_| Msg::FetchData(Format::Json, true),>{ "Fetch Data [binary]" }</button>
<button onclick=|_| Msg::FetchData(Format::Toml, false),>{ "Fetch Data [toml]" }</button>
{ self.view_data() }
<button disabled=self.ws.is_some(),
onclick=|_| WsAction::Connect.into(),>{ "Connect To WebSocket" }</button>
Expand Down
1 change: 1 addition & 0 deletions examples/dashboard/static/data.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
value = 567

0 comments on commit c4320fd

Please sign in to comment.