Skip to content
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
5 changes: 3 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = {
"es6": true
},
"extends": "eslint:recommended",
"parser": "babel-eslint",
"parserOptions": {
"ecmaFeatures": {
"experimentalObjectRestSpread": true,
Expand All @@ -31,8 +32,8 @@ module.exports = {
"single"
],
"semi": [
"error",
"always"
"warn",
"never"
]
}
};
7 changes: 7 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[ignore]

[include]

[libs]

[options]
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ Use this repo to get some practice making **small**, **composable** components.
- `import` the data into `index.js` and **set** it as a prop on your `App` component (in the `render` method)
- Work in small pieces, one step at a time - and **commit** your work every time you finish a feature.

### Homework 6/15

- create a new *functional* component called `UserProfile.js`. For now, just have it return an empty `<div></div>` or something.
(fix errors, commit your work)
- `import` the `sampleUser` into `App.js` and place it in your `constructor(props)` as an initial `state` value.
(fix errors, commit your work)
- *set* the user data you're holding in `state` as a `prop` on the `<UserProfile />` component (don't forget to `export` & `import` the UserProfile)
(fix errors, commit your work)
- fix any errors and verify that the `<UserProfile />` is receiving `props` from `<App />` by using the React DevTools
(fix errors, commit your work)
- Now add JSX to `UserProfile.js` that will allow you to render the `user` data from `props`
(fix errors, commit your work)

### Hints
- Start with the `UserProfile`.
- Don't try to do it all at once. Just write the component with statically defined data, and get it rendering:
Expand All @@ -50,3 +63,4 @@ function UserProfile (props) {
```
- Now, `import` the data from `sampleUser.json` to `index.js` and pass it down as `props` to your `UserProfile` component.
- Once you have data rendering via `props`, you'll want to use [`.map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map?v=control) to iterate over the Array of user data. If you access user data from `props`, you'll find that there's a way to easily swap out the single user in `sampleUser.json` with multiple users in `users.json`.
# flow-starter
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.16.2",
"eslint": "^3.19.0",
"react": "^15.5.4",
"react-dom": "^15.5.4"
"react-dom": "^15.5.4",
"react-redux": "^5.0.6",
"redux": "^3.7.2",
"redux-devtools-extension": "^2.13.2",
"redux-thunk": "^2.2.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-flow": "^6.23.0",
"eslint": "^3.19.0",
"eslint-plugin-react": "^7.0.1",
"flow-bin": "^0.54.1",
"react-scripts": "1.0.6"
},
"scripts": {
Expand Down
33 changes: 27 additions & 6 deletions src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import React from 'react';
import './App.css';
import React from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import ListOfUsers from './ListOfUsers'
import { fetchUsersAction } from '../redux/api'
import User from '../sampleUser.json'
import Users from '../randomUsers.json'
import './App.css'

function App(props) {
const App = (props) => {
return (
<div>
<h1 className="app-header">Hello React</h1>
<h1 className="app-header">Hello Zac</h1>
<ListOfUsers
users={Users}
/>
<button
onClick={props.fetchUsers}
>Click</button>
</div>
);
)
}

export default App;
const mapStateToProps = (state) => ({
api: state.users,
})
const mapDispatchToProps = (dispatch) => ({
fetchUsers: bindActionCreators(fetchUsersAction, dispatch),
})
export default connect(
mapStateToProps,
mapDispatchToProps
)(App)
Empty file added src/components/Button.js
Empty file.
6 changes: 3 additions & 3 deletions src/components/Header.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import React from 'react'

function Header(props) {
// Make it so
// Make it so
}

export default Header;
export default Header
30 changes: 30 additions & 0 deletions src/components/ListOfUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react'
import User from './User'

const renderUsers = (users) => {

const { results } = users

return results.map(
({ name, picture }, index) =>
<User
key={index}
name={name}
picture={picture}
index={index}
/>
)

}

const ListOfUsers = ({ users }) => {

return (
<ul>
{renderUsers(users)}
</ul>
)

}

export default ListOfUsers
21 changes: 21 additions & 0 deletions src/components/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @flow
import React from 'react'

type Props = {
name: Object,
picture: Object,
index: number,
}

export default ({
name,
picture,
index,
}: Props) =>
<div key={index}>
<li>{name.first} {name.last}</li>
<img
src={picture.thumbnail}
alt={name.first}
/>
</div>
35 changes: 28 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';
import registerServiceWorker from './registerServiceWorker';
import './index.css';
import React from 'react'
import ReactDOM from 'react-dom'
import { createStore, applyMiddleware } from 'redux'
import { Provider } from 'react-redux'
import { composeWithDevTools } from 'redux-devtools-extension'
import thunk from 'redux-thunk'
import App from './components/App'
import registerServiceWorker from './registerServiceWorker'
import Users from './sampleUser.json'
import reducer from './redux'
import './index.css'

ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();
const store = createStore(
reducer,
composeWithDevTools(
applyMiddleware(
thunk
)
)
)


ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
)
registerServiceWorker()
2 changes: 1 addition & 1 deletion src/randomUsers.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"users": [{
"results": [{
"name": {
"title": "mr",
"first": "fred",
Expand Down
84 changes: 84 additions & 0 deletions src/redux/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// @flow
import axios from 'axios'

const initialState = {
isFetching: false,
error: {
status: '',
message: '',
},
results: {},
}

const FETCH_USERS_BEGIN: string = 'FETCH_USERS_BEGIN'
const FETCH_USERS_SUCCESS: string = 'FETCH_USERS_SUCCESS'
const FETCH_USERS_ERROR: string = 'FETCH_USERS_ERROR'

type State = {|
+isFetching: boolean,
+error: {
+status: string,
+message: string,
},
+results: Object,
|}
type Action = {
type: string,
payload: Object,
}

const api = (state: State = initialState, action: Object) => {

switch (action.type) {
case FETCH_USERS_BEGIN:
return {
...state,
isFetching: true,
}
case FETCH_USERS_SUCCESS:
return {
...state,
isFetching: false,
users: {...action.payload[0]},
}
case FETCH_USERS_ERROR:
return {
...state,
isFetching: false,
error: {
...state.error,
message: 'error',
},
}
default:
return state
}

}

export const fetchUsersBegin = () =>
({ type: FETCH_USERS_BEGIN })
export const fetchUsersSuccess = (payload: Array<*>) =>
({ type: FETCH_USERS_SUCCESS, payload })
export const fetchUsersError = (error: Object) =>
({ type: FETCH_USERS_ERROR, error })

export const fetchUsersAction = (amount: number = 10) => {
return (dispatch: any) => {
dispatch(fetchUsersBegin())
return axios.get(`https://randomuser.me/api?results=${amount}`)
.then(res =>
dispatch(
fetchUsersSuccess(res.data.results)
)
)
.catch(err =>
dispatch(
fetchUsersError(err)
)
)
}

}

export default api
6 changes: 6 additions & 0 deletions src/redux/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { combineReducers } from 'redux'
import api from './api'

export default combineReducers({
api,
})
Loading