Skip to content

Commit e800f68

Browse files
react-12
1 parent 6dc422f commit e800f68

File tree

6 files changed

+105
-3
lines changed

6 files changed

+105
-3
lines changed

package-lock.json

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"@testing-library/jest-dom": "^5.16.5",
77
"@testing-library/react": "^13.4.0",
88
"@testing-library/user-event": "^13.5.0",
9+
"axios": "^1.2.5",
910
"react": "^18.2.0",
1011
"react-dom": "^18.2.0",
1112
"react-scripts": "5.0.1",

src/App.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ function App() {
1212
console.log('render App');
1313
const [editableVideo, setEditableVideo] = useState(null);
1414
const [mode, setMode] = useState('darkMode');
15+
16+
17+
1518
function videoReducer(videos, action) {
1619
switch (action.type) {
20+
case 'LOAD':
21+
return action.payload;
1722
case 'ADD':
1823
return [...videos, { ...action.payload, id: videos.length + 1 }];
1924
case 'DELETE':
@@ -28,7 +33,7 @@ function App() {
2833
return videos;
2934
}
3035
}
31-
const [videos, dispatch] = useReducer(videoReducer, videoDB);
36+
const [videos, dispatch] = useReducer(videoReducer, []);
3237

3338
function editVideo(id) {
3439
setEditableVideo(videos.find((video) => video.id === id));

src/components/AddVideo.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function AddVideo({editableVideo}) {
3737
setVideo(editableVideo)
3838
}
3939
inputRef.current.focus()
40+
4041
},[editableVideo])
4142

4243
return (

src/components/Video.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1-
import { useContext } from 'react';
1+
import { useContext, useEffect } from 'react';
22
import ThemeContext from '../context/ThemeContext';
33
import useVideoDispatch from '../hooks/VideoDispatch';
44
import './Video.css';
55

66
function Video({title,id,channel="Coder Dost",views,time,verified,children,editVideo}) {
7-
console.log('render Video')
7+
console.log('render Video',id)
88
const theme = useContext(ThemeContext)
99
const dispatch = useVideoDispatch()
10+
11+
12+
13+
14+
15+
// useEffect(()=>{
16+
// const idx= setInterval(()=>{
17+
// console.log('video playing', id)
18+
// },3000)
19+
20+
// return ()=>{
21+
// clearInterval(idx)
22+
// }
23+
24+
// },[id])
1025

1126
return (
1227
<>

src/components/VideoList.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
import Video from "./Video";
22
import PlayButton from "./PlayButton";
33
import useVideos from "../hooks/Videos";
4+
import axios from 'axios';
5+
import {useState, useEffect} from 'react';
6+
import useVideoDispatch from "../hooks/VideoDispatch";
47

58
function VideoList({editVideo}){
9+
const url ="https://my.api.mockaroo.com/video.json?key=2a12c4d0"
10+
611
const videos = useVideos()
12+
const dispatch = useVideoDispatch();
13+
14+
async function handleClick (){
15+
const res = await axios.get(url);
16+
console.log('getVideos', res.data)
17+
dispatch({type:'LOAD',payload:res.data})
18+
}
19+
20+
useEffect(()=>{
21+
async function getVideos (){
22+
const res = await axios.get(url);
23+
console.log('getVideos', res.data)
24+
dispatch({type:'LOAD',payload:res.data})
25+
}
26+
getVideos()
27+
},[dispatch])
28+
29+
730
return(
831
<>
932
{videos.map((video) => (
@@ -25,6 +48,7 @@ function VideoList({editVideo}){
2548
</PlayButton>
2649
</Video>
2750
))}
51+
<button onClick={handleClick}>Get Videos</button>
2852
</>
2953
)
3054
}

0 commit comments

Comments
 (0)