Files
sbd2-frontend/src/lib/api/services/extractions.ts
JSC 6a40311a82
Some checks failed
Frontend CI / lint (push) Failing after 18s
Frontend CI / build (push) Has been skipped
feat: add extraction deletion functionality with confirmation dialog and update extraction list on deletion
2025-08-25 21:40:47 +02:00

155 lines
4.1 KiB
TypeScript

import { apiClient } from '../client'
export interface ExtractionInfo {
id: number
url: string
status: 'pending' | 'processing' | 'completed' | 'failed'
title?: string
service?: string
service_id?: string
sound_id?: number
user_id: number
user_name?: string
error?: string
created_at: string
updated_at: string
}
export interface CreateExtractionResponse {
message: string
extraction: ExtractionInfo
}
export interface GetExtractionsResponse {
extractions: ExtractionInfo[]
total: number
page: number
limit: number
total_pages: number
}
export type ExtractionSortField = 'title' | 'status' | 'service' | 'created_at' | 'updated_at'
export type ExtractionSortOrder = 'asc' | 'desc'
export type ExtractionStatus = 'pending' | 'processing' | 'completed' | 'failed'
export interface GetExtractionsParams {
search?: string
sort_by?: ExtractionSortField
sort_order?: ExtractionSortOrder
status_filter?: ExtractionStatus
page?: number
limit?: number
}
export interface DeleteExtractionResponse {
message: string
}
export class ExtractionsService {
/**
* Create a new extraction job
*/
async createExtraction(url: string): Promise<CreateExtractionResponse> {
const response = await apiClient.post<CreateExtractionResponse>(
`/api/v1/extractions/?url=${encodeURIComponent(url)}`,
)
return response
}
/**
* Get extraction by ID
*/
async getExtraction(extractionId: number): Promise<ExtractionInfo> {
const response = await apiClient.get<ExtractionInfo>(
`/api/v1/extractions/${extractionId}`,
)
return response
}
/**
* Get all extractions
*/
async getAllExtractions(params?: GetExtractionsParams): Promise<GetExtractionsResponse> {
const searchParams = new URLSearchParams()
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?.status_filter) {
searchParams.append('status_filter', params.status_filter)
}
if (params?.page) {
searchParams.append('page', params.page.toString())
}
if (params?.limit) {
searchParams.append('limit', params.limit.toString())
}
const queryString = searchParams.toString()
const url = queryString ? `/api/v1/extractions/?${queryString}` : '/api/v1/extractions/'
const response = await apiClient.get<GetExtractionsResponse>(url)
return response
}
/**
* Get user's extractions
*/
async getUserExtractions(params?: GetExtractionsParams): Promise<GetExtractionsResponse> {
const searchParams = new URLSearchParams()
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?.status_filter) {
searchParams.append('status_filter', params.status_filter)
}
if (params?.page) {
searchParams.append('page', params.page.toString())
}
if (params?.limit) {
searchParams.append('limit', params.limit.toString())
}
const queryString = searchParams.toString()
const url = queryString ? `/api/v1/extractions/user?${queryString}` : '/api/v1/extractions/user'
const response = await apiClient.get<GetExtractionsResponse>(url)
return response
}
/**
* Get currently processing extractions
*/
async getProcessingExtractions(): Promise<ExtractionInfo[]> {
const response = await apiClient.get<ExtractionInfo[]>(
'/api/v1/extractions/processing/current'
)
return response
}
/**
* Delete an extraction
*/
async deleteExtraction(extractionId: number): Promise<DeleteExtractionResponse> {
const response = await apiClient.delete<DeleteExtractionResponse>(
`/api/v1/extractions/${extractionId}`
)
return response
}
}
export const extractionsService = new ExtractionsService()