// rocie - An enterprise grocery management system - Web app // // Copyright (C) 2026 Benedikt Peetz // 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 . use leptos::{ IntoView, component, prelude::{Get, Show, signal}, task::spawn_local, view, }; use leptos_router::{ NavigateOptions, hooks::{use_navigate, use_query_map}, }; use rocie_client::models::{ProvisionInfo, UserStub}; use rocie_macros::Form; use crate::{ api::{get_config, provision_external_wrapped}, components::{banner::Banner, catch_errors::CatchErrors, site_header::SiteHeader}, }; #[component] pub fn Provision() -> impl IntoView { let back = || { let back = use_query_map() .get() .get("back") .expect("Should always have a back, because the router would otherwise not match"); if back.starts_with('/') { back } else { // Prevent a redirect like `/provision?back=https://gnu.org` to work "/".to_owned() } }; let (error_message, error_message_set) = signal(None); view! { { Form! { on_submit = |user_name, password, should_use_defaults| { let config = get_config!(); let navigate = use_navigate(); let back = back(); spawn_local(async move { match provision_external_wrapped( &config, ProvisionInfo { // TODO: Make it possible to give this user a description <2025-12-30> user: UserStub { description: None, name: user_name, password }, use_defaults: should_use_defaults } ).await { Ok(_) => { navigate(back.as_str(), NavigateOptions::default()); } Err(err) => error_message_set.set(Some(format!("Failed to provision: {err}"))), } }); }; } } } }