This local working tree had accumulated a substantial amount of finished-but-never-committed frontend work: custom client fields (replacing the old static ClientFields with a dynamic ClientCustomFieldCard + useCustomFields), CSV import (useImport + settings/import page), history type management (useHistoryTypes + settings/history-types page), an admin-only route middleware, several new icon components, a shared useApiFetch composable, and a client detail page restructure ([id].vue -> [id]/index.vue). None of it had ever been pushed, so production was running a stale build that was already linking to routes/components that didn't exist server-side (missing IconSettings, 404s on /settings/import, etc). This commit brings the repo in line with local state.
57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
import type { Client, PaginatedResponse } from '~/types'
|
|
|
|
export const useClient = () => {
|
|
const clients = useState<Client[]>('clients.list', () => [])
|
|
const searchQuery = ref('')
|
|
|
|
const fetchClients = async (): Promise<void> => {
|
|
const response = await useApiFetch<PaginatedResponse<Client>>('/api/clients')
|
|
clients.value = response.data
|
|
}
|
|
|
|
const search = async (q: string): Promise<void> => {
|
|
if (!q.trim()) {
|
|
await fetchClients()
|
|
return
|
|
}
|
|
const response = await useApiFetch<{ data: Client[] }>('/api/search', {
|
|
query: { q },
|
|
})
|
|
clients.value = response.data
|
|
}
|
|
|
|
const fetchClient = async (id: number): Promise<Client> => {
|
|
const response = await useApiFetch<{ data: Client }>(`/api/clients/${id}`)
|
|
return response.data
|
|
}
|
|
|
|
const createClient = async (payload: { client_number: string; name: string; notes?: Record<string, string> }): Promise<Client> => {
|
|
const response = await useApiFetch<{ data: Client }>('/api/clients', {
|
|
method: 'POST',
|
|
body: payload,
|
|
})
|
|
return response.data
|
|
}
|
|
|
|
const updateClient = async (id: number, payload: Partial<Pick<Client, 'name' | 'notes'>>): Promise<Client> => {
|
|
const response = await useApiFetch<{ data: Client }>(`/api/clients/${id}`, {
|
|
method: 'PUT',
|
|
body: payload,
|
|
})
|
|
return response.data
|
|
}
|
|
|
|
const updateCustomFieldValue = async (
|
|
clientId: number,
|
|
definitionId: number,
|
|
value: string | null,
|
|
): Promise<Client> => {
|
|
const response = await useApiFetch<{ data: Client }>(
|
|
`/api/clients/${clientId}/custom-fields/${definitionId}`,
|
|
{ method: 'PUT', body: { value } },
|
|
)
|
|
return response.data
|
|
}
|
|
|
|
return { clients, searchQuery, fetchClients, search, fetchClient, createClient, updateClient, updateCustomFieldValue }
|
|
}
|