Skip to content
This repository has been archived by the owner on Sep 12, 2020. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bmealhouse committed Jul 12, 2017
0 parents commit fd5ac02
Show file tree
Hide file tree
Showing 20 changed files with 7,266 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .all-contributorsrc
@@ -0,0 +1,22 @@
{
"projectName": "next-redux-saga",
"projectOwner": "bmealhouse",
"files": [
"README.md"
],
"imageSize": 100,
"commit": true,
"contributors": [
{
"login": "bmealhouse",
"name": "Brent Mealhouse",
"avatar_url": "https://avatars0.githubusercontent.com/u/3741255?v=3",
"profile": "https://twitter.com/bmealhouse",
"contributions": [
"code",
"doc",
"test"
]
}
]
}
3 changes: 3 additions & 0 deletions .babelrc
@@ -0,0 +1,3 @@
{
"presets": [["env", {"modules": "commonjs"}], "next/babel"]
}
16 changes: 16 additions & 0 deletions .babelrcold
@@ -0,0 +1,16 @@
{
"presets": [
[
"env",
{
"targets": {
"ie": 9,
"uglify": true
},
"useBuiltIns": false,
"modules": "commonjs"
}
],
"react-app"
]
}
9 changes: 9 additions & 0 deletions .editorconfig
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true
16 changes: 16 additions & 0 deletions .gitignore
@@ -0,0 +1,16 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.

# build
/.next

# dependencies
/node_modules

# testing
/coverage

# misc
.DS_Store
npm-debug.log*
yarn-debug.log*
yarn-error.log*
1 change: 1 addition & 0 deletions .node-version
@@ -0,0 +1 @@
lts
5 changes: 5 additions & 0 deletions .travis.yml
@@ -0,0 +1,5 @@
language: node_js
node_js:
- "6"
- "7"
- "8"
1 change: 1 addition & 0 deletions .yarnrc
@@ -0,0 +1 @@
save-prefix ""
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Brent Mealhouse

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
150 changes: 150 additions & 0 deletions README.md
@@ -0,0 +1,150 @@
# next-redux-saga

