import { useMutation, useQuery, useQueryClient } from '@tanstack/vue-query' import { computed, type Ref } from 'vue' import { apiClient } from '@/lib/axios' import { useAuthStore } from '@/stores/useAuthStore' import type { Organisation, UpdateOrganisationPayload, } from '@/types/organisation' interface ApiResponse { success: boolean data: T message?: string } interface PaginatedResponse { data: T[] links: Record meta: { current_page: number per_page: number total: number last_page: number } } export function useOrganisationList() { return useQuery({ queryKey: ['organisations'], queryFn: async () => { const { data } = await apiClient.get>('/organisations') return data.data }, }) } export function useOrganisationDetail(id: Ref) { return useQuery({ queryKey: ['organisations', id], queryFn: async () => { const { data } = await apiClient.get>(`/organisations/${id.value}`) return data.data }, enabled: () => !!id.value, }) } export function useMyOrganisation() { const authStore = useAuthStore() const id = computed(() => authStore.currentOrganisation?.id ?? '') return useQuery({ queryKey: ['organisations', id], queryFn: async () => { const { data } = await apiClient.get>(`/organisations/${id.value}`) return data.data }, enabled: () => !!id.value, }) } export function useUpdateOrganisation() { const queryClient = useQueryClient() return useMutation({ mutationFn: async ({ id, ...payload }: UpdateOrganisationPayload & { id: string }) => { const { data } = await apiClient.put>(`/organisations/${id}`, payload) return data.data }, onSuccess: (_data, variables) => { queryClient.invalidateQueries({ queryKey: ['organisations'] }) queryClient.invalidateQueries({ queryKey: ['organisations', variables.id] }) }, }) }