Skip to content

Commit 77d6633

Browse files
adds new code.
1 parent 19a1982 commit 77d6633

File tree

10 files changed

+246
-9
lines changed

10 files changed

+246
-9
lines changed

src/App.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,43 @@
11
import React from 'react';
2+
3+
import { connect } from 'react-redux';
24
import './App.css';
5+
import { useEffect } from 'react';
6+
7+
import { getGifs } from './actions';
8+
9+
import GifList from './components/GifList';
10+
import GifForm from './components/GifForm';
11+
12+
function App(props) {
13+
const { loading, error, getGifs } = props;
14+
15+
useEffect(()=> {
16+
getGifs("jump");
17+
}, []);
318

4-
function App() {
519
return (
620
<div className="App">
7-
Async Redux Project
21+
<h1>Search for Gifs</h1>
22+
<GifForm />
23+
24+
{
25+
(error !== "") && <h3>{error}</h3>
26+
}
27+
28+
{
29+
loading ? <h3>We are loading</h3> : <GifList/>
30+
}
31+
832
</div>
933
);
1034
}
1135

12-
export default App;
36+
const mapStateToProps = state => {
37+
return {
38+
loading: state.loading,
39+
error: state.error
40+
}
41+
}
42+
43+
export default connect(mapStateToProps, { getGifs })(App);

src/actions/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import axios from 'axios';
2+
3+
4+
export const FETCH_START = "FETCH_START";
5+
export const FETCH_SUCCESS = "FETCH_SUCCESS";
6+
7+
8+
export const getGifs = (searchTerm)=> {
9+
return(dispatch => {
10+
dispatch(fetchStart());
11+
axios.get(`https://api.giphy.com/v1/gifs/search?api_key=eF68i1SpJhoVnYQRTDKYcXdtpZlGeJDP&q=${searchTerm}`)
12+
.then(res=>{
13+
dispatch(fetchSuccess(res.data.data));
14+
})
15+
});
16+
}
17+
18+
export const fetchStart = () => {
19+
return({type: FETCH_START});
20+
}
21+
22+
export const fetchSuccess = gifs => {
23+
return({type:FETCH_SUCCESS, payload: gifs})
24+
}

src/components/Gif.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react';
2+
3+
const Gif = (props)=>{
4+
const { gif } = props;
5+
6+
return(<div className="gif">
7+
<h3>{gif.title}</h3>
8+
<p>{gif.username}</p>
9+
<img width="200" src={gif.images.original.url}/>
10+
<p>Rating: <strong>{gif.rating}</strong></p>
11+
<a target="_blank" href={gif.url}><button>Check it out on giffy</button></a>
12+
</div>);
13+
}
14+
15+
export default Gif;

src/components/GifForm.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react';
2+
import { connect } from 'react-redux';
3+
import { useState } from 'react';
4+
import { getGifs } from './../actions';
5+
6+
const GifForm = props => {
7+
const [searchTerm, setSearchTerm] = useState("");
8+
9+
const handleSubmit = (e) => {
10+
e.preventDefault();
11+
props.getGifs(searchTerm);
12+
}
13+
14+
const handleChange = (e) => {
15+
props.getGifs(e.target.value);
16+
}
17+
18+
return (<form onSubmit={handleSubmit}>
19+
<input onChange={handleChange}/>
20+
<button>Search</button>
21+
</form>);
22+
}
23+
24+
export default connect(null, { getGifs })(GifForm);

src/components/GifList.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react';
2+
import { connect } from 'react-redux';
3+
4+
import Gif from './Gif';
5+
6+
const GifList = props => {
7+
const { gifs } = props;
8+
9+
return (<div id="gifList">
10+
{
11+
gifs.map(gif => {
12+
return(<Gif key={gif.url} gif={gif}/>);
13+
})
14+
}
15+
</div>);
16+
}
17+
18+
const mapStateToProps = state => {
19+
return {
20+
gifs: state.gifs
21+
}
22+
}
23+
24+
export default connect(mapStateToProps)(GifList);

src/data/gifs.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
const data = [
3+
{
4+
type: "gif",
5+
id: "gzBwhbjfjdZzIaO9En",
6+
url: "https://giphy.com/gifs/studiosoriginals-gzBwhbjfjdZzIaO9En",
7+
username: "studiosoriginals",
8+
source: "",
9+
title: "Miss You Dogs GIF by GIPHY Studios Originals",
10+
rating: "g",
11+
import_datetime: "2021-01-15 21:34:13",
12+
trending_datetime: "2021-03-18 18:00:14",
13+
images: {
14+
original: {
15+
url: "https://media1.giphy.com/media/gzBwhbjfjdZzIaO9En/giphy.gif?cid=80bc7db2gun9ywhqob8y2c94d09e1ah8klz8q6l8re2muu5t&rid=giphy.gif",
16+
hash: "7cd04c1e38de9382e3f2628dc3e7aa46"
17+
}
18+
}
19+
},{
20+
type: "gif",
21+
id: "gzBwhbjfjdZzIaO9En",
22+
url: "https://media0.giphy.com/media/VeHBSH9F4GZpuuic00/giphy.gif?cid=80bc7db2gun9ywhqob8y2c94d09e1ah8klz8q6l8re2muu5t&rid=giphy.gif",
23+
username: "studiosoriginals",
24+
source: "",
25+
title: "Robot GIF by GIPHY Studios Originals",
26+
rating: "g",
27+
import_datetime: "2021-01-15 21:34:13",
28+
trending_datetime: "2021-03-18 18:00:14",
29+
images: {
30+
original: {
31+
url: "https://media0.giphy.com/media/VeHBSH9F4GZpuuic00/giphy.gif?cid=80bc7db2gun9ywhqob8y2c94d09e1ah8klz8q6l8re2muu5t&rid=giphy.gif",
32+
hash: "7cd04c1e38de9382e3f2628dc3e7aa46"
33+
}
34+
}
35+
},{
36+
type: "gif",
37+
id: "gzBwhbjfjdZzIaO9En",
38+
url: "https://giphy.com/gifs/dogs-good-job-pups-kBZBlLVlfECvOQAVno",
39+
username: "studiosoriginals",
40+
source: "",
41+
title: "Yahhhh by GIPHY Studios Originals",
42+
rating: "g",
43+
import_datetime: "2021-01-15 21:34:13",
44+
trending_datetime: "2021-03-18 18:00:14",
45+
images: {
46+
original: {
47+
url: "https://media0.giphy.com/media/kBZBlLVlfECvOQAVno/giphy.gif?cid=80bc7db2gun9ywhqob8y2c94d09e1ah8klz8q6l8re2muu5t&rid=giphy.gif",
48+
hash: "7cd04c1e38de9382e3f2628dc3e7aa46"
49+
}
50+
}
51+
}
52+
];
53+
54+
export default data;

src/index.css

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
21
body {
3-
background: #f5f5f5;
4-
}
2+
margin: 0;
3+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4+
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5+
sans-serif;
6+
-webkit-font-smoothing: antialiased;
7+
-moz-osx-font-smoothing: grayscale;
8+
}
9+
10+
code {
11+
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12+
monospace;
13+
}

