Skip to content

Commit

Permalink
Add confirmation dialog for delete functionality and it's styling
Browse files Browse the repository at this point in the history
  • Loading branch information
George7h committed Mar 7, 2024
1 parent b3405dc commit 62e99cd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/Components/DeletePlace.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect } from 'react';
import React, { useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { deleteItem } from '../Redux/places/deleteItemSlice';
import { fetchPlaces } from '../Redux/places/placesSlice';
Expand All @@ -10,17 +10,31 @@ const DeletePlace = () => {
const status = useSelector((state) => state.deleteItem.status);
const error = useSelector((state) => state.deleteItem.error);

const [showConfirmation, setShowConfirmation] = useState(false);
const [itemIdToDelete, setItemIdToDelete] = useState(null);

useEffect(() => {
dispatch(fetchPlaces());
}, [dispatch]);

const handleDelete = (id) => {
dispatch(deleteItem(id))
setShowConfirmation(true);
setItemIdToDelete(id);
};

const confirmDelete = () => {
dispatch(deleteItem(itemIdToDelete))
.then(() => {
dispatch(fetchPlaces());
setShowConfirmation(false);
});
};

const cancelDelete = () => {
setShowConfirmation(false);
setItemIdToDelete(null);
};

if (places.length === 0) {
return <div>No places to delete</div>;
}
Expand All @@ -41,12 +55,20 @@ const DeletePlace = () => {
return (
<div className="delete-place-container">
<h1>Delete Place</h1>

<ul className="delete-place-card">
{places.map((place) => (
<li key={place.id} className="card">
<h3>{place.description}</h3>
<img className="place-img" src={place.photo} alt={place.description} />
<button className="delete" type="submit" onClick={() => handleDelete(place.id)}>Delete</button>
<button className="delete" type="button" onClick={() => handleDelete(place.id)}>Delete</button>
{showConfirmation && (
<div className="confirmation-dialog">
<p>Are you sure?</p>
<button type="button" id="deleteyes" onClick={confirmDelete}>Yes</button>
<button type="button" id="deleteno" onClick={cancelDelete}>No</button>
</div>
)}
</li>
))}
</ul>
Expand Down
25 changes: 25 additions & 0 deletions src/DeleteItem.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,31 @@
margin: 10px;
}

.confirmation-dialog {
display: flex;
align-items: center;
gap: 10px;
margin: 10px;
}

#deleteyes {
background-color: hsl(0, 80%, 48%);
color: #fff;
border: none;
padding: 10px 25px;
border-radius: 5px;
cursor: pointer;
}

#deleteno {
background-color: hsl(144, 80%, 48%);
color: #fff;
border: none;
padding: 10px 25px;
border-radius: 5px;
cursor: pointer;
}

@media screen and (max-width: 600px) {
.delete-place-container {
display: flex;
Expand Down

0 comments on commit 62e99cd

Please sign in to comment.