Add newly created invitations to the start of the list
This commit is contained in:
parent
790d75d2ff
commit
b820bc837a
@ -4,7 +4,7 @@ import { Entity } from '@/app/lib/definitions';
|
|||||||
import { getCsrfToken, getSlug } from '@/app/lib/utils';
|
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: () => void): void;
|
||||||
update(serializable: Serializable<T>, object: T, callback: () => void): void;
|
update(serializable: Serializable<T>, object: T, callback: () => void): void;
|
||||||
@ -52,7 +52,7 @@ export class AbstractApi<T extends Entity> implements Api<T> {
|
|||||||
.catch((error) => console.error(error));
|
.catch((error) => console.error(error));
|
||||||
}
|
}
|
||||||
|
|
||||||
create(serializable: Serializable<T>, object: T, callback: () => void): void {
|
create(serializable: Serializable<T>, object: T, callback: (createdObject: T) => void): void {
|
||||||
fetch(`/api/${getSlug()}/${serializable.apiPath()}`, {
|
fetch(`/api/${getSlug()}/${serializable.apiPath()}`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: serializable.toJson(object),
|
body: serializable.toJson(object),
|
||||||
@ -60,7 +60,11 @@ export class AbstractApi<T extends Entity> implements Api<T> {
|
|||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'X-CSRF-TOKEN': getCsrfToken(),
|
'X-CSRF-TOKEN': getCsrfToken(),
|
||||||
}
|
}
|
||||||
}).then(callback)
|
})
|
||||||
|
.then((response) => response.json())
|
||||||
|
.then((data) => {
|
||||||
|
callback(serializable.fromJson(data));
|
||||||
|
})
|
||||||
.catch((error) => console.error(error));
|
.catch((error) => console.error(error));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,7 +13,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.group_name, guest.groupId, guest.color, guest.status, guest.children)));
|
||||||
}
|
}
|
||||||
|
|
||||||
toJson(invitation: Invitation): string {
|
toJson(invitation: Invitation): string {
|
||||||
|
@ -78,13 +78,14 @@ function GuestCard(guest: Guest) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function InvitationsBoard({ guests, invitations }: {
|
export default function InvitationsBoard({ guests, invitations: originalInvitations }: {
|
||||||
guests: Array<Guest>,
|
guests: Array<Guest>,
|
||||||
invitations: Array<Invitation>
|
invitations: Array<Invitation>
|
||||||
}) {
|
}) {
|
||||||
const api = new AbstractApi<Invitation>();
|
const api = new AbstractApi<Invitation>();
|
||||||
const serializer = new InvitationSerializer();
|
const serializer = new InvitationSerializer();
|
||||||
|
|
||||||
|
const [invitations, setInvitations] = useState<Invitation[]>(originalInvitations);
|
||||||
const [unassignedGuests, setUnassignedGuests] = useState<Guest[]>(
|
const [unassignedGuests, setUnassignedGuests] = useState<Guest[]>(
|
||||||
guests.filter(
|
guests.filter(
|
||||||
(guest) => !invitations.some((invitation) =>
|
(guest) => !invitations.some((invitation) =>
|
||||||
@ -93,6 +94,7 @@ export default function InvitationsBoard({ guests, invitations }: {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
// Sort invitations to display those without guests at the top
|
// Sort invitations to display those without guests at the top
|
||||||
const sortedInvitations = [...invitations].sort((a, b) => {
|
const sortedInvitations = [...invitations].sort((a, b) => {
|
||||||
if (a.guests.length === 0 && b.guests.length > 0) return -1;
|
if (a.guests.length === 0 && b.guests.length > 0) return -1;
|
||||||
@ -101,8 +103,9 @@ export default function InvitationsBoard({ guests, invitations }: {
|
|||||||
});
|
});
|
||||||
|
|
||||||
function handleCreateInvitation() {
|
function handleCreateInvitation() {
|
||||||
api.create(serializer, new Invitation(), () => {
|
api.create(serializer, new Invitation(), (createdInvitation) => {
|
||||||
console.log("Invitation created successfully");
|
console.log(`Invitation created successfully with ID: ${createdInvitation.id}`);
|
||||||
|
setInvitations((prevInvitations) => [createdInvitation, ...prevInvitations]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user