H5: admin-scherm voor de hero (formulier, slides, upload, focus-editor met voorbeeld, TypeScript-tweeling van FocusMath)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
import { api } from '@/lib/api-client';
|
||||
import type { HeroAdminDto, HeroFocusRequest, HeroImageDto, HeroSaveRequest } from './types';
|
||||
|
||||
export const HERO_KEY = ['hero'];
|
||||
|
||||
/** The hero as the admin edits it (also the image variants of every slide). */
|
||||
export function useHero() {
|
||||
return useQuery<HeroAdminDto, Error>({
|
||||
queryKey: HERO_KEY,
|
||||
queryFn: () => api.get<HeroAdminDto>('/api/v1/hero/all'),
|
||||
});
|
||||
}
|
||||
|
||||
export function useSaveHero() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation<HeroAdminDto, Error, HeroSaveRequest>({
|
||||
mutationFn: (body) => api.put<HeroAdminDto>('/api/v1/hero', body),
|
||||
onSuccess: (hero) => {
|
||||
queryClient.setQueryData(HERO_KEY, hero);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/** Uploads one image file; the server answers with the image and its variants. */
|
||||
export function useUploadHeroImage() {
|
||||
return useMutation<HeroImageDto, Error, File>({
|
||||
mutationFn: (file) => {
|
||||
const form = new FormData();
|
||||
form.append('file', file);
|
||||
return api.post<HeroImageDto>('/api/v1/hero/images', form);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useSetImageFocus() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation<HeroImageDto, Error, { imageId: string; body: HeroFocusRequest }>({
|
||||
mutationFn: ({ imageId, body }) => api.put<HeroImageDto>(`/api/v1/hero/images/${imageId}/focus`, body),
|
||||
onSuccess: () => {
|
||||
// The slide list shows the image variants; they have new addresses after a change of focus.
|
||||
void queryClient.invalidateQueries({ queryKey: HERO_KEY });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeleteHeroImage() {
|
||||
return useMutation<void, Error, string>({
|
||||
mutationFn: (imageId) => api.delete<void>(`/api/v1/hero/images/${imageId}`),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user