From b7988160aac31d9ad527879f8c33c3d02d22156b Mon Sep 17 00:00:00 2001 From: aishaathmanlali Date: Tue, 11 Jun 2024 15:55:46 +0100 Subject: [PATCH] modify client side to delete the video using the delete endpoint --- client/src/components/Videos.jsx | 113 ++++++++++++++++--------------- 1 file changed, 60 insertions(+), 53 deletions(-) diff --git a/client/src/components/Videos.jsx b/client/src/components/Videos.jsx index 53f1ece5bf..9eb9974789 100644 --- a/client/src/components/Videos.jsx +++ b/client/src/components/Videos.jsx @@ -1,58 +1,65 @@ -import { useEffect, useState } from 'react'; -import VideoForm from './VideoForm'; - +import { useEffect, useState } from "react"; +import VideoForm from "./VideoForm"; const Videos = () => { - const [videos, setVideos] = useState([]); - - useEffect(() => { - const fetchVideos = async () => { - try { - const response = await fetch('/api/videos'); - const data = await response.json(); - setVideos(data.videos); - console.log(data) - } catch (error) { - console.error('Error fetching videos:', error); - } - }; - - fetchVideos(); - }, []); - - - const addVideo = (video) => { - setVideos((prevVideos) => [...prevVideos, video]); - }; - - - - const deleteVideo = (index) => { - setVideos((prevVideos) => prevVideos.filter((_, i) => i !== index)); - }; - - - - - - return ( -
- -

Video List

- - -
    - {videos.map((video, index) => ( -
  • - - {video.title} - - -
  • - ))} -
-
- ); + const [videos, setVideos] = useState([]); + + useEffect(() => { + const fetchVideos = async () => { + try { + const response = await fetch("/api/videos"); + const data = await response.json(); + setVideos(data.videos); + console.log(data); + } catch (error) { + console.error("Error fetching videos:", error); + } + }; + + fetchVideos(); + }, []); + + const addVideo = (video) => { + setVideos((prevVideos) => [...prevVideos, video]); + }; + + const deleteVideo = async (id) => { + try { + const response = await fetch(`/api/videos/${id}`, { + method: "DELETE", + }); + + if (response.status === 204) { + setVideos((prevVideos) => + prevVideos.filter((video) => video.id !== id) + ); + } else if (response.status === 404) { + console.error("Video not found"); + } else { + console.error("Failed to delete video"); + } + } catch (error) { + console.error("Error deleting video:", error); + } + }; + + return ( +
+

Video List

+ + + +
+ ); }; export default Videos;