use crate::{ components::router::{AppRoute, NavButton}, infra::{ api::HostService, common_component::{CommonComponent, CommonComponentParts}, }, }; use anyhow::{bail, Result}; use validator_derive::Validate; use yew::prelude::*; use yew_form::Form; use yew_form_derive::Model; pub struct ResetPasswordStep1Form { common: CommonComponentParts, form: Form, just_succeeded: bool, } /// The fields of the form, with the constraints. #[derive(Model, Validate, PartialEq, Clone, Default)] pub struct FormModel { #[validate(length(min = 1, message = "Missing username"))] username: String, } pub enum Msg { Update, Submit, PasswordResetResponse(Result<()>), } impl CommonComponent for ResetPasswordStep1Form { fn handle_msg(&mut self, msg: ::Message) -> Result { match msg { Msg::Update => Ok(true), Msg::Submit => { if !self.form.validate() { bail!("Check the form for errors"); } let FormModel { username } = self.form.model(); self.common.call_backend( HostService::reset_password_step1, &username, Msg::PasswordResetResponse, )?; Ok(true) } Msg::PasswordResetResponse(response) => { response?; self.just_succeeded = true; Ok(true) } } } fn mut_common(&mut self) -> &mut CommonComponentParts { &mut self.common } } impl Component for ResetPasswordStep1Form { type Message = Msg; type Properties = (); fn create(props: Self::Properties, link: ComponentLink) -> Self { ResetPasswordStep1Form { common: CommonComponentParts::::create(props, link), form: Form::::new(FormModel::default()), just_succeeded: false, } } fn update(&mut self, msg: Self::Message) -> ShouldRender { self.just_succeeded = false; CommonComponentParts::::update(self, msg) } fn change(&mut self, _: Self::Properties) -> ShouldRender { false } fn view(&self) -> Html { type Field = yew_form::Field; html! {
{ if self.just_succeeded { html! { {"A reset token has been sent to your email."} } } else { html! {
{"Back"}
} }}
{ if let Some(e) = &self.common.error { html! {
{e.to_string() }
} } else { html! {} } }
} } }