summary refs log tree commit diff stats
path: root/src/pages/units.rs
blob: 96cc732da3607aeff052fcfe6baaff3f5b9e1bdc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// rocie - An enterprise grocery management system - Web app
//
// Copyright (C) 2026 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of Rocie.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.

use leptos::{
    IntoView, component,
    prelude::{ClassAttribute, CollectView, ElementChild},
    view,
};
use rocie_client::models::{Unit, UnitPropertyId};

use crate::{
    api::{unit_properties_wrapped, units_by_property_id_wrapped},
    components::{
        async_fetch::{AsyncFetch, AsyncResource},
        catch_errors::CatchErrors,
        login_wall::LoginWall,
        site_header::SiteHeader,
    },
};

#[component]
pub(crate) fn Units() -> impl IntoView {
    view! {
        <CatchErrors>
            <LoginWall back=move || "/units".to_owned()>
                <SiteHeader logo=icondata_io::IoArrowBack back_location="/" name="Units" />

                <ul class="flex flex-col gap-2 p-2 m-2">
                    {
                        AsyncFetch! {
                            @map_error_in_producer
                    fetcher = unit_properties_wrapped(),
                    producer = |unit_properties| {
                        unit_properties.into_iter().map(|unit_property| {
                            let resource = AsyncResource!{
                                (
                                    unit_property_name: String = unit_property.name.clone(),
                                    unit_property_id: UnitPropertyId = unit_property.id
                                ) -> Result<(Vec<Unit>, String), leptos::error::Error> {
                                    Ok(
                                        (
                                            units_by_property_id_wrapped(unit_property_id).await?,
                                            unit_property_name
                                        )
                                    )
                                }
                            };

                            AsyncFetch! {
                                @map_error_in_producer
                                from_resource = resource,
                                producer = |(units, unit_property_name)| {
                                    let units = units.into_iter().map(|unit| view!{
                                        <li>
                                            {format!("{} ({})", unit.full_name_singular, unit.short_name)}
                                        </li>
                                    }).collect::<Vec<_>>();


                                    view! {
                                        <li>
                                            <div class="bg-gray-200 p-1 rounded-lg">
                                                <p class="font-bold">{unit_property_name}</p>

                                                <ul class="ml-4">
                                                    {units}
                                                </ul>
                                            </div>
                                        </li>
                                    }
                                }
                            }
                        }).collect_view()
                    },
                        }
                    }
                </ul>
            </LoginWall>
        </CatchErrors>
    }
}