64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
|
|
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||
|
|
import { adminFetch } from './admin-api';
|
||
|
|
|
||
|
|
export interface Environment {
|
||
|
|
id: string;
|
||
|
|
slug: string;
|
||
|
|
displayName: string;
|
||
|
|
production: boolean;
|
||
|
|
enabled: boolean;
|
||
|
|
createdAt: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface CreateEnvironmentRequest {
|
||
|
|
slug: string;
|
||
|
|
displayName: string;
|
||
|
|
production: boolean;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface UpdateEnvironmentRequest {
|
||
|
|
displayName: string;
|
||
|
|
production: boolean;
|
||
|
|
enabled: boolean;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useEnvironments() {
|
||
|
|
return useQuery({
|
||
|
|
queryKey: ['admin', 'environments'],
|
||
|
|
queryFn: () => adminFetch<Environment[]>('/environments'),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useCreateEnvironment() {
|
||
|
|
const qc = useQueryClient();
|
||
|
|
return useMutation({
|
||
|
|
mutationFn: (req: CreateEnvironmentRequest) =>
|
||
|
|
adminFetch<Environment>('/environments', {
|
||
|
|
method: 'POST',
|
||
|
|
body: JSON.stringify(req),
|
||
|
|
}),
|
||
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ['admin', 'environments'] }),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useUpdateEnvironment() {
|
||
|
|
const qc = useQueryClient();
|
||
|
|
return useMutation({
|
||
|
|
mutationFn: ({ id, ...req }: UpdateEnvironmentRequest & { id: string }) =>
|
||
|
|
adminFetch<Environment>(`/environments/${id}`, {
|
||
|
|
method: 'PUT',
|
||
|
|
body: JSON.stringify(req),
|
||
|
|
}),
|
||
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ['admin', 'environments'] }),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useDeleteEnvironment() {
|
||
|
|
const qc = useQueryClient();
|
||
|
|
return useMutation({
|
||
|
|
mutationFn: (id: string) =>
|
||
|
|
adminFetch<void>(`/environments/${id}`, { method: 'DELETE' }),
|
||
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ['admin', 'environments'] }),
|
||
|
|
});
|
||
|
|
}
|