src/index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
33

4-
import App from './App';
4+
import { createStore, applyMiddleware } from 'redux';
5+
import { Provider } from 'react-redux';
6+
import thunk from 'redux-thunk';
7+
58
import './index.css';
9+
import App from './App';
10+
11+
import reducer from './reducer';
12+
13+
const store = createStore(reducer, applyMiddleware(thunk));
614

715
ReactDOM.render(
8-
<App />,
16+
<Provider store={store}>
17+
<React.StrictMode>
18+
<App />
19+
</React.StrictMode>
20+
</Provider>,
921
document.getElementById('root')
10-
);
22+
);

src/reducer/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import data from './../data/gifs';
2+
3+
import { FETCH_START, FETCH_SUCCESS } from './../actions';
4+
5+
const initialState = {
6+
gifs: data,
7+
loading: false,
8+
error: ""
9+
}
10+
11+
const reducer = (state = initialState, action) => {
12+
switch(action.type) {
13+
case FETCH_START:
14+
return {
15+
...state,
16+
loading: true,
17+
error:""
18+
}
19+
case FETCH_SUCCESS:
20+
return {
21+
...state,
22+
loading: false,
23+
gifs: action.payload,
24+
error: ""
25+
}
26+
default:
27+
return(state);
28+
}
29+
}
30+
31+
export default reducer;

src/reportWebVitals.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const reportWebVitals = onPerfEntry => {
2+
if (onPerfEntry && onPerfEntry instanceof Function) {
3+
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
4+
getCLS(onPerfEntry);
5+
getFID(onPerfEntry);
6+
getFCP(onPerfEntry);
7+
getLCP(onPerfEntry);
8+
getTTFB(onPerfEntry);
9+
});
10+
}
11+
};
12+
13+
export default reportWebVitals;

0 commit comments

Comments
 (0)