-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from salsita/next
v1.0.0
- Loading branch information
Showing
23 changed files
with
511 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["es2015", "stage-2", "react"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# redux-side-effects-example | ||
|
||
Run example: | ||
|
||
``` | ||
npm install | ||
npm start | ||
open http://localhost:3000 | ||
``` | ||
|
||
Run tests: | ||
|
||
``` | ||
npm test | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>redux-side-effects-example</title> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script type="text/javascript" src="/app.bundle.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "redux-side-effects-example", | ||
"version": "0.1.0", | ||
"scripts": { | ||
"start": "./node_modules/.bin/webpack-dev-server --config webpack.config.js --port 3000 --hot --content-base ./", | ||
"test": "./node_modules/.bin/mocha --require babel-core/register --recursive --require babel-polyfill", | ||
"test:watch": "npm test -- --watch" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.5.1", | ||
"babel-core": "^6.5.2", | ||
"babel-eslint": "^4.1.8", | ||
"babel-loader": "^6.2.2", | ||
"babel-preset-es2015": "^6.5.0", | ||
"babel-preset-react": "^6.5.0", | ||
"babel-preset-stage-2": "^6.5.0", | ||
"chai": "^3.4.1", | ||
"mocha": "^2.2.5", | ||
"webpack": "^1.12.4", | ||
"webpack-dev-server": "^1.12.1" | ||
}, | ||
"dependencies": { | ||
"babel-runtime": "^6.5.0", | ||
"bluebird": "^3.3.4", | ||
"react": "^0.14.7", | ||
"react-dom": "^0.14.2", | ||
"react-redux": "^4.0.0", | ||
"react-router": "^2.0.0", | ||
"react-router-redux": "^4.0.0", | ||
"redux": "^3.3.1", | ||
"redux-elm": "^1.0.0-alpha", | ||
"redux-side-effects": "^1.0.0", | ||
"superagent": "^1.8.2", | ||
"superagent-bluebird-promise": "^3.0.0" | ||
}, | ||
"author": "Tomas Weiss <tomasw@salsitasoft.com>", | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
|
||
export default connect(appState => appState)(({ gifUrl, topic, loading, dispatch }) => { | ||
if (!loading) { | ||
return ( | ||
<div> | ||
{gifUrl && <img src={gifUrl} width={200} height={200} />} | ||
<br /> | ||
Topic: <input type="text" onChange={ev => dispatch({ type: 'CHANGE_TOPIC', payload: ev.target.value })} value={topic} /><br /> | ||
<button onClick={() => dispatch({ type: 'LOAD_GIF' })}>Load GIF!</button> | ||
</div> | ||
); | ||
} else { | ||
return <div>Loading</div>; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React from 'react'; | ||
import { render } from 'react-dom'; | ||
import { createStore, compose } from 'redux'; | ||
import { Provider } from 'react-redux'; | ||
import { createEffectCapableStore } from 'redux-side-effects'; | ||
|
||
import reducer from './reducer'; | ||
import GifViewer from './GifViewer'; | ||
|
||
const storeFactory = compose( | ||
createEffectCapableStore, | ||
window.devToolsExtension ? window.devToolsExtension() : f => f | ||
)(createStore); | ||
|
||
const store = storeFactory(reducer); | ||
|
||
render(( | ||
<Provider store={store}> | ||
<GifViewer /> | ||
</Provider> | ||
), document.getElementById('app')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import request from 'superagent-bluebird-promise'; | ||
import { sideEffect } from 'redux-side-effects'; | ||
|
||
export const loadGifEffect = (dispatch, topic) => { | ||
request(`http://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=${topic}`) | ||
.then(response => dispatch({ type: 'NEW_GIF', payload: response.body.data.image_url })); | ||
}; | ||
|
||
const initialAppState = { | ||
gifUrl: null, | ||
loading: false, | ||
topic: 'funny cats' | ||
}; | ||
|
||
export default function* (appState = initialAppState, action) { | ||
switch (action.type) { | ||
case 'CHANGE_TOPIC': | ||
return { | ||
...appState, | ||
topic: action.payload | ||
}; | ||
|
||
case 'LOAD_GIF': | ||
yield sideEffect(loadGifEffect, appState.topic); | ||
|
||
return { | ||
...appState, | ||
loading: true | ||
}; | ||
|
||
case 'NEW_GIF': | ||
return { | ||
...appState, | ||
loading: false, | ||
gifUrl: action.payload | ||
}; | ||
default: | ||
return appState; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { assert } from 'chai'; | ||
import { sideEffect } from 'redux-side-effects'; | ||
|
||
import reducer, { loadGifEffect } from '../src/reducer'; | ||
|
||
describe('reducer test', () => { | ||
it('should set loading flag and yield side effect to load gif with specific topic after clicking the button', () => { | ||
const initialAppState = reducer(undefined, { type: 'init' }).next().value; | ||
const iterable = reducer(initialAppState, { type: 'LOAD_GIF' }); | ||
|
||
assert.deepEqual(iterable.next(), { | ||
done: false, | ||
value: sideEffect(loadGifEffect, 'funny cats') | ||
}); | ||
assert.deepEqual(iterable.next(), { | ||
done: true, | ||
value: { | ||
gifUrl: null, | ||
loading: true, | ||
topic: 'funny cats' | ||
} | ||
}); | ||
}); | ||
|
||
it('should reset the loading flag and set appropriate GIF url when GIF is loaded', () => { | ||
const iterable = reducer({ | ||
gifUrl: null, | ||
loading: true, | ||
topic: 'funny cats' | ||
}, { type: 'NEW_GIF', payload: 'newurl' }); | ||
|
||
assert.deepEqual(iterable.next(), { | ||
done: true, | ||
value: { | ||
gifUrl: 'newurl', | ||
loading: false, | ||
topic: 'funny cats' | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var path = require('path'); | ||
var webpack = require('webpack'); | ||
|
||
module.exports = { | ||
debug: true, | ||
target: 'web', | ||
devtool: 'sourcemap', | ||
plugins: [ | ||
new webpack.NoErrorsPlugin() | ||
], | ||
entry: [ | ||
'babel-polyfill', | ||
'webpack-dev-server/client?http://localhost:3000', | ||
'webpack/hot/only-dev-server', | ||
'./src/main.js' | ||
], | ||
output: { | ||
path: path.join(__dirname, './dev'), | ||
filename: 'app.bundle.js' | ||
}, | ||
module: { | ||
loaders: [{ | ||
test: /\.jsx$|\.js$/, | ||
loaders: ['babel-loader'], | ||
include: path.join(__dirname, './src') | ||
}] | ||
}, | ||
resolve: { | ||
extensions: ['', '.js', '.jsx'] | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.