Remove ordering and pagination mixins from a bunch of files

environments/review-front-deve-otr6gc/deployments/13419
Kasper Seweryn 2022-06-04 12:35:19 +00:00 zatwierdzone przez Georg Krause
rodzic 3266cd80bd
commit 8cf3500842
3 zmienionych plików z 122 dodań i 139 usunięć

Wyświetl plik

@ -40,7 +40,6 @@ const orderingOptions: [OrderingField, keyof typeof sharedLabels.filters][] = [
]
const logger = useLogger()
const sharedLabels = useSharedLabels()
const router = useRouter()

Wyświetl plik

@ -36,7 +36,7 @@ const props = withDefaults(defineProps<Props>(), {
const page = ref(+props.defaultPage)
type ResponseType = { count: number, results: any[] }
const result = ref<null | ResponseType>(null)
const query = ref('')
const query = ref(props.defaultQuery)
const tags = reactive(props.defaultTags.slice())
const orderingOptions: [OrderingField, keyof typeof sharedLabels.filters][] = [
@ -46,7 +46,6 @@ const orderingOptions: [OrderingField, keyof typeof sharedLabels.filters][] = [
]
const logger = useLogger()
const sharedLabels = useSharedLabels()
const { onOrderingUpdate, orderingString, paginateBy, ordering, orderingDirection } = useOrdering(props.orderingConfigName)
@ -63,7 +62,7 @@ const updateQueryString = () => router.replace({
})
const search = () => {
page.value = 1
page.value = props.defaultPage
updateQueryString()
}

Wyświetl plik

@ -1,3 +1,121 @@
<script setup lang="ts">
import qs from 'qs'
import axios from 'axios'
import $ from 'jquery'
import ArtistCard from '~/components/audio/artist/Card.vue'
import Pagination from '~/components/vui/Pagination.vue'
import TagsSelector from '~/components/library/TagsSelector.vue'
import useLogger from '~/composables/useLogger'
import useSharedLabels from '~/composables/locale/useSharedLabels'
import { RouteWithPreferences } from '~/store/ui'
import { computed, reactive, ref, watch } from 'vue'
import { useGettext } from 'vue3-gettext'
import { useStore } from '~/store'
import useOrdering from '~/composables/useOrdering'
import { onBeforeRouteUpdate, useRouter } from 'vue-router'
interface Props {
orderingConfigName: RouteWithPreferences | null
defaultPage?: number
defaultPaginateBy?: number
defaultQuery?: string
defaultTags?: string[]
scope?: string
}
const props = withDefaults(defineProps<Props>(), {
defaultPage: 1,
defaultPaginateBy: 1,
defaultQuery: '',
defaultTags: () => [],
scope: 'all'
})
// TODO (wvffle): Make sure everything is it's own type
const page = ref(+props.defaultPage)
type ResponseType = { count: number, results: any[] }
const result = ref<null | ResponseType>(null)
const query = ref(props.defaultQuery)
const tags = reactive(props.defaultTags.slice())
const excludeCompilation = ref(true)
const logger = useLogger()
const sharedLabels = useSharedLabels()
const { onOrderingUpdate, orderingString, paginateBy, ordering, orderingDirection } = useOrdering(props.orderingConfigName)
const router = useRouter()
const updateQueryString = () => router.replace({
query: {
query: query.value,
page: page.value,
tag: tags,
paginateBy: paginateBy.value,
ordering: orderingString.value,
content_category: 'music',
include_channels: 'true'
}
})
const search = () => {
page.value = props.defaultPage
updateQueryString()
}
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,
q: query.value,
ordering: orderingString.value,
playable: 'true',
tag: tags,
include_channels: 'true',
content_category: 'music',
has_albums: excludeCompilation.value
}
logger.time('Fetching artists')
try {
const response = await axios.get('artists/', {
params,
paramsSerializer: function (params) {
return qs.stringify(params, { indices: false })
}
})
result.value = response.data
} catch (error) {
// TODO (wvffle): Handle error
result.value = null
} finally {
logger.timeEnd('Fetching artists')
isLoading.value = false
}
}
const store = useStore()
watch([store.state.moderation.lastUpdate, excludeCompilation], fetchData)
onBeforeRouteUpdate(fetchData)
fetchData()
// @ts-expect-error semantic ui
onMounted(() => $('.ui.dropdown').dropdown())
const { $pgettext } = useGettext()
const labels = computed(() => ({
searchPlaceholder: $pgettext('Content/Search/Input.Placeholder', 'Search…'),
title: $pgettext('*/*/*/Noun', 'Artists')
}))
</script>
<template>
<main v-title="labels.title">
<section class="ui vertical stripe segment">
@ -8,7 +126,7 @@
</h2>
<form
:class="['ui', {'loading': isLoading}, 'form']"
@submit.prevent="updatePage();updateQueryString();fetchData()"
@submit.prevent="search"
>
<div class="fields">
<div class="field">
@ -152,144 +270,11 @@
<div class="ui center aligned basic segment">
<pagination
v-if="result && result.count > paginateBy"
:current="page"
v-model:current="page"
:paginate-by="paginateBy"
:total="result.count"
@page-changed="selectPage"
/>
</div>
</section>
</main>
</template>
<script>
import qs from 'qs'
import axios from 'axios'
import $ from 'jquery'
import OrderingMixin from '~/components/mixins/Ordering.vue'
import PaginationMixin from '~/components/mixins/Pagination.vue'
import ArtistCard from '~/components/audio/artist/Card.vue'
import Pagination from '~/components/vui/Pagination.vue'
import TagsSelector from '~/components/library/TagsSelector.vue'
import useLogger from '~/composables/useLogger'
import useSharedLabels from '~/composables/locale/useSharedLabels'
const logger = useLogger()
const FETCH_URL = 'artists/'
export default {
components: {
ArtistCard,
Pagination,
TagsSelector
},
mixins: [OrderingMixin, PaginationMixin],
props: {
defaultQuery: { type: String, required: false, default: '' },
defaultTags: { type: Array, required: false, default: () => { return [] } },
scope: { type: String, required: false, default: 'all' }
},
setup () {
const sharedLabels = useSharedLabels()
return { sharedLabels }
},
data () {
return {
isLoading: true,
result: null,
excludeCompilation: true,
page: parseInt(this.defaultPage),
query: this.defaultQuery,
tags: (this.defaultTags || []).filter((t) => { return t.length > 0 }),
orderingOptions: [['creation_date', 'creation_date'], ['name', 'name']]
}
},
computed: {
labels () {
const searchPlaceholder = this.$pgettext('Content/Search/Input.Placeholder', 'Search…')
const title = this.$pgettext('*/*/*/Noun', 'Artists')
return {
searchPlaceholder,
title
}
}
},
watch: {
page () {
this.updateQueryString()
this.fetchData()
},
'$store.state.moderation.lastUpdate': function () {
this.fetchData()
},
excludeCompilation () {
this.fetchData()
}
},
created () {
this.fetchData()
},
mounted () {
$('.ui.dropdown').dropdown()
},
methods: {
updateQueryString: function () {
history.pushState(
{},
null,
this.$route.path + '?' + new URLSearchParams(
{
query: this.query,
page: this.page,
tag: this.tags,
paginateBy: this.paginateBy,
ordering: this.getOrderingAsString(),
content_category: 'music',
include_channels: true
}).toString()
)
},
fetchData: function () {
const self = this
this.isLoading = true
const url = FETCH_URL
const params = {
scope: this.scope,
page: this.page,
page_size: this.paginateBy,
has_albums: this.excludeCompilation,
q: this.query,
ordering: this.getOrderingAsString(),
playable: 'true',
tag: this.tags,
include_channels: 'true',
content_category: 'music'
}
logger.debug('Fetching artists')
axios.get(
url,
{
params: params,
paramsSerializer: function (params) {
return qs.stringify(params, { indices: false })
}
}
).then(response => {
self.result = response.data
self.isLoading = false
}, () => {
self.result = null
self.isLoading = false
})
},
selectPage: function (page) {
this.page = page
},
updatePage () {
this.page = this.defaultPage
}
}
}
</script>