Skip to content

Commit

Permalink
frontend: support archiving places
Browse files Browse the repository at this point in the history
  • Loading branch information
flosse committed Sep 21, 2023
1 parent 240b2b5 commit 5580662
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 12 deletions.
29 changes: 23 additions & 6 deletions ofdb-frontend-api/src/user.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand All @@ -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<D, T>(&self, req: RequestBuilder, data: &D) -> Result<T>
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<User> {
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<Vec<BboxSubscription>> {
Expand All @@ -46,15 +58,20 @@ 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 {
&self.token
}
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
}
}
7 changes: 6 additions & 1 deletion ofdb-frontend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,12 @@ pub fn App() -> impl IntoView {
/>
<Route
path=format!("{}/:id", Page::Entries.path())
view=move|| view! { <Entry public_api /> }
view=move|| view! {
<Entry
public_api
user_api = user_api.into()
/>
}
/>
<Route
path=Page::Events.path()
Expand Down
51 changes: 47 additions & 4 deletions ofdb-frontend/src/pages/entry.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use leptos::*;
use leptos_router::*;

use crate::api::PublicApi;
use ofdb_boundary::{Review, ReviewStatus};
use ofdb_frontend_api::{PublicApi, UserApi};

use crate::Page;

#[component]
pub fn Entry(public_api: PublicApi) -> impl IntoView {
pub fn Entry(public_api: PublicApi, user_api: Signal<Option<UserApi>>) -> impl IntoView {
// -- signals -- //

let params = use_params_map();
Expand Down Expand Up @@ -39,7 +42,7 @@ pub fn Entry(public_api: PublicApi) -> impl IntoView {

move || {
if let Some(entry) = entry.get() {
view! { <EntryProfile entry /> }.into_view()
view! { <EntryProfile entry user_api /> }.into_view()
} else {
view!{
<div class="mx-auto text-center max-w-7xl px-4 mt-12 pb-16 sm:px-6 sm:pb-24 lg:px-8">
Expand All @@ -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<Option<UserApi>>) -> impl IntoView {
let ofdb_boundary::Entry {
id,
title,
description,
image_url,
Expand All @@ -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! {
<div class="bg-white">
<div aria-hidden="true" class="relative">
Expand Down Expand Up @@ -119,6 +146,22 @@ fn EntryProfile(entry: ofdb_boundary::Entry) -> impl IntoView {
<dt class="font-medium text-gray-900">"Version"</dt>
<dd class="mt-2 text-sm text-gray-500">{ version }</dd>
</div>
{ move || user_api.get().map(|_|
view! {
<div class="border-t border-gray-200 pt-4">
<dt class="font-medium text-gray-900">"Actions"</dt>
<dd class="mt-2 text-sm text-gray-500">
<button
type="button"
class="rounded bg-indigo-600 px-2 py-1 text-xs font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
on:click=move|_|archive_place.dispatch(()) >
"Archive entry"
</button>
</dd>
</div>
}
)
}
</dl>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion ofdb-frontend/src/pages/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub fn Event(public_api: PublicApi, user_api: Signal<Option<UserApi>>) -> impl I
});

move || match fetch_event.value().get() {
Some(Ok(event)) => view! { <EventProfile event user_api = user_api /> }.into_view(),
Some(Ok(event)) => view! { <EventProfile event user_api /> }.into_view(),
None => view! { <p>"The event is loaded ..."</p> }.into_view(),
Some(Err(_)) => view! { <p>"An error occurred while loading the event."</p> }.into_view(),
}
Expand Down

0 comments on commit 5580662

Please sign in to comment.