summary refs log tree commit diff stats
path: root/src/pages/create_product.rs
blob: fcd3b0ba8744d732919dc38ffb7c80ecc2ce94f0 (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
89
90
91
92
93
94
95
96
use std::{convert::Infallible, str::FromStr};

use leptos::{
    IntoView, component,
    prelude::{Get, Show, signal},
    task::spawn_local,
    view,
};
use rocie_client::models::{ProductStub, UnitPropertyId};
use rocie_macros::Form;
use uuid::Uuid;

use crate::{
    api::{get_config, register_product_external_wrapped, unit_properties_wrapped},
    components::{async_fetch::AsyncResource, banner::Banner, site_header::SiteHeader},
};

struct OptionalString(Option<String>);

impl FromStr for OptionalString {
    type Err = Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.is_empty() {
            Ok(Self(None))
        } else {
            Ok(Self(Some(s.to_owned())))
        }
    }
}

#[component]
pub fn CreateProduct() -> impl IntoView {
    let (error_message, error_message_set) = signal(None);

    view! {
        <SiteHeader logo=icondata_io::IoArrowBack back_location="/" name="Create Product" />

        <Show when=move || error_message.get().is_some()>
            <Banner text=move || error_message.get().expect("Is some") />
        </Show>

        {
            Form! {
                on_submit = |product_name, product_description, unit_property_id| {
                    let config = get_config!();

                    spawn_local(async move {
                        match register_product_external_wrapped(&config, ProductStub {
                                description: Some(product_description.0),
                                name: product_name,
                                parent: None, // TODO: Add this <2025-10-25>
                                unit_property: UnitPropertyId { value: unit_property_id },
                            }
                        ).await {
                            Ok(_id) => {}
                            Err(err) => error_message_set.set(Some(format!("Failed to create product: {err}"))),
                        }
                    });
                };

                <Input
                    name=product_name,
                    rust_type=String,
                    html_type="text",
                    label="Product Name",
                />

                <Input
                    name=product_description,
                    rust_type=OptionalString,
                    html_type="text",
                    label="Product Description"
                />

                <Select
                    name=unit_property_id,
                    rust_type=Uuid,
                    label="Unit property",
                    options=AsyncResource! {
                        () -> Result<Vec<(String, String)>, leptos::error::Error> {
                            let unit_properties = unit_properties_wrapped().await?;

                            Ok(
                                unit_properties
                                    .into_iter()
                                    .map(|prop| (prop.name, prop.id.to_string()))
                                    .collect()
                            )
                        }
                    },
                />
            }
        }
    }
}