From 1466215d2fe567d670cf81d523d20f981cfbfe5e Mon Sep 17 00:00:00 2001 From: haiderGithubOfficial Date: Sat, 5 Aug 2023 07:58:44 +0500 Subject: [PATCH] implemented react - 6 --- src/App.css | 10 +++---- src/App.js | 29 +++++++++++++------- src/components/AddVideo.js | 39 +++++++++++++++++---------- src/components/Video.css | 54 ++++++++++++++++++++----------------- src/components/Video.js | 31 ++++++++++----------- src/components/VideoList.js | 48 +++++++++++++++++---------------- 6 files changed, 120 insertions(+), 91 deletions(-) diff --git a/src/App.css b/src/App.css index 9658cb1..ec9701a 100644 --- a/src/App.css +++ b/src/App.css @@ -1,6 +1,6 @@ - -.App{ +.App { background-color: #333; - width:100%; - height:100vh; -} \ No newline at end of file + width: 50%; + height: 100vh; + margin: 0 auto; +} diff --git a/src/App.js b/src/App.js index 7d67d1e..8e1d162 100644 --- a/src/App.js +++ b/src/App.js @@ -6,21 +6,30 @@ import VideoList from './components/VideoList'; function App() { console.log('render App') - const [videos,setVideos] = useState(videoDB); + const [videos, setVideos] = useState(videoDB); + const [editVideo, setEditVideo] = useState(null) - function addVideos(video){ - setVideos([ - ...videos, - {...video, id: videos.length+1} - ]); + function addVideos(video) { + setVideos([ + ...videos, + { ...video, id: videos.length + 1 } + ]); } - return ( -
console.log('App')}> - - + const editableVideo = (id) => { + let editVideo = videos.find(video => video.id === id) + setEditVideo(editVideo); + } + const updateVideo = (updatedVideo) => { + const newVideos = videos.map(video => video.id === updatedVideo.id ? updatedVideo : video); + setVideos([...newVideos]) + } + return ( +
console.log('App')}> + +
); } diff --git a/src/components/AddVideo.js b/src/components/AddVideo.js index 8c8cdb6..d640020 100644 --- a/src/components/AddVideo.js +++ b/src/components/AddVideo.js @@ -1,29 +1,40 @@ import './AddVideo.css'; -import {useState} from 'react'; +import { useEffect, useState } from 'react'; const initialState = { - time: '1 month ago', - channel: 'Coder Dost', - verified: true, - title:'', - views:'' - } + time: '1 month ago', + channel: 'Coder Dost', + verified: true, + title: '', + views: '' +} -function AddVideo({addVideos}) { - const [video, setVideo] = useState(initialState); +function AddVideo({ addVideos, editVideo, setEditVideo, updateVideo }) { + const [video, setVideo] = useState(editVideo || initialState); + setEditVideo(editVideo); function handleSubmit(e) { e.preventDefault(); - addVideos(video) + if (editVideo) { + updateVideo(video) + } else { + addVideos(video) + } setVideo(initialState) - + setEditVideo(null) } + function handleChange(e) { - setVideo({...video, - [e.target.name] : e.target.value + setVideo({ + ...video, + [e.target.name]: e.target.value }) } + useEffect(() => { + if (editVideo) setVideo({ ...editVideo }) + }, [editVideo]) + return (
- Add Video + {editVideo ? 'Edit Video' : 'Add Video'}
); diff --git a/src/components/Video.css b/src/components/Video.css index 62a8bc6..d1ab5c9 100644 --- a/src/components/Video.css +++ b/src/components/Video.css @@ -1,27 +1,33 @@ -.dark{ - background-color: gray; - color:white; +.dark { + background-color: gray; + color: white; } -.container{ - width:15em; - margin: 0 0.2em; - float: left; +.container { + width: 15em; + margin: 0 0.2em; + float: left; + position: relative; +} +.pic img { + width: 100%; +} +.title { + font-weight: bold; + margin-bottom: 1em; + color: white; +} +.channel, +.views { + font-size: small; + color: #ccc; + margin-bottom: 0.2em; +} + +.views span { + padding: 0 0.5em; +} +.edit { + position: absolute; + right: 0; } - .pic img{ - width: 100%; - } - .title{ - font-weight: bold; - margin-bottom: 1em; - color:white - } - .channel, .views{ - font-size: small; - color:#ccc; - margin-bottom: 0.2em; - } - - .views span{ - padding: 0 0.5em; - } \ No newline at end of file diff --git a/src/components/Video.js b/src/components/Video.js index 5d9e6bd..e8111e0 100644 --- a/src/components/Video.js +++ b/src/components/Video.js @@ -1,25 +1,26 @@ import './Video.css'; -function Video({title,id,channel="Coder Dost",views,time,verified,children}) { +function Video({ title, id, channel = "Coder Dost", views, time, verified, children, editableVideo }) { console.log('render Video') - + return ( - <> + <>
-
- Katherine Johnson -
-
{title}
-
{channel} {verified && '✅'}
-
- {views} views . {time} -
-
- {children} -
+ +
+ Katherine Johnson +
+
{title}
+
{channel} {verified && '✅'}
+
+ {views} views . {time} +
+
+ {children} +
- + ); } diff --git a/src/components/VideoList.js b/src/components/VideoList.js index 594dcfd..6ff70b9 100644 --- a/src/components/VideoList.js +++ b/src/components/VideoList.js @@ -1,30 +1,32 @@ import Video from "./Video"; import PlayButton from "./PlayButton"; -function VideoList({videos}){ +function VideoList({ videos, editableVideo }) { - return( - <> - {videos.map((video) => ( - - ))} - - ) + + return ( + <> + {videos.map((video) => ( + + ))} + + ) } export default VideoList \ No newline at end of file