-
-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modify client side to delete the video using the delete endpoint
- Loading branch information
1 parent
f0297e3
commit b798816
Showing
1 changed file
with
60 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<div> | ||
|
||
<h2> Video List</h2> | ||
<VideoForm addVideo={addVideo} /> | ||
|
||
<ul> | ||
{videos.map((video, index) => ( | ||
<li key={index}> | ||
<a href={video.src} target="_blank" rel="noopener noreferrer"> | ||
{video.title} | ||
</a> | ||
<button onClick={() => deleteVideo(index)}>Delete</button> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
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 ( | ||
<div> | ||
<h2>Video List</h2> | ||
<VideoForm addVideo={addVideo} /> | ||
|
||
<ul> | ||
{videos.map((video) => ( | ||
<li key={video.id}> | ||
<a href={video.src} target="_blank" rel="noopener noreferrer"> | ||
{video.title} | ||
</a> | ||
<button onClick={() => deleteVideo(video.id)}>Delete</button> | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Videos; |