feat: platform admin frontend — pages, composables, navigation, impersonation
Build the frontend for platform admin in apps/app/: - TypeScript types (admin.ts) and API composable (useAdmin.ts) with TanStack Query for all admin endpoints - ImpersonationStore (Pinia) + ImpersonationBanner component integrated in the main layout, with token-based session management - Platform navigation section (conditionally shown for super_admin users) - Route guard blocking /platform/* for non-super_admin users - 6 pages: dashboard with stats cards, organisations list/detail, users list/detail with impersonation, activity log with expandable rows - All pages implement loading/error/empty states per conventions - Vite build passes cleanly Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
198
apps/app/src/composables/api/useAdmin.ts
Normal file
198
apps/app/src/composables/api/useAdmin.ts
Normal file
@@ -0,0 +1,198 @@
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/vue-query'
|
||||
import type { Ref } from 'vue'
|
||||
import { apiClient } from '@/lib/axios'
|
||||
import type {
|
||||
ActivityLogEntry,
|
||||
AdminOrganisation,
|
||||
AdminUser,
|
||||
ImpersonationResponse,
|
||||
PlatformStats,
|
||||
UpdateAdminOrganisationPayload,
|
||||
UpdateAdminUserPayload,
|
||||
} 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<AdminOrganisation>>(
|
||||
`/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 useDeleteAdminOrganisation() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (id: string) => {
|
||||
await apiClient.delete(`/admin/organisations/${id}`)
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['admin', 'organisations'] })
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// ─── 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
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user