Skip to content

Commit

Permalink
feat: allow admin to view hidden bars from the UI
Browse files Browse the repository at this point in the history
  • Loading branch information
SchutteJan committed Jul 29, 2024
1 parent a0d85a2 commit 7d19afe
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 20 deletions.
10 changes: 6 additions & 4 deletions frontend/src/api/bars.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { get_api_base_url } from './base'

export async function get_bars(): Promise<Response> {
return fetch(get_api_base_url() + '/bars', {
export async function get_bars(only_published: boolean = true): Promise<Response> {
const queryString = new URLSearchParams({ only_published: only_published.toString() }).toString()

return fetch(get_api_base_url() + '/bars?' + queryString, {
method: 'GET'
})
}
Expand All @@ -24,12 +26,12 @@ export async function deleteVisit(bar_id: number): Promise<Response> {
})
}

export async function hideBar(id: number): Promise<Response> {
export async function setPublished(id: number, published: boolean): Promise<Response> {
return fetch(get_api_base_url() + '/bar/' + id, {
method: 'PATCH',
headers: {
Accept: 'application/json'
},
body: JSON.stringify({ published: false })
body: JSON.stringify({ published: published })
})
}
27 changes: 16 additions & 11 deletions frontend/src/lib/BarItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
import type { LocationResponse, WhoResponse } from '../models/schemas'
import { localDate } from '$lib/time'
import { user } from '$lib/stores'
import { deleteVisit, hideBar, visitBar } from '../api/bars'
import { deleteVisit, setPublished, visitBar } from '../api/bars'
import Checkmark from './Checkmark.svelte'
import Externallink from './Externallink.svelte'
export let bar: LocationResponse
export let isLoggedIn: boolean = false
export let isAdmin: boolean = false
// TODO: Expose "published" on LocationResponse?
export let isHidden: boolean = false
user.subscribe((value: WhoResponse | undefined) => {
isLoggedIn = value !== undefined
Expand Down Expand Up @@ -41,11 +39,12 @@
})
}
function handleHideBar() {
if (confirm(`Are you sure you want to hide ${bar.name}?`)) {
hideBar(bar.id).then(() => {
console.log('Hid bar', bar)
isHidden = true
function toggleHideBar() {
const hideStr = bar.published ? 'hide' : 'unhide'
if (confirm(`Are you sure you want to ${hideStr} ${bar.name}?`)) {
setPublished(bar.id, !bar.published).then(() => {
bar.published = !bar.published
})
}
}
Expand All @@ -56,7 +55,7 @@
<summary class="bar-item">
<img alt={bar.name} class="bar-image" src={bar.imageurl ?? placeholder} />
<div class="bar-content">
{#if isHidden}
{#if !bar.published}
<h3><s>{bar.name}</s></h3>
{:else}
<h3>{bar.name}</h3>
Expand Down Expand Up @@ -91,8 +90,14 @@
>Open in Maps <Externallink />
</a>

{#if isLoggedIn && isAdmin && !isHidden}
<button class="pico-background-red" on:click={handleHideBar}>Hide</button>
{#if isLoggedIn && isAdmin}
<button class:pico-background-red={bar.published} on:click={toggleHideBar}>
{#if bar.published}
Hide
{:else}
Show
{/if}
</button>
{/if}

{#if isLoggedIn && bar.visited_at}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/models/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface LocationResponse {
id: number
imageurl?: string | null
name: string
published: boolean
visited_at?: string | null
}
export interface Login {
Expand Down
37 changes: 32 additions & 5 deletions frontend/src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,25 +1,52 @@
<script lang="ts">
import BarItem from '../lib/BarItem.svelte'
import BarItem from '$lib/BarItem.svelte'
import { user } from '$lib/stores'
import { onMount } from 'svelte'
import type { LocationResponse } from '../models/schemas'
import type { LocationResponse, UserRoleEnum } from '../models/schemas'
import { get_bars } from '../api/bars'
let bars: Array<LocationResponse> = []
let loading = true
let userRole: UserRoleEnum
let only_published = true
onMount(async () => {
get_bars()
user.subscribe((value) => {
if (value) {
userRole = value.role
}
})
function fetchBars() {
loading = true
get_bars(only_published)
.then((response) => response.json())
.then((data) => {
bars = data
loading = false
})
}
onMount(async () => {
fetchBars()
})
</script>

<section>
<h2>List</h2>
<p>All {bars.length == 0 ? '' : bars.length} bars in Amsterdam listed.</p>
<p>
All {bars.length == 0 ? '' : bars.length} bars in Amsterdam listed.
{#if userRole == 'Admin'}
<label>
<input
type="checkbox"
name="only_published"
bind:checked={only_published}
on:change={fetchBars}
/>
Show Only Published Bars
</label>
{/if}
</p>

<div class="bar-list-container">
{#if loading}
Expand Down

0 comments on commit 7d19afe

Please sign in to comment.