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-09-25 17:04:46 -04:00
|
|
|
use sycamore::{futures::spawn_local_scoped, prelude::*};
|
2022-12-22 10:49:50 -05:00
|
|
|
use tracing::{debug, info};
|
2022-08-30 17:31:33 -04:00
|
|
|
|
2022-12-26 21:56:28 -05:00
|
|
|
use crate::app_state::{self, StateHandler};
|
2022-08-30 17:31:33 -04:00
|
|
|
|
2022-09-25 17:04:46 -04:00
|
|
|
#[component]
|
|
|
|
pub fn LoginForm<G: Html>(cx: Scope) -> View<G> {
|
|
|
|
let username = create_signal(cx, "".to_owned());
|
|
|
|
let password = create_signal(cx, "".to_owned());
|
|
|
|
let clicked = create_signal(cx, ("".to_owned(), "".to_owned()));
|
|
|
|
create_effect(cx, move || {
|
2022-08-30 17:31:33 -04:00
|
|
|
let (username, password) = (*clicked.get()).clone();
|
|
|
|
if username != "" && password != "" {
|
2022-09-25 17:04:46 -04:00
|
|
|
spawn_local_scoped(cx, async move {
|
2022-12-22 10:49:50 -05:00
|
|
|
let state = app_state::State::get_from_context(cx);
|
|
|
|
let store = crate::api::HttpStore::get_from_context(cx);
|
2022-08-30 17:31:33 -04:00
|
|
|
debug!("authenticating against ui");
|
2022-08-30 17:31:35 -04:00
|
|
|
// TODO(jwall): Navigate to plan if the below is successful.
|
2022-12-22 10:49:50 -05:00
|
|
|
state.auth.set(store.authenticate(username, password).await);
|
2022-08-30 17:31:33 -04:00
|
|
|
});
|
|
|
|
}
|
2022-09-25 17:04:46 -04:00
|
|
|
});
|
|
|
|
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)
|
|
|
|
input(type="button", value="Login", on:click=move |_| {
|
2022-08-30 17:31:33 -04:00
|
|
|
info!("Attempting login request");
|
|
|
|
clicked.set(((*username.get_untracked()).clone(), (*password.get_untracked()).clone()));
|
|
|
|
debug!("triggering login click subscribers");
|
|
|
|
clicked.trigger_subscribers();
|
2022-09-25 17:04:46 -04:00
|
|
|
}) { }
|
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-10-30 18:39:05 -04:00
|
|
|
LoginForm()
|
2022-08-30 17:31:33 -04:00
|
|
|
}
|
|
|
|
}
|