Fix more clippy issues, implement forgot password
All checks were successful
Build Crate / build (push) Successful in 1m48s

This commit is contained in:
2024-08-28 23:03:52 -04:00
parent 1faaf65aad
commit ed780c9585
15 changed files with 181 additions and 49 deletions

View File

@@ -8,10 +8,16 @@ cfg_if::cfg_if! {
if #[cfg(client)] {
use crate::{
state_enums::{ OpenState},
templates::{get_api_path},
global_state::{self, AppStateRx},
templates::get_api_path,
global_state::{AppStateRx},
endpoints::FORGOT_PASSWORD,
models::{
auth::ForgotPasswordRequest,
generic::GenericResponse,
},
};
use reqwest::StatusCode;
}
}
@@ -25,6 +31,7 @@ lazy_static! {
struct ForgotPasswordFormState {
username: String,
how_to_reach: String,
error: String,
}
impl ForgotPasswordFormStateRx {
@@ -32,6 +39,7 @@ impl ForgotPasswordFormStateRx {
fn reset(&self) {
self.username.set(String::new());
self.how_to_reach.set(String::new());
self.error.set(String::new());
}
}
@@ -49,7 +57,6 @@ fn forgot_password_form_capsule<G: Html>(
{
spawn_local_scoped(cx, async move {
let global_state = Reactor::<G>::from_cx(cx).get_global_state::<AppStateRx>(cx);
// Close modal
state.reset();
global_state
@@ -63,6 +70,26 @@ fn forgot_password_form_capsule<G: Html>(
#[cfg(client)]
{
spawn_local_scoped(cx, async move {
let request = ForgotPasswordRequest {
username: state.username.get().as_ref().clone(),
contact_info: state.how_to_reach.get().as_ref().clone(),
};
// // @todo clean up error handling
let client = reqwest::Client::new();
let response = client
.post(get_api_path(FORGOT_PASSWORD).as_str())
.json(&request)
.send()
.await
.unwrap();
let status = response.status();
let response_data = response.json::<GenericResponse>().await.unwrap();
if status != StatusCode::OK {
state.error.set(response_data.status);
return;
}
let global_state = Reactor::<G>::from_cx(cx).get_global_state::<AppStateRx>(cx);
// Close modal
@@ -81,11 +108,26 @@ fn forgot_password_form_capsule<G: Html>(
div (class="bg-white rounded-lg shadow relative dark:bg-gray-700"){
div (class="flex justify-end p-2"){
button (on:click = close_modal, class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 ml-auto inline-flex items-center dark:hover:bg-gray-800 dark:hover:text-white"){
"Back"
"Close"
}
}
div (class="space-y-6 px-6 lg:px-8 pb-4 sm:pb-6 xl:pb-8") {
h3 (class="text-xl font-medium text-gray-900 dark:text-white"){"Forgot Password"}
(match state.error.get().as_ref() != "" {
true => { view!{cx,
div (role="alert") {
div (class="bg-red-500 text-white font-bold rounded-t px-4 py-2") {
"Error"
}
div (class="border border-t-0 border-red-400 rounded-b bg-red-100 px-4 py-3 text-red-700"){
p {(state.error.get())}
}
}
}},
false => {view!{cx,}},
})
div {
label (class="text-sm font-medium text-gray-900 block mb-2 dark:text-gray-300") {"Username"}
input (bind:value = state.username, class="bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white") {}
@@ -113,7 +155,8 @@ pub fn get_capsule<G: Html>() -> Capsule<G, ForgotPasswordFormProps> {
#[engine_only_fn]
async fn get_build_state(_info: StateGeneratorInfo<()>) -> ForgotPasswordFormState {
ForgotPasswordFormState {
username: "".to_owned(),
how_to_reach: "".to_owned(),
username: String::new(),
how_to_reach: String::new(),
error: String::new(),
}
}