summary refs log tree commit diff stats
path: root/src/components/login_wall.rs
blob: 134e2788f690732497d5018a2e787e50b293d9e3 (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
// 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,
    error::Error,
    prelude::{Children, IntoAny},
    view,
};
use leptos_router::{NavigateOptions, hooks::use_navigate};

use crate::{
    api::{can_be_provisioned_wrapped, is_logged_in_wrapped},
    components::async_fetch::{AsyncFetch, AsyncResource},
};

#[component]
pub fn LoginWall(
    back: impl Fn() -> String + Send + Sync + 'static,
    children: Children,
) -> impl IntoView {
    view! {
        {
            AsyncFetch! {
                @map_error_in_producer
                from_resource = AsyncResource!(
                    () -> Result<(bool, bool), Error> {
                        Ok((can_be_provisioned_wrapped().await?, is_logged_in_wrapped().await?))
                    }
                ),
                producer = |(can_be_provisioned, is_logged_in)| {
                    if is_logged_in {
                        children()
                    } else if can_be_provisioned {
                        use_navigate()(format!("/provision/?back={}", back()).as_str(), NavigateOptions::default());
                        ().into_any()
                    } else {
                        use_navigate()(format!("/login/?back={}", back()).as_str(), NavigateOptions::default());
                        ().into_any()
                    }
                }
            }
        }
    }
}