Merge pull request 'Create new guest without reloading guests list' (#269) from create-guest-without-reload into main
Reviewed-on: #269
This commit is contained in:
commit
171de9f1aa
@ -99,7 +99,10 @@ export default function Page() {
|
|||||||
<GuestFormDialog
|
<GuestFormDialog
|
||||||
key={guestBeingEdited?.id}
|
key={guestBeingEdited?.id}
|
||||||
groups={groups}
|
groups={groups}
|
||||||
onCreate={() => { refreshGuests(); setGuestBeingEdited(undefined) }}
|
onCreate={(newGuest) => {
|
||||||
|
setGuests([newGuest!, ...guests]);
|
||||||
|
setGuestBeingEdited(undefined) ;
|
||||||
|
}}
|
||||||
guest={guestBeingEdited}
|
guest={guestBeingEdited}
|
||||||
visible={guestBeingEdited !== undefined}
|
visible={guestBeingEdited !== undefined}
|
||||||
onHide={() => { setGuestBeingEdited(undefined) }}
|
onHide={() => { setGuestBeingEdited(undefined) }}
|
||||||
|
@ -6,7 +6,7 @@ import { getCsrfToken, getSlug } from '@/app/lib/utils';
|
|||||||
export interface Api<T extends Entity> {
|
export interface Api<T extends Entity> {
|
||||||
getAll(serializable: Serializable<T>, callback: (objets: T[]) => void): void;
|
getAll(serializable: Serializable<T>, callback: (objets: T[]) => void): void;
|
||||||
get(serializable: Serializable<T>, id: string, callback: (object: T) => void): void;
|
get(serializable: Serializable<T>, id: string, callback: (object: T) => void): void;
|
||||||
create(serializable: Serializable<T>, object: T, callback: () => void): void;
|
create(serializable: Serializable<T>, object: T, callback: (object: T) => void): void;
|
||||||
update(serializable: Serializable<T>, object: T, callback: () => void): void;
|
update(serializable: Serializable<T>, object: T, callback: () => void): void;
|
||||||
destroy(serializable: Serializable<T>, object: T, callback: () => void): void;
|
destroy(serializable: Serializable<T>, object: T, callback: () => void): void;
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import { Serializable } from "../api/abstract-api";
|
import { Serializable } from "../api/abstract-api";
|
||||||
import { Entity } from "./definitions";
|
import { Entity } from "./definitions";
|
||||||
|
import { Group } from "./group";
|
||||||
|
|
||||||
export const guestStatuses = ['considered', 'invited', 'confirmed', 'declined', 'tentative'] as const;
|
export const guestStatuses = ['considered', 'invited', 'confirmed', 'declined', 'tentative'] as const;
|
||||||
export type GuestStatus = typeof guestStatuses[number];
|
export type GuestStatus = typeof guestStatuses[number];
|
||||||
@ -9,30 +10,28 @@ export type GuestStatus = typeof guestStatuses[number];
|
|||||||
export class Guest implements Entity {
|
export class Guest implements Entity {
|
||||||
id?: string;
|
id?: string;
|
||||||
name?: string;
|
name?: string;
|
||||||
group_name?: string;
|
|
||||||
groupId?: string;
|
|
||||||
color?: string;
|
color?: string;
|
||||||
status?: GuestStatus;
|
status?: GuestStatus;
|
||||||
children?: Guest[];
|
children?: Guest[];
|
||||||
|
group?: Group;
|
||||||
|
|
||||||
constructor(id?: string, name?: string, group_name?: string, groupId?: string, color?: string, status?: GuestStatus, children?: Guest[]) {
|
constructor(id?: string, name?: string, color?: string, status?: GuestStatus, children?: Guest[], Group?: Group) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.group_name = group_name;
|
|
||||||
this.groupId = groupId;
|
|
||||||
this.color = color;
|
this.color = color;
|
||||||
this.status = status;
|
this.status = status;
|
||||||
this.children = children;
|
this.children = children;
|
||||||
|
this.group = Group;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class GuestSerializer implements Serializable<Guest> {
|
export class GuestSerializer implements Serializable<Guest> {
|
||||||
fromJson(data: any): Guest {
|
fromJson(data: any): Guest {
|
||||||
return new Guest(data.id, data.name, data.group?.name, data.group?.id, data.color, data.status, data.children);
|
return new Guest(data.id, data.name, data.color, data.status, data.children, new Group(data.group?.id, data.group?.name));
|
||||||
}
|
}
|
||||||
|
|
||||||
toJson(guest: Guest): string {
|
toJson(guest: Guest): string {
|
||||||
return JSON.stringify({ guest: { name: guest.name, status: guest.status, group_id: guest.groupId } });
|
return JSON.stringify({ guest: { name: guest.name, status: guest.status, group_id: guest.group?.id } });
|
||||||
}
|
}
|
||||||
|
|
||||||
apiPath(): string {
|
apiPath(): string {
|
||||||
|
@ -15,7 +15,7 @@ export class Invitation implements Entity {
|
|||||||
|
|
||||||
export class InvitationSerializer {
|
export class InvitationSerializer {
|
||||||
fromJson(data: any): Invitation {
|
fromJson(data: any): Invitation {
|
||||||
return new Invitation(data.id, (data.guests || []).map((guest: any) => new Guest(guest.id, guest.name, guest.group_name, guest.groupId, guest.color, guest.status, guest.children)));
|
return new Invitation(data.id, (data.guests || []).map((guest: any) => new Guest(guest.id, guest.name, guest.color, guest.status, guest.children)));
|
||||||
}
|
}
|
||||||
|
|
||||||
toJson(invitation: Invitation): string {
|
toJson(invitation: Invitation): string {
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import { Serializable } from "../api/abstract-api";
|
import { Serializable } from "../api/abstract-api";
|
||||||
import { Entity } from "./definitions";
|
import { Entity } from "./definitions";
|
||||||
|
import { Group } from "./group";
|
||||||
import { Guest } from "./guest";
|
import { Guest } from "./guest";
|
||||||
|
|
||||||
export type Discomfort = {
|
export type Discomfort = {
|
||||||
@ -53,10 +54,10 @@ export class TableSimulationSerializer implements Serializable<TableSimulation>
|
|||||||
return {
|
return {
|
||||||
id: guest.id,
|
id: guest.id,
|
||||||
name: guest.name,
|
name: guest.name,
|
||||||
group_id: guest.groupId,
|
|
||||||
color: guest.color,
|
color: guest.color,
|
||||||
status: guest.status,
|
status: guest.status,
|
||||||
children: guest.children,
|
children: guest.children,
|
||||||
|
group: new Group(guest.group?.id, guest.group?.name)
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
discomfort: table.discomfort,
|
discomfort: table.discomfort,
|
||||||
|
@ -15,14 +15,14 @@ import { useState } from 'react';
|
|||||||
|
|
||||||
export default function GuestFormDialog({ groups, onCreate, onHide, guest, visible }: {
|
export default function GuestFormDialog({ groups, onCreate, onHide, guest, visible }: {
|
||||||
groups: Group[],
|
groups: Group[],
|
||||||
onCreate?: () => void,
|
onCreate?: (guest: Guest) => void,
|
||||||
onHide: () => void,
|
onHide: () => void,
|
||||||
guest?: Guest,
|
guest?: Guest,
|
||||||
visible: boolean,
|
visible: boolean,
|
||||||
}) {
|
}) {
|
||||||
|
|
||||||
const [name, setName] = useState(guest?.name || '');
|
const [name, setName] = useState(guest?.name || '');
|
||||||
const [group, setGroup] = useState(guest?.groupId || null);
|
const [group, setGroup] = useState(guest?.group?.id || null);
|
||||||
const [status, setStatus] = useState<GuestStatus | null>(guest?.status || null);
|
const [status, setStatus] = useState<GuestStatus | null>(guest?.status || null);
|
||||||
|
|
||||||
const api = new AbstractApi<Guest>();
|
const api = new AbstractApi<Guest>();
|
||||||
@ -41,17 +41,17 @@ export default function GuestFormDialog({ groups, onCreate, onHide, guest, visib
|
|||||||
|
|
||||||
if (guest?.id !== undefined) {
|
if (guest?.id !== undefined) {
|
||||||
guest.name = name;
|
guest.name = name;
|
||||||
guest.groupId = group;
|
guest.group!.id = group;
|
||||||
guest.status = status;
|
guest.status = status;
|
||||||
|
|
||||||
api.update(serializer, guest, () => {
|
api.update(serializer, guest, () => {
|
||||||
resetForm();
|
resetForm();
|
||||||
onCreate && onCreate();
|
onCreate && onCreate(guest);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
api.create(serializer, new Guest(undefined, name, undefined, group, undefined, status), ()=> {
|
api.create(serializer, new Guest(undefined, name, undefined, status, [], groups.find((g) => g.id === group)), (newGuest)=> {
|
||||||
resetForm();
|
resetForm();
|
||||||
onCreate && onCreate();
|
onCreate && onCreate(newGuest);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ export default function guestsTable({ guests, onUpdate, onEdit }: {
|
|||||||
{guest.name}
|
{guest.name}
|
||||||
</td>
|
</td>
|
||||||
<td className="px-6 py-4">
|
<td className="px-6 py-4">
|
||||||
{guest.group_name}
|
{guest.group?.name}
|
||||||
</td>
|
</td>
|
||||||
<td className="px-6 py-4">
|
<td className="px-6 py-4">
|
||||||
<span className="flex items-center text-sm dark:text-white me-3">
|
<span className="flex items-center text-sm dark:text-white me-3">
|
||||||
|
@ -27,7 +27,15 @@ const mockGuestsAPI = ({ page }: { page: Page }) => {
|
|||||||
|
|
||||||
await route.fulfill({ json })
|
await route.fulfill({ json })
|
||||||
} else if (route.request().method() === 'POST') {
|
} else if (route.request().method() === 'POST') {
|
||||||
const json = {};
|
const json = {
|
||||||
|
"id":"ff58aa2d-643d-4c29-be9c-50e10ae6853c",
|
||||||
|
"name":"John Snow",
|
||||||
|
"status":"invited",
|
||||||
|
"group": {
|
||||||
|
"id": "da8edf26-3e1e-4cbb-b985-450c49fffe01",
|
||||||
|
"name": "Work",
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
await route.fulfill({ json });
|
await route.fulfill({ json });
|
||||||
}
|
}
|
||||||
@ -104,6 +112,7 @@ test('should allow creating a new guest', async ({ page }) => {
|
|||||||
await mockGroupsAPI({ page });
|
await mockGroupsAPI({ page });
|
||||||
|
|
||||||
await page.goto('/default/dashboard/guests');
|
await page.goto('/default/dashboard/guests');
|
||||||
|
await expect(page.getByText('There are 2 elements in the list')).toBeVisible();
|
||||||
await page.getByRole('button', { name: 'Add new' }).click();
|
await page.getByRole('button', { name: 'Add new' }).click();
|
||||||
|
|
||||||
await page.getByRole('dialog').getByLabel('Name').fill('John Snow');
|
await page.getByRole('dialog').getByLabel('Name').fill('John Snow');
|
||||||
@ -119,6 +128,13 @@ test('should allow creating a new guest', async ({ page }) => {
|
|||||||
await page.keyboard.press('Enter');
|
await page.keyboard.press('Enter');
|
||||||
|
|
||||||
await page.getByRole('dialog').getByRole('button', { name: 'Create' }).click();
|
await page.getByRole('dialog').getByRole('button', { name: 'Create' }).click();
|
||||||
|
|
||||||
|
await expect(page.getByText('There are 3 elements in the list')).toBeVisible();
|
||||||
|
|
||||||
|
await expect(page.getByRole('row').nth(1).getByRole('cell', { name: 'John Snow' })).toBeVisible();
|
||||||
|
await expect(page.getByRole('row').nth(1).getByRole('cell', { name: 'Work' })).toBeVisible();
|
||||||
|
await expect(page.getByRole('row').nth(1).getByRole('cell', { name: 'Invited' })).toBeVisible();
|
||||||
|
await expect(page.getByRole('row').nth(1).locator('svg')).toHaveCount(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should display the list of groups', async ({ page }) => {
|
test('should display the list of groups', async ({ page }) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user