diff --git a/ofdb-frontend-api/src/user.rs b/ofdb-frontend-api/src/user.rs index e2060491..e8b99581 100644 --- a/ofdb-frontend-api/src/user.rs +++ b/ofdb-frontend-api/src/user.rs @@ -1,5 +1,5 @@ use gloo_net::http::{Request, RequestBuilder}; -use serde::de::DeserializeOwned; +use serde::{de::DeserializeOwned, Serialize}; use web_sys::RequestCredentials; use ofdb_boundary::*; @@ -24,16 +24,28 @@ impl UserApi { where T: DeserializeOwned, { - let response = req - .header("Authorization", &self.auth_header_value()) + let response = self + .add_auth_headers(req) .header("Content-Type", "application/json") .send() .await?; into_json(response).await } + async fn send_json(&self, req: RequestBuilder, data: &D) -> Result + where + T: DeserializeOwned, + D: Serialize, + { + let response = self.add_auth_headers(req).json(data)?.send().await?; + into_json(response).await + } + fn add_auth_headers(&self, req: RequestBuilder) -> RequestBuilder { + req.header("Authorization", &self.auth_header_value()) + .credentials(RequestCredentials::Include) + } pub async fn user_info(&self) -> Result { let url = format!("{}/users/current", self.url); - let request = Request::get(&url).credentials(RequestCredentials::Include); + let request = Request::get(&url); self.send(request).await } pub async fn bbox_subscriptions(&self) -> Result> { @@ -46,7 +58,7 @@ impl UserApi { } pub async fn logout(&self) -> Result<()> { let url = format!("{}/logout", self.url); - let request = Request::post(&url).credentials(RequestCredentials::Include); + let request = Request::post(&url); self.send(request).await } pub fn token(&self) -> &JwtToken { @@ -54,7 +66,12 @@ impl UserApi { } pub async fn archive_events(&self, ids: &[&str]) -> Result<()> { let url = format!("{}/events/{}/archive", self.url, ids.join(",")); - let request = Request::post(&url).credentials(RequestCredentials::Include); + let request = Request::post(&url); self.send(request).await } + pub async fn review_places(&self, ids: &[&str], review: &Review) -> Result<()> { + let url = format!("{}/places/{}/review", self.url, ids.join(",")); + let request = Request::post(&url); + self.send_json(request, review).await + } } diff --git a/ofdb-frontend/src/lib.rs b/ofdb-frontend/src/lib.rs index 9cbb6f9a..222e63b4 100644 --- a/ofdb-frontend/src/lib.rs +++ b/ofdb-frontend/src/lib.rs @@ -151,7 +151,12 @@ pub fn App() -> impl IntoView { /> } + view=move|| view! { + + } /> impl IntoView { +pub fn Entry(public_api: PublicApi, user_api: Signal>) -> impl IntoView { // -- signals -- // let params = use_params_map(); @@ -39,7 +42,7 @@ pub fn Entry(public_api: PublicApi) -> impl IntoView { move || { if let Some(entry) = entry.get() { - view! { }.into_view() + view! { }.into_view() } else { view!{
@@ -51,8 +54,9 @@ pub fn Entry(public_api: PublicApi) -> impl IntoView { } #[component] -fn EntryProfile(entry: ofdb_boundary::Entry) -> impl IntoView { +fn EntryProfile(entry: ofdb_boundary::Entry, user_api: Signal>) -> impl IntoView { let ofdb_boundary::Entry { + id, title, description, image_url, @@ -69,6 +73,29 @@ fn EntryProfile(entry: ofdb_boundary::Entry) -> impl IntoView { .. } = entry; + let archive_place = create_action(move |_| { + let id = id.clone(); + let navigate = use_navigate(); + async move { + let Some(user_api) = user_api.get() else { + unreachable!(); + }; + let review = Review { + status: ReviewStatus::Archived, + comment: None, + }; + match user_api.review_places(&[&id], &review).await { + Ok(_) => { + log::info!("Successfully archived entry {id}"); + navigate(Page::Home.path(), Default::default()); + } + Err(err) => { + log::error!("Unable to archive entry {id}: {err}"); + } + } + } + }); + view! {
+ { move || user_api.get().map(|_| + view! { +
+
"Actions"
+
+ +
+
+ } + ) + }
diff --git a/ofdb-frontend/src/pages/event.rs b/ofdb-frontend/src/pages/event.rs index 153c3752..adc8b040 100644 --- a/ofdb-frontend/src/pages/event.rs +++ b/ofdb-frontend/src/pages/event.rs @@ -113,7 +113,7 @@ pub fn Event(public_api: PublicApi, user_api: Signal>) -> impl I }); move || match fetch_event.value().get() { - Some(Ok(event)) => view! { }.into_view(), + Some(Ok(event)) => view! { }.into_view(), None => view! {

"The event is loaded ..."

}.into_view(), Some(Err(_)) => view! {

"An error occurred while loading the event."

}.into_view(), }