Permalink
Browse files

working mono repo with examples

  • Loading branch information...
James Baxley
James Baxley committed Dec 5, 2017
1 parent 51ac308 commit 7081fdc199035e750d05452d705b304f437b958e
Showing with 1,008 additions and 92 deletions.
  1. +11 −0 examples/async/.gitignore
  2. +36 −0 examples/async/README.md
  3. +23 −0 examples/async/package.json
  4. +21 −0 examples/async/public/index.html
  5. +50 −0 examples/async/src/actions/index.js
  6. +23 −0 examples/async/src/components/Picker.js
  7. +12 −0 examples/async/src/components/Posts.js
  8. +98 −0 examples/async/src/containers/App.js
  9. +22 −0 examples/async/src/index.js
  10. +70 −0 examples/async/src/reducers/index.js
  11. +21 −0 examples/todo/.gitignore
  12. +21 −0 examples/todo/LICENSE
  13. +17 −0 examples/todo/README.md
  14. +26 −0 examples/todo/package.json
  15. BIN examples/todo/public/favicon.ico
  16. +50 −0 examples/todo/public/index.html
  17. +15 −0 examples/todo/public/manifest.json
  18. +13 −0 examples/todo/src/components/App.js
  19. +14 −0 examples/todo/src/components/Footer.js
  20. +22 −0 examples/todo/src/components/Link.js
  21. +14 −0 examples/todo/src/components/Todo.js
  22. +14 −0 examples/todo/src/components/TodoList.js
  23. +38 −0 examples/todo/src/containers/AddTodo.js
  24. +31 −0 examples/todo/src/containers/FilterLink.js
  25. +52 −0 examples/todo/src/containers/VisibleTodoList.js
  26. +52 −0 examples/todo/src/createServiceWorker.js
  27. +21 −0 examples/todo/src/index.js
  28. +5 −0 examples/todo/src/schema/index.js
  29. +41 −0 examples/todo/src/schema/todos.js
  30. +20 −0 examples/todo/src/schema/visbilityFilter.js
  31. +6 −0 lerna.json
  32. +31 −71 package.json
  33. +96 −0 packages/apollo-link-state/package.json
  34. 0 { → packages/apollo-link-state}/rollup.config.js
  35. +2 −2 { → packages/apollo-link-state}/src/__tests__/advanced.ts
  36. 0 { → packages/apollo-link-state}/src/__tests__/client.ts
  37. 0 { → packages/apollo-link-state}/src/__tests__/index.ts
  38. +20 −19 { → packages/apollo-link-state}/src/index.ts
  39. 0 { → packages/apollo-link-state}/src/utils.ts
  40. 0 { → packages/apollo-link-state}/tsconfig.json
  41. 0 { → packages/apollo-link-state}/tslint.json
@@ -0,0 +1,11 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
node_modules

# production
build

# misc
.DS_Store
npm-debug.log
@@ -0,0 +1,36 @@
# Redux Async Example

