feat(front): full search form on channels list

This commit is contained in:
ArneBo 2025-02-19 17:30:02 +01:00
parent d74a8f637c
commit 32a1112a39
3 changed files with 190 additions and 45 deletions

View File

@ -1,7 +1,8 @@
<script setup lang="ts"> <script setup lang="ts">
import type { BackendError, BackendResponse, Channel } from '~/types' import type { BackendError, PaginatedChannelList } from '~/types'
import { type operations } from '~/generated/types.ts'
import { ref, onMounted, watch, reactive } from 'vue' import { ref, onMounted, watch } from 'vue'
import { clone } from 'lodash-es' import { clone } from 'lodash-es'
import axios from 'axios' import axios from 'axios'
@ -14,7 +15,7 @@ import Section from '~/components/ui/Section.vue'
import Pagination from '~/components/ui/Pagination.vue' import Pagination from '~/components/ui/Pagination.vue'
interface Events { interface Events {
(e: 'fetched', channels: BackendResponse<Channel>): void (e: 'fetched', channels: PaginatedChannelList): void
} }
interface Props { interface Props {
@ -28,7 +29,7 @@ const props = withDefaults(defineProps<Props>(), {
limit: 5, limit: 5,
}) })
const channels = reactive([] as Channel[]) const result = ref<PaginatedChannelList>()
const errors = ref([] as string[]) const errors = ref([] as string[])
const nextPage = ref() const nextPage = ref()
const page = usePage() const page = usePage()
@ -39,17 +40,17 @@ const isLoading = ref(false)
const fetchData = async (url = 'channels/') => { const fetchData = async (url = 'channels/') => {
isLoading.value = true isLoading.value = true
const params = { const params: operations['get_channels_2']['parameters']['query'] = {
...clone(props.filters), ...clone(props.filters),
page: page.value, page: page.value,
page_size: props.limit page_size: props.limit,
} }
try { try {
const response = await axios.get(url, { params }) const response = await axios.get<PaginatedChannelList>(url, { params })
nextPage.value = response.data.next nextPage.value = response.data.next
count.value = response.data.count count.value = response.data.count
channels.splice(0, channels.length, ...response.data.results) result.value = response.data
emit('fetched', response.data) emit('fetched', response.data)
} catch (error) { } catch (error) {
errors.value = (error as BackendError).backendErrors errors.value = (error as BackendError).backendErrors
@ -72,11 +73,11 @@ watch([() => props.filters, page],
<Section <Section
align-left align-left
small-items small-items
:h2="title" :h2="title || undefined"
> >
<Loader v-if="isLoading" style="grid-column: 1 / -1;" /> <Loader v-if="isLoading" style="grid-column: 1 / -1;" />
<template <template
v-if="!isLoading && channels.length === 0" v-if="!isLoading && result?.count === 0"
> >
<empty-state <empty-state
:refresh="true" :refresh="true"
@ -84,13 +85,19 @@ watch([() => props.filters, page],
style="grid-column: 1 / -1;" style="grid-column: 1 / -1;"
/> />
</template> </template>
<Pagination
v-if="result && count > limit && limit > 16"
v-model:page="page"
:pages="Math.ceil((count || 0) / limit)"
style="grid-column: 1 / -1;"
/>
<channel-card <channel-card
v-for="object in channels" v-for="channel in result?.results"
:key="object.uuid" :key="channel.uuid"
:object="object" :object="channel"
/> />
<Pagination <Pagination
v-if="channels && count > limit" v-if="result && count > limit"
v-model:page="page" v-model:page="page"
:pages="Math.ceil((count || 0) / limit)" :pages="Math.ceil((count || 0) / limit)"
style="grid-column: 1 / -1;" style="grid-column: 1 / -1;"

View File

@ -68,6 +68,7 @@ export type LibraryFollow = components["schemas"]["LibraryFollow"]
export type Cover = components["schemas"]["CoverField"] export type Cover = components["schemas"]["CoverField"]
export type RateLimitStatus = components["schemas"]["RateLimit"]['scopes'][number] export type RateLimitStatus = components["schemas"]["RateLimit"]['scopes'][number]
export type PaginatedAlbumList = components["schemas"]["PaginatedAlbumList"] export type PaginatedAlbumList = components["schemas"]["PaginatedAlbumList"]
export type PaginatedChannelList = components["schemas"]["PaginatedChannelList"]
export type SimpleArtist = components["schemas"]["SimpleArtist"] export type SimpleArtist = components["schemas"]["SimpleArtist"]
export type Artist = components['schemas']['SimpleArtist'] export type Artist = components['schemas']['SimpleArtist']

View File

@ -1,12 +1,26 @@
<script setup lang="ts"> <script setup lang="ts">
import type { Channel } from '~/types' import type { OrderingProps } from '~/composables/navigation/useOrdering'
import type { PaginatedChannelList } from '~/types'
import { type operations } from '~/generated/types.ts'
import type { RouteRecordName } from 'vue-router'
import type { OrderingField } from '~/store/ui'
import { computed, ref, watch } from 'vue'
import { useRouteQuery } from '@vueuse/router'
import { useI18n } from 'vue-i18n' import { useI18n } from 'vue-i18n'
import { ref, computed } from 'vue' import { syncRef } from '@vueuse/core'
import { sortedUniq } from 'lodash-es'
import { useRouter } from 'vue-router' import { useRouter } from 'vue-router'
import { useStore } from '~/store'
import axios from 'axios' import axios from 'axios'
import useSharedLabels from '~/composables/locale/useSharedLabels'
import usePage from '~/composables/navigation/usePage'
import useErrorHandler from '~/composables/useErrorHandler'
import useOrdering from '~/composables/navigation/useOrdering'
import useLogger from '~/composables/useLogger'
import ChannelsWidget from '~/components/audio/ChannelsWidget.vue' import ChannelsWidget from '~/components/audio/ChannelsWidget.vue'
import RemoteSearchForm from '~/components/RemoteSearchForm.vue' import RemoteSearchForm from '~/components/RemoteSearchForm.vue'
import ChannelForm from '~/components/audio/ChannelForm.vue' import ChannelForm from '~/components/audio/ChannelForm.vue'
@ -14,19 +28,37 @@ import Layout from '~/components/ui/Layout.vue'
import Header from '~/components/ui/Header.vue' import Header from '~/components/ui/Header.vue'
import Modal from '~/components/ui/Modal.vue' import Modal from '~/components/ui/Modal.vue'
import Button from '~/components/ui/Button.vue' import Button from '~/components/ui/Button.vue'
import Input from '~/components/ui/Input.vue'
import Pills from '~/components/ui/Pills.vue'
import Spacer from '~/components/ui/Spacer.vue' import Spacer from '~/components/ui/Spacer.vue'
import Pagination from '~/components/ui/Pagination.vue'
import useErrorHandler from '~/composables/useErrorHandler' interface Props extends OrderingProps {
scope?: 'me' | 'all'
interface Props { // TODO(wvffle): Remove after https://github.com/vuejs/core/pull/4512 is merged
orderingConfigName?: RouteRecordName
defaultQuery?: string defaultQuery?: string
} }
const props = withDefaults(defineProps<Props>(), { const props = withDefaults(defineProps<Props>(), {
scope: 'all',
orderingConfigName: undefined,
defaultQuery: '' defaultQuery: ''
}) })
const query = ref(props.defaultQuery) const subscribedQuery = ref(props.defaultQuery)
const q = useRouteQuery('query', '')
const query = ref(q.value ?? '')
syncRef(q, query, { direction: 'ltr' })
const result = ref<PaginatedChannelList>()
const orderingOptions: [OrderingField, keyof typeof sharedLabels.filters][] = [
['creation_date', 'creation_date'],
['modification_date', 'modification_date']
]
const widgetKey = ref(new Date().toLocaleString()) const widgetKey = ref(new Date().toLocaleString())
const { t } = useI18n() const { t } = useI18n()
@ -35,35 +67,74 @@ const labels = computed(() => ({
})) }))
const router = useRouter() const router = useRouter()
const logger = useLogger()
const sharedLabels = useSharedLabels()
const step = ref(1) const step = ref(1)
const submittable = ref(false) const submittable = ref(false)
const category = ref('podcast') const category = ref('podcast')
const modalContent = ref() const modalContent = ref()
const createForm = ref() const createForm = ref()
const previousPage = ref() const page = usePage()
const nextPage = ref() const tags = useRouteQuery<string[]>('tag', [])
const channels = ref([] as Channel[]) const tagList = computed(() => ({
current: [],
others: tags.value
}))
const { onOrderingUpdate, orderingString, paginateBy, ordering, orderingDirection } = useOrdering(props)
const count = ref(0) const count = ref(0)
const isLoading = ref(false) const isLoading = ref(false)
const fetchData = async () => { const fetchData = async () => {
isLoading.value = true isLoading.value = true
const params : operations['get_channels_2']['parameters']['query'] = {
try { scope: props.scope,
const response = await axios.get('channels/', { params: { q: query.value } }) page: page.value,
previousPage.value = response.data.previous page_size: paginateBy.value,
nextPage.value = response.data.next q: query.value,
channels.value.push(...response.data.results) ordering: orderingString.value,
count.value = response.data.count tag: tags.value,
} catch (error) {
useErrorHandler(error as Error)
} }
const measureLoading = logger.time('Fetching channels')
try {
const response = await axios.get<PaginatedChannelList>('channels/', {
params,
paramsSerializer: {
indexes: null
}
})
count.value = response.data.count
result.value = response.data
} catch (error) {
useErrorHandler(error as Error)
result.value = undefined
} finally {
measureLoading()
isLoading.value = false isLoading.value = false
}
} }
fetchData()
const store = useStore()
watch(() => store.state.moderation.lastUpdate, fetchData)
const reloadWidget = () => (widgetKey.value = new Date().toLocaleString()) const reloadWidget = () => (widgetKey.value = new Date().toLocaleString())
watch([page, tags, q], fetchData)
const search = () => {
page.value = 1
q.value = query.value
}
onOrderingUpdate(() => {
page.value = 1
fetchData()
})
const paginateOptions = computed(() => sortedUniq([12, 30, 50, paginateBy.value].sort((a, b) => a - b)))
const showSubscribeModal = ref(false) const showSubscribeModal = ref(false)
const showCreateModal = ref(false) const showCreateModal = ref(false)
</script> </script>
@ -83,7 +154,6 @@ const showCreateModal = ref(false)
<Modal <Modal
v-model="showSubscribeModal" v-model="showSubscribeModal"
:title="t('views.channels.SubscriptionsList.modal.subscription.header')" :title="t('views.channels.SubscriptionsList.modal.subscription.header')"
class="tiny"
> >
<div <div
ref="modalContent" ref="modalContent"
@ -116,7 +186,7 @@ const showCreateModal = ref(false)
</Modal> </Modal>
<inline-search-bar <inline-search-bar
v-model="query" v-model="subscribedQuery"
:placeholder="labels.searchPlaceholder" :placeholder="labels.searchPlaceholder"
@search="reloadWidget" @search="reloadWidget"
/> />
@ -124,7 +194,7 @@ const showCreateModal = ref(false)
:key="widgetKey" :key="widgetKey"
:limit="4" :limit="4"
:show-modification-date="true" :show-modification-date="true"
:filters="{q: query, subscribed: 'true'}" :filters="{q: subscribedQuery, subscribed: 'true'}"
/> />
<!-- TODO: Translations --> <!-- TODO: Translations -->
<Header <Header
@ -136,17 +206,84 @@ const showCreateModal = ref(false)
icon="bi-plus" icon="bi-plus"
primary primary
/> />
<inline-search-bar <Layout form flex
:class="['ui', {'loading': isLoading}, 'form']"
@submit.prevent="search"
>
<!-- TODO: Translations -->
<Input search
id="artist-search"
v-model="query" v-model="query"
name="search"
:label="t('components.library.Podcasts.label.search')"
autofocus
:placeholder="labels.searchPlaceholder" :placeholder="labels.searchPlaceholder"
@search="reloadWidget" >
</Input>
<Pills
v-model="tagList"
:label="t('components.library.Podcasts.label.tags')"
style="max-width: 150px;"
/> />
<Layout stack noGap label for="artist-ordering">
<span class="label">
{{ t('components.library.Podcasts.ordering.label') }}
</span>
<select
id="artist-ordering"
v-model="ordering"
class="dropdown"
>
<option
v-for="(option, key) in orderingOptions"
:key="key"
:value="option[0]"
>
{{ sharedLabels.filters[option[1]] }}
</option>
</select>
</Layout>
<Layout stack noGap label for="artist-ordering-direction">
<span class="label">
{{ t('components.library.Podcasts.ordering.direction.label') }}
</span>
<select
id="artist-ordering-direction"
v-model="orderingDirection"
class="dropdown"
>
<option value="+">
{{ t('components.library.Podcasts.ordering.direction.ascending') }}
</option>
<option value="-">
{{ t('components.library.Podcasts.ordering.direction.descending') }}
</option>
</select>
</Layout>
<Layout stack noGap label for="artist-results">
<span class="label">
{{ t('components.library.Podcasts.pagination.results') }}
</span>
<select
id="artist-results"
v-model="paginateBy"
class="dropdown"
>
<option
v-for="opt in paginateOptions"
:key="opt"
:value="opt"
>
{{ opt }}
</option>
</select>
</Layout>
</Layout>
<channels-widget <channels-widget
:show-modification-date="true" :show-modification-date="true"
:limit="24" :limit="paginateBy"
:filters="{ordering: '-creation_date'}" :filters="{q: query, ordering: ordering, tags: tags}"
> />
</channels-widget>
<Modal v-model="showCreateModal" <Modal v-model="showCreateModal"
:title="t(`views.auth.ProfileOverview.modal.createChannel.${ :title="t(`views.auth.ProfileOverview.modal.createChannel.${