Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Js codes #510

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,17 @@
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
18 changes: 18 additions & 0 deletions src/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
import axios from "axios";
import { dispatch } from "rxjs/internal/observable/pairs";
export const FETCH_START = "FETCH_START";
export const FETCH_SUCCESS = "FETCH_SUCCESS";
export const FETCH_FAILURE = "FETCH_FAILURE";

export const getPeople = () => dispatch => {
dispatch({type: FETCH_START})

axios
.get("http://swapi.co/api/people")
.then(res => {
console.log(res.data)
dispatch({type: FETCH_SUCCESS, payload: res.data})
})
.catch(err => dispatch({type: FETCH_FAILURE, payload: err}))
}

// we'll need axios

// we'll need to create 3 different action types here.
Expand Down
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import ReactDOM from "react-dom";
import "./index.css";
import App from "./App";
import { Provider } from "react-redux";
import { createStore } from "redux";
import thunk from "redux-thunk"
import { createStore, applyMiddleware } from "redux";
import rootReducer from "./reducers";
import logger from "redux-logger";
// needed dependancies
// applyMiddleware from redux
// thunk from redux-thunk
// logger from redux-logger
// rootReducer from ./reducers

const store = createStore(
rootReducer
/* applyMiddleware goes here */
rootReducer,
(applyMiddleware(logger, thunk))
);

ReactDOM.render(
Expand Down
30 changes: 27 additions & 3 deletions src/reducers/starWarsReducer.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
import /* we need our action types here*/ "../actions";
import {FETCH_START, FETCH_SUCCESS, FETCH_FAILURE} from "../actions";

const initialState = {
characters: []
characters: [],
error: '',
isLoading: false,
starWars: null
// Array characters, Boolean fetching, null error.
};
export const charsReducer = (state = initialState, action) => {
const charsReducer = (state = initialState, action) => {
switch (action.type) {
case FETCH_START:
return {
...state,
error: null,
isLoading: true
};
case FETCH_SUCCESS:
return {
...state,
isLoading: false,
characters: [...state.characters, ...action.payload]
};
case FETCH_FAILURE:
return {
...state,
isLoading: false,
error: "I..I...i dont even want to hear it. Erase whatever you put in WRONG, and try again!!!"
}
// Fill me in with the important reducers
// action types should be FETCHING, SUCCESS and FAILURE
// your switch statement should handle all of these cases.
default:
return state;
}
};

export default charsReducer;
23 changes: 16 additions & 7 deletions src/views/CharacterListView.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react";
import { connect } from "react-redux";

import { CharacterList } from "../components";
import {getPeople} from "../actions"
// import actions

class CharacterListView extends React.Component {
Expand All @@ -10,11 +11,15 @@ class CharacterListView extends React.Component {
}

componentDidMount() {
this.props.getPeople()
// call our action
}

render() {
if (this.props.fetching) {
return (
<h1>Im Getting Dressed, give me a second!</h1>
)
// return something here to indicate that you are fetching data
}
return (
Expand All @@ -25,11 +30,15 @@ class CharacterListView extends React.Component {
}
}

// our mapStateToProps needs to have two properties inherited from state
// the characters and the fetching boolean
export default connect(
null /* mapStateToProps replaces null here */,
{
/* action creators go here */
const mapStateToProps = state => {
return {
characters: state.charsReducer.characters,
fetching: state.charsReducer.fetching,
error: state.charsReducer.error
}
)(CharacterListView);
}

export default connect(
mapStateToProps,
{getPeople})
(CharacterListView);