Refactor package types and improve filter handling
Switch to API schema-defined types for package properties, replacing custom typings for consistency. Streamline filter initialization, validation, and URL parameter handling, while adding safeguards for null values. Simplify components by removing unused exports and types.
This commit is contained in:
@@ -2,10 +2,10 @@ import { defineStore } from 'pinia'
|
||||
import { reactive } from 'vue'
|
||||
import { components, getPackages } from '@/api'
|
||||
|
||||
export interface PackageFilters {
|
||||
interface PackageFilters {
|
||||
status?: components['schemas']['Package']['status'][]
|
||||
pkgbase?: components['schemas']['Package']['pkgbase']
|
||||
exact?: boolean | undefined
|
||||
exact?: boolean
|
||||
repo?: components['schemas']['Package']['repo']
|
||||
}
|
||||
|
||||
@@ -20,10 +20,10 @@ export const usePackagesStore = defineStore('packages', () => {
|
||||
error: null as string | null,
|
||||
lastUpdated: Date.now(),
|
||||
filters: {
|
||||
status: undefined as components['schemas']['Package']['status'][] | undefined,
|
||||
pkgbase: undefined as components['schemas']['Package']['pkgbase'] | undefined,
|
||||
exact: undefined as boolean | undefined,
|
||||
repo: undefined as components['schemas']['Package']['repo'] | undefined
|
||||
status: undefined,
|
||||
pkgbase: undefined,
|
||||
exact: undefined,
|
||||
repo: undefined
|
||||
} as PackageFilters
|
||||
})
|
||||
|
||||
@@ -36,20 +36,21 @@ export const usePackagesStore = defineStore('packages', () => {
|
||||
initFromUrl()
|
||||
}
|
||||
|
||||
const filter = {}
|
||||
const filter: PackageFilters = {}
|
||||
if (state.filters.status && state.filters.status.length > 0) {
|
||||
filter['status'] = state.filters.status
|
||||
filter.status = state.filters.status
|
||||
}
|
||||
if (state.filters.pkgbase) {
|
||||
filter['pkgbase'] = state.filters.pkgbase
|
||||
filter.pkgbase = state.filters.pkgbase
|
||||
}
|
||||
if (state.filters.exact === true) {
|
||||
filter['exact'] = ''
|
||||
filter.exact = state.filters.exact
|
||||
}
|
||||
if (state.filters.repo) {
|
||||
filter['repo'] = state.filters.repo
|
||||
filter.repo = state.filters.repo
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
getPackages({
|
||||
limit: state.limit,
|
||||
offset: state.offset,
|
||||
@@ -139,14 +140,16 @@ export const usePackagesStore = defineStore('packages', () => {
|
||||
const params = new URLSearchParams()
|
||||
|
||||
let page = state.offset / state.limit + 1
|
||||
// Only add page parameter if it's not the first page
|
||||
// Only add a page parameter if it's not the first page
|
||||
if (page > 1) {
|
||||
params.set('page', page.toString())
|
||||
}
|
||||
|
||||
if (state.filters.status && state.filters.status.length > 0) {
|
||||
state.filters.status.forEach((status) => {
|
||||
params.append('status', status)
|
||||
state.filters.status.forEach((status: components['schemas']['Package']['status']) => {
|
||||
if (status) {
|
||||
params.append('status', status)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -179,19 +182,24 @@ export const usePackagesStore = defineStore('packages', () => {
|
||||
const pageParam = urlParams.get('page') || '1'
|
||||
const parsedPage = parseInt(pageParam, 10)
|
||||
const page = !isNaN(parsedPage) && parsedPage > 0 ? parsedPage : 1
|
||||
state.offset = (page - 1) * state.limitq
|
||||
state.offset = (page - 1) * state.limit
|
||||
}
|
||||
|
||||
if (urlParams.has('status')) {
|
||||
state.filters.status = urlParams.getAll('status')
|
||||
const status = urlParams.getAll('status')
|
||||
state.filters.status = status as components['schemas']['Package']['status'][]
|
||||
}
|
||||
|
||||
if (urlParams.has('pkgbase')) {
|
||||
state.filters.pkgbase = urlParams.get('pkgbase')
|
||||
const pkgbase = urlParams.get('pkgbase')
|
||||
if (pkgbase === null) return
|
||||
state.filters.pkgbase = pkgbase as components['schemas']['Package']['pkgbase']
|
||||
}
|
||||
|
||||
if (urlParams.has('repo')) {
|
||||
state.filters.repo = urlParams.get('repo')
|
||||
const repo = urlParams.get('repo')
|
||||
if (repo === null) return
|
||||
state.filters.repo = repo as components['schemas']['Package']['repo']
|
||||
}
|
||||
|
||||
if (urlParams.has('exact')) {
|
||||
|
Reference in New Issue
Block a user