43 lines
1.5 KiB
TypeScript
43 lines
1.5 KiB
TypeScript
|
|
import type { CustomFieldDefinition, CustomFieldType } from '~/types'
|
||
|
|
|
||
|
|
export interface CustomFieldDefinitionPayload {
|
||
|
|
label: string
|
||
|
|
type: CustomFieldType
|
||
|
|
options?: string[]
|
||
|
|
is_required?: boolean
|
||
|
|
}
|
||
|
|
|
||
|
|
export const useCustomFields = () => {
|
||
|
|
const definitions = useState<CustomFieldDefinition[]>('customFields.definitions', () => [])
|
||
|
|
|
||
|
|
const fetchDefinitions = async (): Promise<void> => {
|
||
|
|
const response = await useApiFetch<{ data: CustomFieldDefinition[] }>('/api/custom-fields')
|
||
|
|
definitions.value = response.data
|
||
|
|
}
|
||
|
|
|
||
|
|
const createDefinition = async (payload: CustomFieldDefinitionPayload): Promise<void> => {
|
||
|
|
await useApiFetch('/api/custom-fields', { method: 'POST', body: payload })
|
||
|
|
await fetchDefinitions()
|
||
|
|
}
|
||
|
|
|
||
|
|
const updateDefinition = async (id: number, payload: CustomFieldDefinitionPayload): Promise<void> => {
|
||
|
|
await useApiFetch(`/api/custom-fields/${id}`, { method: 'PUT', body: payload })
|
||
|
|
await fetchDefinitions()
|
||
|
|
}
|
||
|
|
|
||
|
|
const deleteDefinition = async (id: number): Promise<void> => {
|
||
|
|
await useApiFetch(`/api/custom-fields/${id}`, { method: 'DELETE' })
|
||
|
|
definitions.value = definitions.value.filter(d => d.id !== id)
|
||
|
|
}
|
||
|
|
|
||
|
|
const reorderDefinitions = async (ids: number[]): Promise<void> => {
|
||
|
|
const response = await useApiFetch<{ data: CustomFieldDefinition[] }>('/api/custom-fields/reorder', {
|
||
|
|
method: 'POST',
|
||
|
|
body: { ids },
|
||
|
|
})
|
||
|
|
definitions.value = response.data
|
||
|
|
}
|
||
|
|
|
||
|
|
return { definitions, fetchDefinitions, createDefinition, updateDefinition, deleteDefinition, reorderDefinitions }
|
||
|
|
}
|