Aanbod: RESTful routes zonder /admin, één OfferingDto en herordenen via PUT /offerings/order
Admin en bezoeker gebruiken dezelfde resource; alleen GET is anoniem, de rest vereist AdminOnly. OfferingAdminDto vervalt (displayOrder zit nu in OfferingDto), move-up/move-down zijn vervangen door de order-route die een onbekende of onvolledige lijst met 400 afwijst. De admin-frontend en alle tests zijn mee aangepast. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
export interface OfferingAdminDto {
|
||||
export interface OfferingDto {
|
||||
id: string;
|
||||
slug: string;
|
||||
title: string;
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { useMutation, useQueryClient, type UseMutationOptions } from '@tanstack/react-query';
|
||||
import { api } from '@/lib/api-client';
|
||||
import type { CreateOfferingRequest, OfferingAdminDto } from './types';
|
||||
import type { CreateOfferingRequest, OfferingDto } from './types';
|
||||
|
||||
export function useCreateOffering(
|
||||
options?: Pick<UseMutationOptions<OfferingAdminDto, Error, CreateOfferingRequest>, 'onSuccess' | 'onError'>,
|
||||
options?: Pick<UseMutationOptions<OfferingDto, Error, CreateOfferingRequest>, 'onSuccess' | 'onError'>,
|
||||
) {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation<OfferingAdminDto, Error, CreateOfferingRequest>({
|
||||
mutationFn: (data) => api.post<OfferingAdminDto>('/api/v1/offerings/admin', data),
|
||||
return useMutation<OfferingDto, Error, CreateOfferingRequest>({
|
||||
mutationFn: (data) => api.post<OfferingDto>('/api/v1/offerings', data),
|
||||
onSuccess: (data, variables, ...rest) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['offerings', 'admin'] });
|
||||
options?.onSuccess?.(data, variables, ...rest);
|
||||
|
||||
@@ -6,7 +6,7 @@ export function useDeleteOffering(
|
||||
) {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation<void, Error, string>({
|
||||
mutationFn: (id) => api.delete<void>(`/api/v1/offerings/admin/${id}`),
|
||||
mutationFn: (id) => api.delete<void>(`/api/v1/offerings/${id}`),
|
||||
onSuccess: (data, variables, ...rest) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['offerings', 'admin'] });
|
||||
options?.onSuccess?.(data, variables, ...rest);
|
||||
|
||||
@@ -1,16 +1,32 @@
|
||||
import { useMutation, useQueryClient, type UseMutationOptions } from '@tanstack/react-query';
|
||||
import { api } from '@/lib/api-client';
|
||||
import type { MoveDirection } from './types';
|
||||
import type { MoveDirection, OfferingDto } from './types';
|
||||
|
||||
const OFFERINGS_KEY = ['offerings', 'admin'];
|
||||
|
||||
/**
|
||||
* Moves one offering a step up or down. The API has no move endpoint (the collection is reordered
|
||||
* as a whole via `PUT /api/v1/offerings/order`), so the new order is computed from the cached list.
|
||||
* Moving past either end is a no-op.
|
||||
*/
|
||||
export function useMoveOffering(
|
||||
direction: MoveDirection,
|
||||
options?: Pick<UseMutationOptions<void, Error, string>, 'onSuccess' | 'onError'>,
|
||||
) {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation<void, Error, string>({
|
||||
mutationFn: (id) => api.post<void>(`/api/v1/offerings/admin/${id}/move-${direction}`),
|
||||
mutationFn: async (id) => {
|
||||
const offerings = queryClient.getQueryData<OfferingDto[]>(OFFERINGS_KEY) ?? [];
|
||||
const ids = [...offerings].sort((a, b) => a.displayOrder - b.displayOrder).map((o) => o.id);
|
||||
const index = ids.indexOf(id);
|
||||
const target = direction === 'up' ? index - 1 : index + 1;
|
||||
if (index === -1 || target < 0 || target >= ids.length) return;
|
||||
|
||||
[ids[index], ids[target]] = [ids[target], ids[index]];
|
||||
await api.put<void>('/api/v1/offerings/order', { orderedIds: ids });
|
||||
},
|
||||
onSuccess: (data, variables, ...rest) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['offerings', 'admin'] });
|
||||
queryClient.invalidateQueries({ queryKey: OFFERINGS_KEY });
|
||||
options?.onSuccess?.(data, variables, ...rest);
|
||||
},
|
||||
onError: options?.onError,
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { api } from '@/lib/api-client';
|
||||
import type { OfferingAdminDto } from './types';
|
||||
import type { OfferingDto } from './types';
|
||||
|
||||
export function useOfferings() {
|
||||
return useQuery<OfferingAdminDto[], Error>({
|
||||
return useQuery<OfferingDto[], Error>({
|
||||
queryKey: ['offerings', 'admin'],
|
||||
queryFn: () => api.get<OfferingAdminDto[]>('/api/v1/offerings/admin'),
|
||||
queryFn: () => api.get<OfferingDto[]>('/api/v1/offerings'),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ export function useReorderOfferings(
|
||||
) {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation<void, Error, string[]>({
|
||||
mutationFn: (orderedIds) => api.put<void>('/api/v1/offerings/admin/reorder', { orderedIds }),
|
||||
mutationFn: (orderedIds) => api.put<void>('/api/v1/offerings/order', { orderedIds }),
|
||||
onSuccess: (data, variables, ...rest) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['offerings', 'admin'] });
|
||||
options?.onSuccess?.(data, variables, ...rest);
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
import { useMutation, useQueryClient, type UseMutationOptions } from '@tanstack/react-query';
|
||||
import { api } from '@/lib/api-client';
|
||||
import type { OfferingAdminDto, UpdateOfferingRequest } from './types';
|
||||
import type { OfferingDto, UpdateOfferingRequest } from './types';
|
||||
|
||||
type UpdateVariables = { id: string } & UpdateOfferingRequest;
|
||||
|
||||
export function useUpdateOffering(
|
||||
options?: Pick<UseMutationOptions<OfferingAdminDto, Error, UpdateVariables>, 'onSuccess' | 'onError'>,
|
||||
options?: Pick<UseMutationOptions<OfferingDto, Error, UpdateVariables>, 'onSuccess' | 'onError'>,
|
||||
) {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation<OfferingAdminDto, Error, UpdateVariables>({
|
||||
mutationFn: ({ id, ...body }) => api.put<OfferingAdminDto>(`/api/v1/offerings/admin/${id}`, body),
|
||||
return useMutation<OfferingDto, Error, UpdateVariables>({
|
||||
mutationFn: ({ id, ...body }) => api.put<OfferingDto>(`/api/v1/offerings/${id}`, body),
|
||||
onSuccess: (data, variables, ...rest) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['offerings', 'admin'] });
|
||||
options?.onSuccess?.(data, variables, ...rest);
|
||||
|
||||
Reference in New Issue
Block a user