Skip to content

Commit 5889676

Browse files
authored
add button to guests to be able to be removed (#9)
1 parent c5825c6 commit 5889676

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

app/assets/stylesheets/application.css

+10
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ input[type="submit"] {
152152
position: relative;
153153
display: flex;
154154
margin-bottom: 0.5rem;
155+
width: 50%;
155156

156157
label {
157158
visibility: hidden;
@@ -166,6 +167,15 @@ input[type="submit"] {
166167
padding: 0.5rem 1rem;
167168
margin-right: 0.5rem;
168169
}
170+
171+
button {
172+
flex: 1 0 auto;
173+
width: 100px;
174+
}
175+
176+
.guest-item-remove {
177+
background-color: #3A3B3C;
178+
}
169179
}
170180

171181
.guest-item-error {

app/javascript/event_guests_manager/components/GuestManager.jsx

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useCallback, useEffect, useState } from "react";
2-
import { addGuest, getGuestList } from "../utils";
2+
import { addGuest, getGuestList, removeGuest } from "../utils";
33

44
export const GuestManager = () => {
55
const [guests, setGuests] = useState([]);
@@ -46,6 +46,19 @@ export const GuestManager = () => {
4646
[newGuest],
4747
);
4848

49+
const handleRemoveGuest = useCallback(
50+
(guestId) => async () => {
51+
try {
52+
await removeGuest(guestId);
53+
const nextGuestList = await getGuestList();
54+
setGuests(nextGuestList);
55+
} catch (e) {
56+
setError("An error occurred while removing the guest.");
57+
}
58+
},
59+
[],
60+
);
61+
4962
return (
5063
<form aria-labelledby="guest-manager-heading" onSubmit={handleAddGuest}>
5164
{error && <div className="guest-item-error">{error}</div>}
@@ -71,6 +84,13 @@ export const GuestManager = () => {
7184
disabled
7285
readOnly
7386
/>
87+
<button
88+
type="button"
89+
onClick={handleRemoveGuest(guest.id)}
90+
className="guest-item-remove"
91+
>
92+
Remove
93+
</button>
7494
</div>
7595
))}
7696
<div className="guest-item">

app/javascript/event_guests_manager/utils/GuestUtils.js

+17
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,20 @@ export const addGuest = async ({ name, email }) => {
2929
throw new Error("Failed to add guest");
3030
}
3131
};
32+
33+
export const removeGuest = async (guestId) => {
34+
try {
35+
const response = await fetch(
36+
`${window.location.pathname}/guests/${guestId}`,
37+
{
38+
method: "DELETE",
39+
},
40+
);
41+
if (!response.ok) {
42+
throw new Error("Failed to remove guest");
43+
}
44+
} catch (error) {
45+
console.error("Error removing guest:", error);
46+
throw new Error("Failed to remove guest");
47+
}
48+
};

0 commit comments

Comments
 (0)