feat: add search and sorting functionality to SoundsPage; update fetch logic to support new parameters
This commit is contained in:
@@ -21,8 +21,16 @@ export interface Sound {
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
export type SoundSortField = 'name' | 'filename' | 'duration' | 'size' | 'type' | 'play_count' | 'created_at' | 'updated_at'
|
||||
export type SortOrder = 'asc' | 'desc'
|
||||
|
||||
export interface GetSoundsParams {
|
||||
types?: string[]
|
||||
search?: string
|
||||
sort_by?: SoundSortField
|
||||
sort_order?: SortOrder
|
||||
limit?: number
|
||||
offset?: number
|
||||
}
|
||||
|
||||
export interface GetSoundsResponse {
|
||||
@@ -31,38 +39,52 @@ export interface GetSoundsResponse {
|
||||
|
||||
export class SoundsService {
|
||||
/**
|
||||
* Get all sounds with optional type filtering
|
||||
* Get all sounds with optional filtering, searching, and sorting
|
||||
*/
|
||||
async getSounds(params?: GetSoundsParams): Promise<Sound[]> {
|
||||
const queryParams: Record<string, string | number | boolean | undefined> = {}
|
||||
const searchParams = new URLSearchParams()
|
||||
|
||||
// Handle multiple types
|
||||
if (params?.types) {
|
||||
// Handle multiple types by building query string manually
|
||||
const searchParams = new URLSearchParams()
|
||||
params.types.forEach(type => {
|
||||
searchParams.append('types', type)
|
||||
})
|
||||
|
||||
const response = await apiClient.get<GetSoundsResponse>(`/api/v1/sounds/?${searchParams.toString()}`)
|
||||
return response.sounds || []
|
||||
}
|
||||
|
||||
const response = await apiClient.get<GetSoundsResponse>('/api/v1/sounds/', { params: queryParams })
|
||||
// Handle other parameters
|
||||
if (params?.search) {
|
||||
searchParams.append('search', params.search)
|
||||
}
|
||||
if (params?.sort_by) {
|
||||
searchParams.append('sort_by', params.sort_by)
|
||||
}
|
||||
if (params?.sort_order) {
|
||||
searchParams.append('sort_order', params.sort_order)
|
||||
}
|
||||
if (params?.limit) {
|
||||
searchParams.append('limit', params.limit.toString())
|
||||
}
|
||||
if (params?.offset) {
|
||||
searchParams.append('offset', params.offset.toString())
|
||||
}
|
||||
|
||||
const url = searchParams.toString() ? `/api/v1/sounds/?${searchParams.toString()}` : '/api/v1/sounds/'
|
||||
const response = await apiClient.get<GetSoundsResponse>(url)
|
||||
return response.sounds || []
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sounds of a specific type
|
||||
*/
|
||||
async getSoundsByType(type: string): Promise<Sound[]> {
|
||||
return this.getSounds({ types: [type] })
|
||||
async getSoundsByType(type: string, params?: Omit<GetSoundsParams, 'types'>): Promise<Sound[]> {
|
||||
return this.getSounds({ ...params, types: [type] })
|
||||
}
|
||||
|
||||
/**
|
||||
* Get SDB type sounds
|
||||
*/
|
||||
async getSDBSounds(): Promise<Sound[]> {
|
||||
return this.getSoundsByType('SDB')
|
||||
async getSDBSounds(params?: Omit<GetSoundsParams, 'types'>): Promise<Sound[]> {
|
||||
return this.getSoundsByType('SDB', params)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user