Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NW6 | Zeliha Pala | Full-Stack-Project | Week-2 | DELETE-A- Video-Recommendation #34

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions client/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const App = () => {
return (
<>
<h1>Video Recommendations</h1>
<VideoList />
<NewVideoForm />
<VideoList />
<NewVideoForm />
</>
);
};
Expand Down
29 changes: 29 additions & 0 deletions client/src/DeleteVideoRecommendation.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from "react";

const DeleteVideoRecommendation = ({ videoId, onDelete }) => {
const handleDelete = () => {
fetch(`/api/videos/${videoId}`, {
method: "DELETE",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
})
.then((response) => {
if (!response.ok) {
throw new Error("Failed to delete the video");
}
return response.json();
})
.then((data) => {
onDelete(videoId);
})
.catch((error) => {
console.error(error);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Console.error is not a great solution here. The average user never looks in the console so you should make a habit of using a visible error like a alert()

});
};

return <button onClick={handleDelete}>Remove Video</button>;
};

export default DeleteVideoRecommendation;
16 changes: 9 additions & 7 deletions client/src/NewVideoForm.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from "react";

import "./NewVideoForm.css";

const NewVideoForm = () => {
const [title, setTitle] = useState("");
const [src, setSrc] = useState("");
Expand All @@ -26,10 +26,10 @@ const NewVideoForm = () => {
};

return (
<div>
<h2>Add New Video</h2>
<form onSubmit={handleSubmit}>
<div>
<div className="form-container">
<h2 className="form-header">Add New Video</h2>
<form className="form-body" onSubmit={handleSubmit}>
<div className="input-group">
<label htmlFor="title">Video Title: </label>
<input
type="text"
Expand All @@ -39,7 +39,7 @@ const NewVideoForm = () => {
required
/>
</div>
<div>
<div className="input-group">
<label htmlFor="src">Video URL: </label>
<input
type="url"
Expand All @@ -49,7 +49,9 @@ const NewVideoForm = () => {
required
/>
</div>
<button type="submit">Submit</button>
<button className="submit-button" type="submit">
Submit
</button>
</form>
</div>
);
Expand Down
12 changes: 10 additions & 2 deletions client/src/VideoRecommendations.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "./VideoRecommendations.css";
import React, { useState, useEffect } from "react";
import "./VideoRecommendations.css";
import DeleteVideoRecommendation from "./DeleteVideoRecommendation";

const VideoList = () => {
const [videos, setVideos] = useState([]);
Expand All @@ -21,14 +22,21 @@ const VideoList = () => {
});
}, []);

const handleDelete = (videoId) => {
setVideos(videos.filter((video) => video.id !== videoId));
};

return (
<div className="video-list">
{videos.map((videoData, i) => (
<div className="video" data-testid="video" key={i}>
<div>
{/* <a>{videoData.title}</a> */}
<a href={videoData.src}>{videoData.title}</a>
</div>
<DeleteVideoRecommendation
videoId={videoData.id}
onDelete={handleDelete}
/>
</div>
))}
</div>
Expand Down
15 changes: 15 additions & 0 deletions server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,19 @@ router.post("/videos", async (req, res) => {
res.status(200).json({ success: true, data: { id: newVideoId } });
});

router.delete("/videos/:id", async (req, res) => {
const videoId = req.params.id;

try {
await db.query("DELETE FROM videos WHERE id = $1", [videoId]);
res
.status(200)
.json({ success: true, message: "Video deleted successfully" });
} catch (error) {
res
.status(500)
.json({ success: false, error: "Failed to delete the video" });
Comment on lines +30 to +41
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good. I appreciate the use of appropriate HTTP error codes and paramatized queries.

}
});

export default router;
Loading