This project template was built with [Create React App](https://github.com/facebookincubator/create-react-app), which provides a simple way to start React projects with no build configuration needed.

Projects built with Create-React-App include support for ES6 syntax, as well as several unofficial / not-yet-final forms of Javascript syntax such as Class Properties and JSX. See the list of [language features and polyfills supported by Create-React-App](https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#supported-language-features-and-polyfills) for more information.

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.<br>
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.<br>
You will also see any lint errors in the console.

### `npm run build`

Builds the app for production to the `build` folder.<br>
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.<br>
Your app is ready to be deployed!

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

@@ -0,0 +1,23 @@
{
"name": "async",
"version": "0.0.1",
"private": true,
"devDependencies": {
"react-scripts": "^1.0.2",
"redux-logger": "^3.0.6"
},
"dependencies": {
"prop-types": "^15.5.10",
"react": "^15.5.0",
"react-dom": "^15.5.0",
"react-redux": "^5.0.5",
"redux": "^3.5.2",
"redux-thunk": "^2.1.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"eject": "react-scripts eject",
"test": "react-scripts test"
}
}
@@ -0,0 +1,21 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Redux Async Example</title>
</head>
<body>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` in this folder.
To create a production bundle, use `npm run build`.
-->
</body>
</html>
@@ -0,0 +1,50 @@
export const REQUEST_POSTS = 'REQUEST_POSTS';
export const RECEIVE_POSTS = 'RECEIVE_POSTS';
export const SELECT_SUBREDDIT = 'SELECT_SUBREDDIT';
export const INVALIDATE_SUBREDDIT = 'INVALIDATE_SUBREDDIT';

export const selectSubreddit = subreddit => ({
type: SELECT_SUBREDDIT,
subreddit,
});

export const invalidateSubreddit = subreddit => ({
type: INVALIDATE_SUBREDDIT,
subreddit,
});

export const requestPosts = subreddit => ({
type: REQUEST_POSTS,
subreddit,
});

export const receivePosts = (subreddit, json) => ({
type: RECEIVE_POSTS,
subreddit,
posts: json.data.children.map(child => child.data),
receivedAt: Date.now(),
});

const fetchPosts = subreddit => dispatch => {
dispatch(requestPosts(subreddit));
return fetch(`https://www.reddit.com/r/${subreddit}.json`)
.then(response => response.json())
.then(json => dispatch(receivePosts(subreddit, json)));
};

const shouldFetchPosts = (state, subreddit) => {
const posts = state.postsBySubreddit[subreddit];
if (!posts) {
return true;
}
if (posts.isFetching) {
return false;
}
return posts.didInvalidate;
};

export const fetchPostsIfNeeded = subreddit => (dispatch, getState) => {
if (shouldFetchPosts(getState(), subreddit)) {
return dispatch(fetchPosts(subreddit));
}
};
@@ -0,0 +1,23 @@
import React from 'react';
import PropTypes from 'prop-types';

const Picker = ({ value, onChange, options }) => (
<span>
<h1>{value}</h1>
<select onChange={e => onChange(e.target.value)} value={value}>
{options.map(option => (
<option value={option} key={option}>
{option}
</option>
))}
</select>
</span>
);

Picker.propTypes = {
options: PropTypes.arrayOf(PropTypes.string.isRequired).isRequired,
value: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
};

export default Picker;
@@ -0,0 +1,12 @@
import React from 'react';
import PropTypes from 'prop-types';

const Posts = ({ posts }) => (
<ul>{posts.map((post, i) => <li key={i}>{post.title}</li>)}</ul>
);

Posts.propTypes = {
posts: PropTypes.array.isRequired,
};

export default Posts;
@@ -0,0 +1,98 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import {
selectSubreddit,
fetchPostsIfNeeded,
invalidateSubreddit,
} from '../actions';
import Picker from '../components/Picker';
import Posts from '../components/Posts';

class App extends Component {
static propTypes = {
selectedSubreddit: PropTypes.string.isRequired,
posts: PropTypes.array.isRequired,
isFetching: PropTypes.bool.isRequired,
lastUpdated: PropTypes.number,
dispatch: PropTypes.func.isRequired,
};

componentDidMount() {
const { dispatch, selectedSubreddit } = this.props;
dispatch(fetchPostsIfNeeded(selectedSubreddit));
}

componentWillReceiveProps(nextProps) {
if (nextProps.selectedSubreddit !== this.props.selectedSubreddit) {
const { dispatch, selectedSubreddit } = nextProps;
dispatch(fetchPostsIfNeeded(selectedSubreddit));
}
}

handleChange = nextSubreddit => {
this.props.dispatch(selectSubreddit(nextSubreddit));
};

handleRefreshClick = e => {
e.preventDefault();

const { dispatch, selectedSubreddit } = this.props;
dispatch(invalidateSubreddit(selectedSubreddit));
dispatch(fetchPostsIfNeeded(selectedSubreddit));
};

render() {
const { selectedSubreddit, posts, isFetching, lastUpdated } = this.props;
const isEmpty = posts.length === 0;
return (
<div>
<Picker
value={selectedSubreddit}
onChange={this.handleChange}
options={['reactjs', 'frontend']}
/>
<p>
{lastUpdated && (
<span>
Last updated at {new Date(lastUpdated).toLocaleTimeString()}.{' '}
</span>
)}
{!isFetching && (
<button onClick={this.handleRefreshClick}>Refresh</button>
)}
</p>
{isEmpty ? (
isFetching ? (
<h2>Loading...</h2>
) : (
<h2>Empty.</h2>
)
) : (
<div style={{ opacity: isFetching ? 0.5 : 1 }}>
<Posts posts={posts} />
</div>
)}
</div>
);
}
}

const mapStateToProps = state => {
const { selectedSubreddit, postsBySubreddit } = state;
const { isFetching, lastUpdated, items: posts } = postsBySubreddit[
selectedSubreddit
] || {
isFetching: true,
items: [],
};

return {
selectedSubreddit,
posts,
isFetching,
lastUpdated,
};
};

export default connect(mapStateToProps)(App);
@@ -0,0 +1,22 @@
import React from 'react';
import { render } from 'react-dom';
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';
import { createLogger } from 'redux-logger';
import reducer from './reducers';
import App from './containers/App';

const middleware = [thunk];
if (process.env.NODE_ENV !== 'production') {
middleware.push(createLogger());
}

const store = createStore(reducer, applyMiddleware(...middleware));

render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root'),
);
@@ -0,0 +1,70 @@
import { combineReducers } from 'redux';
import {
SELECT_SUBREDDIT,
INVALIDATE_SUBREDDIT,
REQUEST_POSTS,
RECEIVE_POSTS,
} from '../actions';

const selectedSubreddit = (state = 'reactjs', action) => {
switch (action.type) {
case SELECT_SUBREDDIT:
return action.subreddit;
default:
return state;
}
};

const posts = (
state = {
isFetching: false,
didInvalidate: false,
items: [],
},
action,
) => {
switch (action.type) {
case INVALIDATE_SUBREDDIT:
return {
...state,
didInvalidate: true,
};
case REQUEST_POSTS:
return {
...state,
isFetching: true,
didInvalidate: false,
};
case RECEIVE_POSTS:
return {
...state,
isFetching: false,
didInvalidate: false,
items: action.posts,
lastUpdated: action.receivedAt,
};
default:
return state;
}
};

const postsBySubreddit = (state = {}, action) => {
switch (action.type) {
case INVALIDATE_SUBREDDIT:
case RECEIVE_POSTS:
case REQUEST_POSTS:
return {
...state,
[action.subreddit]: posts(state[action.subreddit], action),
};
default:
return state;
}
};

const rootReducer = combineReducers({
postsBySubreddit,
selectedSubreddit,
});

export default rootReducer;
@@ -0,0 +1,21 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
/node_modules

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
Oops, something went wrong.

0 comments on commit 7081fdc

Please sign in to comment.