Add "Nieuwe organisatie" button to the platform organisations list page. Dialog with name field (auto-generates slug) and slug field. Uses the existing POST /organisations endpoint. On success, navigates to the new organisation's detail page. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
270 lines
7.8 KiB
TypeScript
270 lines
7.8 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/vue-query'
|
|
import type { Ref } from 'vue'
|
|
import { apiClient } from '@/lib/axios'
|
|
import type {
|
|
ActivityLogEntry,
|
|
AdminOrganisation,
|
|
AdminOrganisationDetail,
|
|
AdminOrganisationMember,
|
|
AdminUser,
|
|
CreateOrganisationPayload,
|
|
ImpersonationResponse,
|
|
InviteMemberPayload,
|
|
PlatformStats,
|
|
UpdateAdminOrganisationPayload,
|
|
UpdateAdminUserPayload,
|
|
UpdateMemberRolePayload,
|
|
} from '@/types/admin'
|
|
|
|
interface ApiResponse<T> {
|
|
success: boolean
|
|
data: T
|
|
message?: string
|
|
}
|
|
|
|
interface PaginatedResponse<T> {
|
|
data: T[]
|
|
links: Record<string, string | null>
|
|
meta: {
|
|
current_page: number
|
|
per_page: number
|
|
total: number
|
|
last_page: number
|
|
}
|
|
}
|
|
|
|
// ─── Organisations ──────────────────────────────────────────
|
|
|
|
export function useAdminOrganisations(params: Ref<Record<string, string | number | undefined>>) {
|
|
return useQuery({
|
|
queryKey: ['admin', 'organisations', params],
|
|
queryFn: async () => {
|
|
const { data } = await apiClient.get<PaginatedResponse<AdminOrganisation>>(
|
|
'/admin/organisations',
|
|
{ params: params.value },
|
|
)
|
|
return data
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useAdminOrganisation(id: Ref<string>) {
|
|
return useQuery({
|
|
queryKey: ['admin', 'organisations', id],
|
|
queryFn: async () => {
|
|
const { data } = await apiClient.get<ApiResponse<AdminOrganisationDetail>>(
|
|
`/admin/organisations/${id.value}`,
|
|
)
|
|
return data.data
|
|
},
|
|
enabled: () => !!id.value,
|
|
})
|
|
}
|
|
|
|
export function useUpdateAdminOrganisation() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async ({ id, payload }: { id: string; payload: UpdateAdminOrganisationPayload }) => {
|
|
const { data } = await apiClient.put<ApiResponse<AdminOrganisation>>(
|
|
`/admin/organisations/${id}`,
|
|
payload,
|
|
)
|
|
return data.data
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['admin', 'organisations'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useCreateOrganisation() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async (payload: CreateOrganisationPayload) => {
|
|
const { data } = await apiClient.post<ApiResponse<AdminOrganisation>>(
|
|
'/organisations',
|
|
payload,
|
|
)
|
|
return data.data
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['admin', 'organisations'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useDeleteAdminOrganisation() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async (id: string) => {
|
|
await apiClient.delete(`/admin/organisations/${id}`)
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['admin', 'organisations'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
// ─── Organisation Members ──────────────────────────────────
|
|
|
|
export function useInviteOrganisationMember() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async ({ organisationId, payload }: { organisationId: string; payload: InviteMemberPayload }) => {
|
|
const { data } = await apiClient.post<ApiResponse<AdminOrganisationMember | null>>(
|
|
`/admin/organisations/${organisationId}/invite`,
|
|
payload,
|
|
)
|
|
return data
|
|
},
|
|
onSuccess: (_data, variables) => {
|
|
queryClient.invalidateQueries({ queryKey: ['admin', 'organisations', variables.organisationId] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useRemoveOrganisationMember() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async ({ organisationId, userId }: { organisationId: string; userId: string }) => {
|
|
await apiClient.delete(`/admin/organisations/${organisationId}/members/${userId}`)
|
|
},
|
|
onSuccess: (_data, variables) => {
|
|
queryClient.invalidateQueries({ queryKey: ['admin', 'organisations', variables.organisationId] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useUpdateOrganisationMemberRole() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async ({ organisationId, userId, payload }: { organisationId: string; userId: string; payload: UpdateMemberRolePayload }) => {
|
|
const { data } = await apiClient.put<ApiResponse<AdminOrganisationMember>>(
|
|
`/admin/organisations/${organisationId}/members/${userId}`,
|
|
payload,
|
|
)
|
|
return data.data
|
|
},
|
|
onSuccess: (_data, variables) => {
|
|
queryClient.invalidateQueries({ queryKey: ['admin', 'organisations', variables.organisationId] })
|
|
},
|
|
})
|
|
}
|
|
|
|
// ─── Users ──────────────────────────────────────────────────
|
|
|
|
export function useAdminUsers(params: Ref<Record<string, string | number | undefined>>) {
|
|
return useQuery({
|
|
queryKey: ['admin', 'users', params],
|
|
queryFn: async () => {
|
|
const { data } = await apiClient.get<PaginatedResponse<AdminUser>>(
|
|
'/admin/users',
|
|
{ params: params.value },
|
|
)
|
|
return data
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useAdminUser(id: Ref<string>) {
|
|
return useQuery({
|
|
queryKey: ['admin', 'users', id],
|
|
queryFn: async () => {
|
|
const { data } = await apiClient.get<ApiResponse<AdminUser>>(
|
|
`/admin/users/${id.value}`,
|
|
)
|
|
return data.data
|
|
},
|
|
enabled: () => !!id.value,
|
|
})
|
|
}
|
|
|
|
export function useUpdateAdminUser() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async ({ id, payload }: { id: string; payload: UpdateAdminUserPayload }) => {
|
|
const { data } = await apiClient.put<ApiResponse<AdminUser>>(
|
|
`/admin/users/${id}`,
|
|
payload,
|
|
)
|
|
return data.data
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['admin', 'users'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useDeleteAdminUser() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async (id: string) => {
|
|
await apiClient.delete(`/admin/users/${id}`)
|
|
},
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['admin', 'users'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
// ─── Stats ──────────────────────────────────────────────────
|
|
|
|
export function usePlatformStats() {
|
|
return useQuery({
|
|
queryKey: ['admin', 'stats'],
|
|
queryFn: async () => {
|
|
const { data } = await apiClient.get<{ data: PlatformStats }>(
|
|
'/admin/stats',
|
|
)
|
|
return data.data
|
|
},
|
|
})
|
|
}
|
|
|
|
// ─── Activity Log ───────────────────────────────────────────
|
|
|
|
export function useAdminActivityLog(params: Ref<Record<string, string | number | undefined>>) {
|
|
return useQuery({
|
|
queryKey: ['admin', 'activity-log', params],
|
|
queryFn: async () => {
|
|
const { data } = await apiClient.get<PaginatedResponse<ActivityLogEntry>>(
|
|
'/admin/activity-log',
|
|
{ params: params.value },
|
|
)
|
|
return data
|
|
},
|
|
})
|
|
}
|
|
|
|
// ─── Impersonation ──────────────────────────────────────────
|
|
|
|
export function useStartImpersonation() {
|
|
return useMutation({
|
|
mutationFn: async (userId: string) => {
|
|
const { data } = await apiClient.post<ApiResponse<ImpersonationResponse>>(
|
|
`/admin/impersonate/${userId}`,
|
|
)
|
|
return data.data
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useStopImpersonation() {
|
|
return useMutation({
|
|
mutationFn: async () => {
|
|
const { data } = await apiClient.post<ApiResponse<{ user: AdminUser }>>(
|
|
'/admin/stop-impersonation',
|
|
)
|
|
return data.data
|
|
},
|
|
})
|
|
}
|