diff --git a/frontend/src/components/form.rs b/frontend/src/components/form.rs index d7dddec..c4fed7d 100644 --- a/frontend/src/components/form.rs +++ b/frontend/src/components/form.rs @@ -1,7 +1,10 @@ use serde::Serialize; +use wasm_bindgen::closure::Closure; +use wasm_bindgen::JsCast; +use web_sys::window; use yew::format::{Json, Nothing}; -use yew::prelude::*; use yew::services::fetch::{FetchService, FetchTask, Request, Response}; +use yew::{prelude::*, web_sys}; #[derive(Serialize)] struct FormData { @@ -18,6 +21,7 @@ pub struct Form { fetch_task: Option, loading: bool, validation_errors: ValidationErrors, + success_message: Option, } #[derive(Default)] @@ -33,6 +37,7 @@ pub enum Msg { UpdateContent(String), Submit, Response(Result<(), anyhow::Error>), + CloseSuccessMessage, } impl Component for Form { @@ -48,6 +53,7 @@ impl Component for Form { fetch_task: None, loading: false, validation_errors: ValidationErrors::default(), + success_message: None, } } @@ -97,7 +103,6 @@ impl Component for Form { FetchService::fetch(request, callback).expect("Failed to start request"); self.fetch_task = Some(task); } - true } Msg::Response(result) => { @@ -105,16 +110,39 @@ impl Component for Form { self.fetch_task = None; match result { Ok(_) => { - log::info!("Request successful"); self.name = String::new(); self.email = String::new(); self.content = String::new(); self.validation_errors = ValidationErrors::default(); + self.success_message = Some("문의가 성공적으로 접수되었습니다.".into()); + + if let Some(window) = window() { + let link = self.link.clone(); + let closure = Closure::wrap(Box::new(move || { + link.send_message(Msg::CloseSuccessMessage); + }) + as Box); + + window + .set_timeout_with_callback_and_timeout_and_arguments_0( + closure.as_ref().unchecked_ref(), + 3100, + ) + .unwrap(); + + closure.forget(); + } + } + Err(err) => { + log::error!("Request failed: {:?}", err); } - Err(err) => log::error!("Request failed: {:?}", err), } true } + Msg::CloseSuccessMessage => { + self.success_message = None; + true + } } } @@ -126,74 +154,88 @@ impl Component for Form { let is_disabled = self.loading || !self.is_valid(); html! { -
-
- - - { - if let Some(error) = &self.validation_errors.name { - html! {

{error}

} - } else { - html! {} +
+ { + if self.success_message.is_some() { + html! { +
+

{ self.success_message.as_ref().unwrap() }

+
} + } else { + html! {} } -
-
- - - { - if let Some(error) = &self.validation_errors.email { - html! {

{error}

} - } else { - html! {} + } + + +
+ + + { + if let Some(error) = &self.validation_errors.name { + html! {

{error}

} + } else { + html! {} + } } - } -
-
- -