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,10 +1,10 @@
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog';
|
||||
import type { OfferingAdminDto } from '../services/types';
|
||||
import type { OfferingDto } from '../services/types';
|
||||
|
||||
interface DeleteOfferingDialogProps {
|
||||
offering: OfferingAdminDto | null;
|
||||
offering: OfferingDto | null;
|
||||
isDeleting: boolean;
|
||||
onConfirm: () => void;
|
||||
onCancel: () => void;
|
||||
|
||||
@@ -9,10 +9,10 @@ import { Label } from '@/components/ui/label';
|
||||
import { FieldError } from '@/components/ui/FieldError';
|
||||
import { slugifyPreview } from '@/lib/slugify';
|
||||
import { offeringFormSchema, type OfferingFormData } from '../schemas/offering';
|
||||
import type { OfferingAdminDto } from '../services/types';
|
||||
import type { OfferingDto } from '../services/types';
|
||||
|
||||
interface OfferingFormProps {
|
||||
initialValues?: OfferingAdminDto;
|
||||
initialValues?: OfferingDto;
|
||||
onSubmit: (values: OfferingFormData) => void;
|
||||
isSubmitting: boolean;
|
||||
}
|
||||
|
||||
@@ -7,10 +7,10 @@ import { Button } from '@/components/ui/button';
|
||||
import { TableCell, TableRow } from '@/components/ui/table';
|
||||
import { useUpdateOffering } from '../services/useUpdateOffering';
|
||||
import { useMoveOffering } from '../services/useMoveOffering';
|
||||
import type { OfferingAdminDto } from '../services/types';
|
||||
import type { OfferingDto } from '../services/types';
|
||||
|
||||
interface OfferingRowProps {
|
||||
offering: OfferingAdminDto;
|
||||
offering: OfferingDto;
|
||||
isFirst: boolean;
|
||||
isLast: boolean;
|
||||
onDeleteRequested: () => void;
|
||||
|
||||
@@ -5,11 +5,11 @@ import { GripVertical } from 'lucide-react';
|
||||
import { Table, TableBody, TableHead, TableHeader, TableRow } from '@/components/ui/table';
|
||||
import { OfferingRow } from './OfferingRow';
|
||||
import { useOfferingsDnd } from '../hooks/useOfferingsDnd';
|
||||
import type { OfferingAdminDto } from '../services/types';
|
||||
import type { OfferingDto } from '../services/types';
|
||||
|
||||
interface OfferingsListProps {
|
||||
offerings: OfferingAdminDto[];
|
||||
onDeleteRequested: (offering: OfferingAdminDto) => void;
|
||||
offerings: OfferingDto[];
|
||||
onDeleteRequested: (offering: OfferingDto) => void;
|
||||
}
|
||||
|
||||
export function OfferingsList({ offerings, onDeleteRequested }: OfferingsListProps) {
|
||||
|
||||
@@ -9,9 +9,9 @@ import {
|
||||
} from '@dnd-kit/core';
|
||||
import { arrayMove, sortableKeyboardCoordinates } from '@dnd-kit/sortable';
|
||||
import { useReorderOfferings } from '../services/useReorderOfferings';
|
||||
import type { OfferingAdminDto } from '../services/types';
|
||||
import type { OfferingDto } from '../services/types';
|
||||
|
||||
export function useOfferingsDnd(offerings: OfferingAdminDto[]) {
|
||||
export function useOfferingsDnd(offerings: OfferingDto[]) {
|
||||
const [items, setItems] = useState(offerings);
|
||||
const [syncedOfferings, setSyncedOfferings] = useState(offerings);
|
||||
const [activeId, setActiveId] = useState<string | null>(null);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import type { OfferingAdminDto, CreateOfferingRequest, UpdateOfferingRequest } from '@/features/offerings/services/types';
|
||||
import type { OfferingDto, CreateOfferingRequest, UpdateOfferingRequest } from '@/features/offerings/services/types';
|
||||
import { API_BASE } from '@/features/auth/mocks/fixtures';
|
||||
|
||||
function slugify(value: string): string {
|
||||
@@ -11,7 +11,7 @@ function slugify(value: string): string {
|
||||
.replace(/^-+|-+$/g, '');
|
||||
}
|
||||
|
||||
const seed: OfferingAdminDto[] = [
|
||||
const seed: OfferingDto[] = [
|
||||
{
|
||||
id: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
|
||||
slug: 'starter',
|
||||
@@ -38,7 +38,7 @@ const seed: OfferingAdminDto[] = [
|
||||
},
|
||||
];
|
||||
|
||||
let mockOfferings: OfferingAdminDto[] = [...seed];
|
||||
let mockOfferings: OfferingDto[] = [...seed];
|
||||
|
||||
export const resetMockOfferings = () => {
|
||||
mockOfferings = [...seed];
|
||||
@@ -47,13 +47,9 @@ export const resetMockOfferings = () => {
|
||||
export const getMockOfferings = () => mockOfferings;
|
||||
|
||||
export const offeringsHandlers = [
|
||||
http.get(`${API_BASE}/api/v1/offerings`, () =>
|
||||
HttpResponse.json(mockOfferings.map(({ displayOrder: _displayOrder, ...dto }) => dto)),
|
||||
),
|
||||
http.get(`${API_BASE}/api/v1/offerings`, () => HttpResponse.json(mockOfferings)),
|
||||
|
||||
http.get(`${API_BASE}/api/v1/offerings/admin`, () => HttpResponse.json(mockOfferings)),
|
||||
|
||||
http.post(`${API_BASE}/api/v1/offerings/admin`, async ({ request }) => {
|
||||
http.post(`${API_BASE}/api/v1/offerings`, async ({ request }) => {
|
||||
const body = (await request.json()) as CreateOfferingRequest;
|
||||
const slug = body.slug && body.slug.length > 0 ? slugify(body.slug) : slugify(body.title);
|
||||
if (mockOfferings.some((o) => o.slug === slug)) {
|
||||
@@ -63,7 +59,7 @@ export const offeringsHandlers = [
|
||||
);
|
||||
}
|
||||
|
||||
const newOffering: OfferingAdminDto = {
|
||||
const newOffering: OfferingDto = {
|
||||
id: crypto.randomUUID(),
|
||||
...body,
|
||||
slug,
|
||||
@@ -73,7 +69,7 @@ export const offeringsHandlers = [
|
||||
return HttpResponse.json(newOffering, { status: 201 });
|
||||
}),
|
||||
|
||||
http.put(`${API_BASE}/api/v1/offerings/admin/:id`, async ({ params, request }) => {
|
||||
http.put(`${API_BASE}/api/v1/offerings/:id`, async ({ params, request }) => {
|
||||
const { id } = params as { id: string };
|
||||
const body = (await request.json()) as UpdateOfferingRequest;
|
||||
const existing = mockOfferings.find((o) => o.id === id);
|
||||
@@ -92,48 +88,25 @@ export const offeringsHandlers = [
|
||||
);
|
||||
}
|
||||
|
||||
const updated: OfferingAdminDto = { ...existing, ...body, slug };
|
||||
const updated: OfferingDto = { ...existing, ...body, slug };
|
||||
mockOfferings = mockOfferings.map((o) => (o.id === id ? updated : o));
|
||||
return HttpResponse.json(updated);
|
||||
}),
|
||||
|
||||
http.delete(`${API_BASE}/api/v1/offerings/admin/:id`, ({ params }) => {
|
||||
http.delete(`${API_BASE}/api/v1/offerings/:id`, ({ params }) => {
|
||||
const { id } = params as { id: string };
|
||||
mockOfferings = mockOfferings.filter((o) => o.id !== id);
|
||||
return new HttpResponse(null, { status: 204 });
|
||||
}),
|
||||
|
||||
http.put(`${API_BASE}/api/v1/offerings/admin/reorder`, async ({ request }) => {
|
||||
http.put(`${API_BASE}/api/v1/offerings/order`, async ({ request }) => {
|
||||
const body = (await request.json()) as { orderedIds: string[] };
|
||||
mockOfferings = body.orderedIds
|
||||
.map((id, index) => {
|
||||
const offering = mockOfferings.find((o) => o.id === id);
|
||||
return offering ? { ...offering, displayOrder: index } : null;
|
||||
})
|
||||
.filter((o): o is OfferingAdminDto => o !== null);
|
||||
return new HttpResponse(null, { status: 204 });
|
||||
}),
|
||||
|
||||
http.post(`${API_BASE}/api/v1/offerings/admin/:id/move-up`, ({ params }) => {
|
||||
const { id } = params as { id: string };
|
||||
moveAdjacent(id, -1);
|
||||
return new HttpResponse(null, { status: 204 });
|
||||
}),
|
||||
|
||||
http.post(`${API_BASE}/api/v1/offerings/admin/:id/move-down`, ({ params }) => {
|
||||
const { id } = params as { id: string };
|
||||
moveAdjacent(id, 1);
|
||||
.filter((o): o is OfferingDto => o !== null);
|
||||
return new HttpResponse(null, { status: 204 });
|
||||
}),
|
||||
];
|
||||
|
||||
function moveAdjacent(id: string, delta: number) {
|
||||
const sorted = [...mockOfferings].sort((a, b) => a.displayOrder - b.displayOrder);
|
||||
const index = sorted.findIndex((o) => o.id === id);
|
||||
const swapIndex = index + delta;
|
||||
if (index === -1 || swapIndex < 0 || swapIndex >= sorted.length) return;
|
||||
|
||||
const a = sorted[index];
|
||||
const b = sorted[swapIndex];
|
||||
[a.displayOrder, b.displayOrder] = [b.displayOrder, a.displayOrder];
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
@@ -13,7 +13,7 @@ beforeEach(() => {
|
||||
resetMockOfferings();
|
||||
});
|
||||
|
||||
const OFFERINGS_URL = `${API_BASE}/api/v1/offerings/admin`;
|
||||
const OFFERINGS_URL = `${API_BASE}/api/v1/offerings`;
|
||||
|
||||
describe('OfferingsListPage', () => {
|
||||
it('renders the page title and Add button', async () => {
|
||||
@@ -86,6 +86,32 @@ describe('OfferingsListPage', () => {
|
||||
).toBeDisabled();
|
||||
});
|
||||
|
||||
it('moves a row down by sending the full new order to PUT /offerings/order', async () => {
|
||||
mockAuthenticated();
|
||||
let sentBody: { orderedIds: string[] } | undefined;
|
||||
server.use(
|
||||
http.put(`${OFFERINGS_URL}/order`, async ({ request }) => {
|
||||
sentBody = (await request.json()) as { orderedIds: string[] };
|
||||
return new HttpResponse(null, { status: 204 });
|
||||
}),
|
||||
);
|
||||
renderApp('/offerings');
|
||||
|
||||
await userEvent.click(
|
||||
await screen.findByTestId(
|
||||
'offering-row-aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa-move-down-button',
|
||||
{},
|
||||
{ timeout: 10000 },
|
||||
),
|
||||
);
|
||||
|
||||
await vi.waitFor(() =>
|
||||
expect(sentBody).toEqual({
|
||||
orderedIds: ['bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb', 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'],
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('redirects unauthenticated users to login', async () => {
|
||||
mockGuest();
|
||||
renderApp('/offerings');
|
||||
|
||||
@@ -7,11 +7,11 @@ import { OfferingsList } from '../components/OfferingsList';
|
||||
import { DeleteOfferingDialog } from '../components/DeleteOfferingDialog';
|
||||
import { useOfferings } from '../services/useOfferings';
|
||||
import { useDeleteOffering } from '../services/useDeleteOffering';
|
||||
import type { OfferingAdminDto } from '../services/types';
|
||||
import type { OfferingDto } from '../services/types';
|
||||
|
||||
export function OfferingsListPage() {
|
||||
const { t } = useTranslation();
|
||||
const [offeringPendingDelete, setOfferingPendingDelete] = useState<OfferingAdminDto | null>(null);
|
||||
const [offeringPendingDelete, setOfferingPendingDelete] = useState<OfferingDto | null>(null);
|
||||
const { data: offerings, isPending, isError } = useOfferings();
|
||||
const deleteOffering = useDeleteOffering({
|
||||
onSuccess: () => setOfferingPendingDelete(null),
|
||||
|
||||
@@ -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