kitchen/web/src/web.rs

112 lines
3.4 KiB
Rust
Raw Normal View History

2022-01-13 18:07:01 -05:00
// Copyright 2022 Jeremy Wall
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2022-01-25 21:03:36 -05:00
use crate::{console_debug, console_error, console_log};
2022-01-23 14:28:01 -05:00
use reqwasm::http;
2022-01-25 20:32:17 -05:00
use sycamore::context::{use_context, ContextProvider, ContextProviderProps};
use sycamore::futures::spawn_local_in_scope;
use sycamore::prelude::*;
2022-01-23 14:28:01 -05:00
use recipes::{parse, Recipe};
2022-01-25 20:32:17 -05:00
#[derive(Clone)]
2022-01-24 19:59:16 -05:00
struct AppService {
2022-01-25 20:32:17 -05:00
recipes: Signal<Vec<Recipe>>,
2022-01-23 14:28:01 -05:00
}
2022-01-24 19:59:16 -05:00
impl AppService {
fn new() -> Self {
Self {
2022-01-25 20:32:17 -05:00
recipes: Signal::new(Vec::new()),
2022-01-24 19:59:16 -05:00
}
}
2022-01-23 14:28:01 -05:00
2022-01-24 19:59:16 -05:00
async fn fetch_recipes() -> Result<Vec<Recipe>, String> {
let resp = match http::Request::get("/api/v1/recipes").send().await {
Ok(resp) => resp,
Err(e) => return Err(format!("Error: {}", e)),
};
if resp.status() != 200 {
return Err(format!("Status: {}", resp.status()));
} else {
2022-01-25 21:03:36 -05:00
console_debug!("We got a valid response back!");
2022-01-24 19:59:16 -05:00
let recipe_list = match resp.json::<Vec<String>>().await {
Ok(recipes) => recipes,
Err(e) => return Err(format!("Eror getting recipe list as json {}", e)),
};
let mut parsed_list = Vec::new();
for r in recipe_list {
let recipe = match parse::as_recipe(&r) {
Ok(r) => r,
Err(e) => {
2022-01-25 21:03:36 -05:00
console_error!("Error parsing recipe {}", e);
2022-01-24 19:59:16 -05:00
break;
2022-01-23 14:28:01 -05:00
}
2022-01-24 19:59:16 -05:00
};
2022-01-25 21:03:36 -05:00
console_debug!("We parsed a recipe {}", recipe.title);
2022-01-24 19:59:16 -05:00
parsed_list.push(recipe);
2022-01-23 14:28:01 -05:00
}
2022-01-24 19:59:16 -05:00
return Ok(parsed_list);
2022-01-23 14:28:01 -05:00
}
2022-01-24 19:59:16 -05:00
}
2022-01-25 20:32:17 -05:00
fn get_recipes(&self) -> Signal<Vec<Recipe>> {
self.recipes.clone()
2022-01-24 19:59:16 -05:00
}
fn set_recipes(&mut self, recipes: Vec<Recipe>) {
2022-01-25 20:32:17 -05:00
self.recipes.set(recipes);
2022-01-24 19:59:16 -05:00
}
}
/// Component to list available recipes.
2022-01-25 20:32:17 -05:00
#[component(RecipeList<G>)]
fn recipe_list() -> View<G> {
2022-01-25 21:14:30 -05:00
let app_service = use_context::<AppService>();
2022-01-24 19:59:16 -05:00
2022-01-25 20:32:17 -05:00
view! {
2022-01-23 14:28:01 -05:00
ul {
2022-01-25 20:32:17 -05:00
Indexed(IndexedProps{
2022-01-25 21:14:30 -05:00
iterable: app_service.get_recipes().handle(),
2022-01-25 20:32:17 -05:00
template: |recipe| {
view! { li { (recipe.title) } }
}
})
2022-01-23 14:28:01 -05:00
}
2022-01-25 20:32:17 -05:00
}
2022-01-23 14:28:01 -05:00
}
2022-01-13 18:07:01 -05:00
2022-01-25 20:32:17 -05:00
#[component(UI<G>)]
pub fn ui() -> View<G> {
2022-01-25 21:14:30 -05:00
let app_service = AppService::new();
2022-01-25 21:03:36 -05:00
console_log!("Starting UI");
2022-01-25 20:32:17 -05:00
spawn_local_in_scope({
2022-01-25 21:14:30 -05:00
let mut app_service = app_service.clone();
2022-01-25 20:32:17 -05:00
async move {
match AppService::fetch_recipes().await {
Ok(recipes) => {
2022-01-25 21:14:30 -05:00
app_service.set_recipes(recipes);
2022-01-25 20:32:17 -05:00
}
2022-01-25 21:03:36 -05:00
Err(msg) => console_error!("Failed to get recipes {}", msg),
2022-01-24 19:59:16 -05:00
}
2022-01-25 20:32:17 -05:00
}
});
view! {
div { "hello chefs!" }
ContextProvider(ContextProviderProps {
2022-01-25 21:14:30 -05:00
value: app_service,
2022-01-25 20:32:17 -05:00
children: || view! { RecipeList() }
})
}
2022-01-23 14:28:01 -05:00
}