feat: companies CRUD with person dialog integration and navigation
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
74
apps/app/src/composables/api/useCompanies.ts
Normal file
74
apps/app/src/composables/api/useCompanies.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/vue-query'
|
||||
import type { Ref } from 'vue'
|
||||
import { apiClient } from '@/lib/axios'
|
||||
import type { Company, CreateCompanyPayload, UpdateCompanyPayload } from '@/types/organisation'
|
||||
|
||||
interface ApiResponse<T> {
|
||||
success: boolean
|
||||
data: T
|
||||
message?: string
|
||||
}
|
||||
|
||||
export function useCompanies(orgId: Ref<string>) {
|
||||
return useQuery({
|
||||
queryKey: ['companies', orgId],
|
||||
queryFn: async () => {
|
||||
const { data } = await apiClient.get<{ data: Company[] }>(
|
||||
`/organisations/${orgId.value}/companies`,
|
||||
)
|
||||
|
||||
return data.data
|
||||
},
|
||||
enabled: () => !!orgId.value,
|
||||
staleTime: Infinity,
|
||||
})
|
||||
}
|
||||
|
||||
export function useCreateCompany(orgId: Ref<string>) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (payload: CreateCompanyPayload) => {
|
||||
const { data } = await apiClient.post<ApiResponse<Company>>(
|
||||
`/organisations/${orgId.value}/companies`,
|
||||
payload,
|
||||
)
|
||||
|
||||
return data.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['companies', orgId] })
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useUpdateCompany(orgId: Ref<string>) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ id, ...payload }: UpdateCompanyPayload & { id: string }) => {
|
||||
const { data } = await apiClient.put<ApiResponse<Company>>(
|
||||
`/organisations/${orgId.value}/companies/${id}`,
|
||||
payload,
|
||||
)
|
||||
|
||||
return data.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['companies', orgId] })
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useDeleteCompany(orgId: Ref<string>) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (id: string) => {
|
||||
await apiClient.delete(`/organisations/${orgId.value}/companies/${id}`)
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['companies', orgId] })
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user