[![Build Status](https://travis-ci.org/bmealhouse/next-redux-saga.svg?branch=master)](https://travis-ci.org/bmealhouse/next-redux-saga)
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)

> redux-saga HOC for [Next.js](https://github.com/zeit/next.js/)
## Installation

```sh
yarn add next-redux-saga
```

## Usage

`next-redux-saga` uses the redux store created by [next-redux-wrapper](https://github.com/kirill-konshin/next-redux-wrapper). Please refer to their documentation for more information.

The working example below is based off [pages/index.js](https://github.com/bmealhouse/next-redux-saga/blob/master/pages/index.js).

> **Try it out:**
>
> 1. Clone this repository
> 2. Install dependencies: `yarn`
> 3. Start the project: `yarn start`
> 4. Visit [http://localhost:3000](http://localhost:3000)
**root-reducer.js**

```js
function rootReducer(state = {}, action) {
switch (action.type) {
case 'GET_REDUX_PROP':
return {...state, redux: action.data}
case 'GET_REDUX_SAGA_PROP_SUCCESS':
return {...state, reduxSaga: action.data}
default:
return state
}
}
```

**root-saga.js**

```js
import {delay} from 'redux-saga'
import {all, call, put, takeEvery} from 'redux-saga/effects'

function helloSaga() {
console.log('Hello Saga!')
}

function* getReduxSagaPropSaga() {
yield call(delay, 500)
yield put({
type: 'GET_REDUX_SAGA_PROP_SUCCESS',
data: 'Hello redux-saga!'
})
}

function* rootSaga() {
yield all([
call(helloSaga),
takeEvery('GET_REDUX_SAGA_PROP', getReduxSagaPropSaga)
])
}
```

**configure-store.js**

```js
import createSagaMiddleware from 'redux-saga'
import {createStore, applyMiddleware} from 'redux'
import rootReducer from './root-reducer'
import rootSaga from './root-saga'

const sagaMiddleware = createSagaMiddleware()

function configureStore(initialState) {
const store = createStore(
rootReducer,
initialState,
applyMiddleware(sagaMiddleware)
)

// NOTE: you must attach `sagaTask` to the store
store.sagaTask = sagaMiddleware.run(rootSaga)
return store
}
```

**example-page.js**

```js
import React, {Component} from 'react'
import {string} from 'prop-types'
import withRedux from 'next-redux-wrapper'
import withReduxSaga from 'next-redux-saga'

class ExamplePage extends Component {
static propTypes = {
static: string,
redux: string,
reduxSaga: string
}

static async getInitialProps({store}) {
store.dispatch({type: 'GET_REDUX_PROP', data: 'Hello redux!'})
store.dispatch({type: 'GET_REDUX_SAGA_PROP'})
return {static: 'Hello static!'}
}

render() {
return (
<ul>
<li>prop from getInitialProps: {this.props.static}</li>
<li>prop from redux: {this.props.redux}</li>
<li>prop from redux-saga: {this.props.reduxSaga}</li>
</ul>
)
}
}

export default withRedux(configureStore, state => state)(
withReduxSaga(ExamplePage)
)
```

## Contributors

Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
| [<img src="https://avatars0.githubusercontent.com/u/3741255?v=3" width="100px;"/><br /><sub>Brent Mealhouse</sub>](https://twitter.com/bmealhouse)<br />[💻](https://github.com/bmealhouse/next-redux-saga/commits?author=bmealhouse "Code") [📖](https://github.com/bmealhouse/next-redux-saga/commits?author=bmealhouse "Documentation") [⚠️](https://github.com/bmealhouse/next-redux-saga/commits?author=bmealhouse "Tests") |
| :---: |
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!

## Contributing

1. [Fork](https://help.github.com/articles/fork-a-repo/) this repository to your own GitHub account and then [clone](https://help.github.com/articles/cloning-a-repository/) it to your local device
2. Install the dependecies: `yarn`
3. Link the package to the global module directory: `yarn link`
4. Run `yarn test -- --watch` and start making your changes
5. You can use `yarn link next-redux-saga` to test your changes in an actual project

## LICENSE

MIT
114 changes: 114 additions & 0 deletions dist/index.js
@@ -0,0 +1,114 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reduxSaga = require('redux-saga');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function withReduxSaga(BaseComponent) {
var WrappedComponent = function (_Component) {
(0, _inherits3.default)(WrappedComponent, _Component);

function WrappedComponent() {
(0, _classCallCheck3.default)(this, WrappedComponent);
return (0, _possibleConstructorReturn3.default)(this, (WrappedComponent.__proto__ || (0, _getPrototypeOf2.default)(WrappedComponent)).apply(this, arguments));
}

(0, _createClass3.default)(WrappedComponent, [{
key: 'render',
value: function render() {
return _react2.default.createElement(BaseComponent, this.props);
}
}], [{
key: 'getInitialProps',
value: function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx) {
var isServer, store, props;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
isServer = ctx.isServer, store = ctx.store;
props = void 0;

if (!BaseComponent.getInitialProps) {
_context.next = 6;
break;
}

_context.next = 5;
return BaseComponent.getInitialProps(ctx);

case 5:
props = _context.sent;

case 6:
if (!isServer) {
_context.next = 10;
break;
}

store.dispatch(_reduxSaga.END);
_context.next = 10;
return store.sagaTask.done;

case 10:
return _context.abrupt('return', props);

case 11:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));

function getInitialProps(_x) {
return _ref.apply(this, arguments);
}

return getInitialProps;
}()
}]);
return WrappedComponent;
}(_react.Component);

return WrappedComponent;
}

exports.default = withReduxSaga;
25 changes: 25 additions & 0 deletions lib/__snapshots__/client.test.js.snap
@@ -0,0 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`wrapped component awaits async getInitialProps 1`] = `
<div>
Async
</div>
`;

exports[`wrapped component awaits sync getInitialProps 1`] = `
<div>
Sync
</div>
`;

exports[`wrapped component passes along props 1`] = `
<div>
Hello !
</div>
`;

exports[`wrapped component skips getInitialProps 1`] = `
<div>
Skip
</div>
`;

0 comments on commit fd5ac02

Please sign in to comment.