2022-08-30 17:31:33 -04:00
|
|
|
// Copyright 2022 Jeremy Wall (Jeremy@marzhilsltudios.com)
|
|
|
|
//
|
|
|
|
// 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.
|
2022-12-29 12:42:05 -06:00
|
|
|
use sycamore::futures::spawn_local_scoped;
|
|
|
|
use sycamore::prelude::*;
|
2022-12-22 10:49:50 -05:00
|
|
|
use tracing::{debug, info};
|
2022-08-30 17:31:33 -04:00
|
|
|
|
2022-12-27 23:00:48 -05:00
|
|
|
use crate::app_state::{Message, StateHandler};
|
2022-08-30 17:31:33 -04:00
|
|
|
|
2022-09-25 17:04:46 -04:00
|
|
|
#[component]
|
2022-12-27 23:00:48 -05:00
|
|
|
pub fn LoginForm<'ctx, G: Html>(cx: Scope<'ctx>, sh: StateHandler<'ctx>) -> View<G> {
|
2022-09-25 17:04:46 -04:00
|
|
|
let username = create_signal(cx, "".to_owned());
|
|
|
|
let password = create_signal(cx, "".to_owned());
|
|
|
|
view! {cx,
|
2022-08-30 17:31:33 -04:00
|
|
|
form() {
|
|
|
|
label(for="username") { "Username" }
|
2022-09-25 17:04:46 -04:00
|
|
|
input(type="text", id="username", bind:value=username)
|
2022-08-30 17:31:33 -04:00
|
|
|
label(for="password") { "Password" }
|
2022-09-25 17:04:46 -04:00
|
|
|
input(type="password", bind:value=password)
|
2023-12-02 15:28:08 -05:00
|
|
|
button(on:click=move |evt: web_sys::Event| {
|
2022-08-30 17:31:33 -04:00
|
|
|
info!("Attempting login request");
|
2022-12-29 12:42:05 -06:00
|
|
|
let (username, password) = ((*username.get_untracked()).clone(), (*password.get_untracked()).clone());
|
2023-12-02 15:28:08 -05:00
|
|
|
// NOTE(jwall): This is required if we want to keep the below auth request from
|
2023-12-04 14:48:50 -05:00
|
|
|
// failing to send with blocked by browser. This is because it's on a click and
|
|
|
|
// the form tries to do a submit event and aborts our network request.
|
2023-12-02 15:28:08 -05:00
|
|
|
evt.prevent_default();
|
2022-12-29 12:42:05 -06:00
|
|
|
if username != "" && password != "" {
|
|
|
|
spawn_local_scoped(cx, async move {
|
|
|
|
let store = crate::api::HttpStore::get_from_context(cx);
|
|
|
|
debug!("authenticating against ui");
|
|
|
|
if let Some(user_data) = store.authenticate(username, password).await {
|
|
|
|
sh.dispatch(cx, Message::SetUserData(user_data));
|
2023-01-02 20:48:31 -06:00
|
|
|
sh.dispatch(cx, Message::LoadState(Some(Box::new(|| sycamore_router::navigate("/ui/planning/plan")))));
|
2022-12-29 12:42:05 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-08-30 17:31:33 -04:00
|
|
|
debug!("triggering login click subscribers");
|
2023-01-07 16:21:06 -05:00
|
|
|
}) { "Login" } " "
|
2022-08-30 17:31:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 17:04:46 -04:00
|
|
|
#[component]
|
2022-12-26 21:56:28 -05:00
|
|
|
pub fn LoginPage<'ctx, G: Html>(cx: Scope<'ctx>, sh: StateHandler<'ctx>) -> View<G> {
|
2022-09-25 17:04:46 -04:00
|
|
|
view! {cx,
|
2022-12-27 23:00:48 -05:00
|
|
|
LoginForm(sh)
|
2022-08-30 17:31:33 -04:00
|
|
|
}
|
|
|
|
}
|