Separate components into their own modules

This commit is contained in:
Jeremy Wall 2022-02-02 16:29:27 -05:00
parent 58bd494368
commit cb641f448f
6 changed files with 88 additions and 5 deletions

17
web/src/components/mod.rs Normal file
View File

@ -0,0 +1,17 @@
// 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.
pub mod recipe;
pub mod root;
//mod recipe;
//mod menu;

View File

@ -0,0 +1,66 @@
// 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.
use crate::service::AppService;
use recipes;
use sycamore::{context::use_context, prelude::*};
#[component(Steps<G>)]
fn steps(steps: ReadSignal<Vec<recipes::Step>>) -> View<G> {
view! {
h2 { "Steps: " }
ul(class="recipe_steps") {
Indexed(IndexedProps{
iterable: steps,
template: |step: recipes::Step| { view! {
li {
//div() {}
div(class="instructions") {}
ul(class="ingredients") {
Indexed(IndexedProps{
iterable: Signal::new(step.ingredients).handle(),
template: |i| { view! {
li {
(i.amt) (i.name) (i.form.as_ref().map(|f| format!("({})", f)).unwrap_or(String::new()))
}
}}
})
}
}}
}
})
}
}
}
#[component(Recipe<G>)]
pub fn recipe(idx: ReadSignal<usize>) -> View<G> {
let app_service = use_context::<AppService>();
// TODO(jwall): This does unnecessary copies. Can we eliminate that?
let recipe = create_memo(move || app_service.get_recipes().get()[*idx.get()].1.clone());
let title = create_memo(cloned!((recipe) => move || recipe.get().title.clone()));
let desc = create_memo(
cloned!((recipe) => move || recipe.clone().get().desc.clone().unwrap_or_else(|| String::new())),
);
let steps = create_memo(cloned!((recipe) => move || recipe.get().steps.clone()));
view! {
div(class="recipe") {
h1(class="recipe_title") { (title.get()) }
div(class="recipe_description") {
(desc.get())
}
Steps(steps)
}
}
}

View File

@ -11,12 +11,11 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
mod typings; mod components;
mod service; mod service;
mod typings;
mod web; mod web;
mod root;
//mod recipe;
//mod menu;
use sycamore::prelude::*; use sycamore::prelude::*;
use web::UI; use web::UI;

View File

@ -20,6 +20,7 @@ use recipes::{parse, Recipe};
#[derive(Clone)] #[derive(Clone)]
pub struct AppService { pub struct AppService {
// TODO(jwall): Should each Recipe also be a Signal?
recipes: Signal<Vec<(usize, Recipe)>>, recipes: Signal<Vec<(usize, Recipe)>>,
} }

View File

@ -11,8 +11,8 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use crate::{components::root, service::AppService};
use crate::{console_debug, console_error, console_log}; use crate::{console_debug, console_error, console_log};
use crate::{root, service::AppService};
use sycamore::{ use sycamore::{
context::{ContextProvider, ContextProviderProps}, context::{ContextProvider, ContextProviderProps},