funkwhale/front/src/components/library/Radios.vue

281 wiersze
8.3 KiB
Vue
Czysty Zwykły widok Historia

2022-06-11 22:00:57 +00:00
<script setup lang="ts">
2022-07-10 23:16:41 +00:00
import type { RouteWithPreferences, OrderingField } from '~/store/ui'
import type { OrderingProps } from '~/composables/useOrdering'
import { onBeforeRouteUpdate, useRouter } from 'vue-router'
import { computed, ref, watch, onMounted } from 'vue'
import { useGettext } from 'vue3-gettext'
import { useStore } from '~/store'
2022-06-11 22:00:57 +00:00
import axios from 'axios'
import $ from 'jquery'
2022-06-11 22:00:57 +00:00
import Pagination from '~/components/vui/Pagination.vue'
import RadioCard from '~/components/radios/Card.vue'
2022-06-11 22:00:57 +00:00
import useSharedLabels from '~/composables/locale/useSharedLabels'
import useErrorHandler from '~/composables/useErrorHandler'
2022-07-10 23:16:41 +00:00
import useOrdering from '~/composables/useOrdering'
import useLogger from '~/composables/useLogger'
2022-06-11 22:00:57 +00:00
interface Props extends OrderingProps {
defaultPage?: number
defaultQuery?: string
scope?: string
2022-07-04 18:56:46 +00:00
// TODO(wvffle): Remove after https://github.com/vuejs/core/pull/4512 is merged
orderingConfigName: RouteWithPreferences | null
2022-06-11 22:00:57 +00:00
}
const props = withDefaults(defineProps<Props>(), {
defaultPage: 1,
defaultQuery: '',
scope: 'all'
})
const page = ref(+props.defaultPage)
type ResponseType = { count: number, results: any[] }
const result = ref<null | ResponseType>(null)
const query = ref(props.defaultQuery)
const orderingOptions: [OrderingField, keyof typeof sharedLabels.filters][] = [
['creation_date', 'creation_date'],
['name', 'name']
]
const logger = useLogger()
const sharedLabels = useSharedLabels()
const { onOrderingUpdate, orderingString, paginateBy, ordering, orderingDirection } = useOrdering(props.orderingConfigName)
2022-06-11 22:00:57 +00:00
const router = useRouter()
const updateQueryString = () => router.replace({
query: {
query: query.value,
page: page.value,
paginateBy: paginateBy.value,
ordering: orderingString.value
}
})
watch(page, updateQueryString)
onOrderingUpdate(updateQueryString)
const isLoading = ref(false)
const fetchData = async () => {
isLoading.value = true
const params = {
scope: props.scope,
page: page.value,
page_size: paginateBy.value,
name__icontains: query.value,
ordering: orderingString.value
}
logger.time('Fetching radios')
try {
const response = await axios.get('radios/radios/', {
params
})
result.value = response.data
} catch (error) {
useErrorHandler(error as Error)
2022-06-11 22:00:57 +00:00
result.value = null
} finally {
logger.timeEnd('Fetching radios')
isLoading.value = false
}
}
const store = useStore()
const isAuthenticated = computed(() => store.state.auth.authenticated)
const hasFavorites = computed(() => store.state.favorites.count > 0)
onBeforeRouteUpdate(fetchData)
fetchData()
onMounted(() => $('.ui.dropdown').dropdown())
const { $pgettext } = useGettext()
const labels = computed(() => ({
searchPlaceholder: $pgettext('Content/Search/Input.Placeholder', 'Enter a radio name…'),
title: $pgettext('*/*/*', 'Radios')
}))
</script>
2018-01-07 21:13:44 +00:00
<template>
<main v-title="labels.title">
<section class="ui vertical stripe segment">
2018-06-30 13:28:47 +00:00
<h2 class="ui header">
2021-12-06 10:35:20 +00:00
<translate translate-context="Content/Radio/Title">
Browsing radios
</translate>
2018-06-30 13:28:47 +00:00
</h2>
2021-12-06 10:35:20 +00:00
<div class="ui hidden divider" />
2018-07-17 11:09:13 +00:00
<div class="ui row">
<h3 class="ui header">
2021-12-06 10:35:20 +00:00
<translate translate-context="Content/Radio/Title">
Instance radios
</translate>
2018-07-17 11:09:13 +00:00
</h3>
<div class="ui cards">
2021-12-06 10:35:20 +00:00
<radio-card
v-if="isAuthenticated"
:type="'actor-content'"
:object-id="$store.state.auth.fullUsername"
/>
<radio-card
v-if="isAuthenticated && hasFavorites"
:type="'favorites'"
/>
<radio-card :type="'random'" />
<radio-card :type="'recently-added'" />
<radio-card
v-if="$store.state.auth.authenticated"
:type="'less-listened'"
/>
2018-07-17 11:09:13 +00:00
</div>
</div>
2021-12-06 10:35:20 +00:00
<div class="ui hidden divider" />
2018-07-17 11:09:13 +00:00
<h3 class="ui header">
2021-12-06 10:35:20 +00:00
<translate translate-context="Content/Radio/Title">
User radios
</translate>
2018-07-17 11:09:13 +00:00
</h3>
2021-12-06 10:35:20 +00:00
<router-link
v-if="isAuthenticated"
2021-12-06 10:35:20 +00:00
class="ui success button"
to="/library/radios/build"
>
<translate translate-context="Content/Radio/Button.Label/Verb">
Create your own radio
</translate>
2018-04-15 10:45:56 +00:00
</router-link>
2021-12-06 10:35:20 +00:00
<div class="ui hidden divider" />
<form
:class="['ui', {'loading': isLoading}, 'form']"
2022-06-11 22:00:57 +00:00
@submit.prevent="page = props.defaultPage"
2021-12-06 10:35:20 +00:00
>
2018-01-07 21:13:44 +00:00
<div class="fields">
<div class="field">
2020-08-01 09:11:51 +00:00
<label for="radios-search"><translate translate-context="Content/Search/Input.Label/Noun">Search</translate></label>
<div class="ui action input">
2021-12-06 10:35:20 +00:00
<input
id="radios-search"
v-model="query"
type="text"
name="search"
:placeholder="labels.searchPlaceholder"
>
<button
class="ui icon button"
type="submit"
:aria-label="$pgettext('Content/Search/Input.Label/Noun', 'Search')"
>
<i class="search icon" />
</button>
</div>
2018-01-07 21:13:44 +00:00
</div>
<div class="field">
2020-08-01 09:11:51 +00:00
<label for="radios-ordering"><translate translate-context="Content/Search/Dropdown.Label/Noun">Ordering</translate></label>
2021-12-06 10:35:20 +00:00
<select
id="radios-ordering"
v-model="ordering"
class="ui dropdown"
>
<option
v-for="(option, key) in orderingOptions"
:key="key"
:value="option[0]"
>
{{ sharedLabels.filters[option[1]] }}
2018-01-07 21:13:44 +00:00
</option>
</select>
</div>
<div class="field">
2020-08-01 09:11:51 +00:00
<label for="radios-ordering-direction"><translate translate-context="Content/Search/Dropdown.Label/Noun">Order</translate></label>
2021-12-06 10:35:20 +00:00
<select
id="radios-ordering-direction"
v-model="orderingDirection"
class="ui dropdown"
>
2018-06-30 13:28:47 +00:00
<option value="+">
2021-12-06 10:35:20 +00:00
<translate translate-context="Content/Search/Dropdown">
Ascending
</translate>
2018-06-30 13:28:47 +00:00
</option>
<option value="-">
2021-12-06 10:35:20 +00:00
<translate translate-context="Content/Search/Dropdown">
Descending
</translate>
2018-06-30 13:28:47 +00:00
</option>
2018-01-07 21:13:44 +00:00
</select>
</div>
<div class="field">
2020-08-01 09:11:51 +00:00
<label for="radios-results"><translate translate-context="Content/Search/Dropdown.Label/Noun">Results per page</translate></label>
2021-12-06 10:35:20 +00:00
<select
id="radios-results"
v-model="paginateBy"
class="ui dropdown"
>
2022-06-11 22:00:57 +00:00
<option :value="12">
2021-12-06 10:35:20 +00:00
12
</option>
2022-06-11 22:00:57 +00:00
<option :value="25">
2021-12-06 10:35:20 +00:00
25
</option>
2022-06-11 22:00:57 +00:00
<option :value="50">
2021-12-06 10:35:20 +00:00
50
</option>
2018-01-07 21:13:44 +00:00
</select>
</div>
</div>
</form>
2021-12-06 10:35:20 +00:00
<div class="ui hidden divider" />
<div
2022-07-01 12:21:57 +00:00
v-if="result && result.results.length === 0"
2021-12-06 10:35:20 +00:00
class="ui placeholder segment"
>
<div class="ui icon header">
2021-12-06 10:35:20 +00:00
<i class="feed icon" />
<translate translate-context="Content/Radios/Placeholder">
No results matching your query
</translate>
</div>
<router-link
2021-12-06 10:35:20 +00:00
v-if="$store.state.auth.authenticated"
:to="{name: 'library.radios.build'}"
class="ui success button labeled icon"
>
<i class="rss icon" />
<translate translate-context="Content/*/Verb">
Create a radio
</translate>
</router-link>
</div>
2018-02-26 19:10:35 +00:00
<div
v-if="result && result.results.length > 0"
2021-12-06 10:35:20 +00:00
class="ui cards"
>
<radio-card
v-for="radio in result.results"
:key="radio.id"
2021-12-06 10:35:20 +00:00
type="custom"
:custom-radio="radio"
/>
2018-01-07 21:13:44 +00:00
</div>
<div class="ui center aligned basic segment">
<pagination
v-if="result && result.count > paginateBy"
2022-06-11 22:00:57 +00:00
v-model:current="page"
2018-01-07 21:13:44 +00:00
:paginate-by="paginateBy"
:total="result.count"
2021-12-06 10:35:20 +00:00
/>
2018-01-07 21:13:44 +00:00
</div>
</section>
</main>
2018-01-07 21:13:44 +00:00
</template>