├── .babelrc ├── .gitignore ├── CODE_OF_CONDUCT.md ├── LICENSE.md ├── README.md ├── npm-debug.log ├── package.json ├── scripts └── build.sh ├── src └── index.js └── webpack.config.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015", "stage-0"] 3 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | lib -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. 4 | 5 | We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion. 6 | 7 | Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct. 8 | 9 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team. 10 | 11 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. 12 | 13 | This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/) 14 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Vahe Hovhannisyan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Redux Pagination 2 | ========================= 3 | 4 | Pagination utilities (action creators, higher order reducer) for [Redux](https://github.com/rackt/redux). 5 | 6 | ## Note! 7 | 8 | The current implementation makes a lot of assumptions about your api, heavily 9 | depends on [redux-thunk](https://github.com/gaearon/redux-thunk) and this [middleware](https://github.com/erikras/react-redux-universal-hot-example/blob/master/src/redux/middleware/clientMiddleware.js)! 10 | You should really look at the [code](https://github.com/vhpoet/redux-pagination/blob/master/src/index.js) before trying to use this. 11 | 12 | ## Installation 13 | 14 | ``` 15 | npm install --save redux-pagination 16 | ``` 17 | 18 | ## API 19 | 20 | ```js 21 | import paginate from 'redux-pagination' 22 | paginate(reducer) 23 | paginate(reducer, config) 24 | ``` 25 | 26 | ## Adding pagination to your reducers 27 | 28 | `redux-pagination` is a reducer enhancer (higher-order reducer), it provides the 29 | paginate function, which takes an existing reducer and a configuration object 30 | and enhances your existing reducer with pagination functionality. 31 | 32 | To install, firstly import `redux-pagination` 33 | 34 | ```js 35 | // Redux utility functions 36 | import { combineReducers } from 'redux' 37 | // redux-pagination higher-order reducer 38 | import paginate from 'redux-pagination'; 39 | ``` 40 | 41 | Then, add `paginate` to your reducer(s) like this: 42 | 43 | ```js 44 | combineReducers({ 45 | history: paginate(history) 46 | }) 47 | ``` 48 | 49 | ## Pagination Actions 50 | 51 | Import the action creator init function: 52 | 53 | ```js 54 | import { initActionCreators } from 'redux-pagination'; 55 | ``` 56 | 57 | Init the Action Creators 58 | 59 | ```js 60 | const paginationActionCreators = initActionCreators({ 61 | limit: 30, // number of items per page 62 | path: '/payments' // path to the make the call to 63 | }) 64 | ``` 65 | 66 | Then, you can use `store.dispatch()` and the pagination action creators to 67 | perform pagination operations on your state: 68 | 69 | ```js 70 | store.dispatch(paginationActionCreators.getPage(pageNumber)) 71 | ``` 72 | 73 | ## TODO 74 | - add tests 75 | - add caching 76 | - don't make assumptions about the api 77 | - don't depend on a specific client 78 | - don't depend on specific url params 79 | - don't depend on that middleware I mentioned above 80 | 81 | ## License 82 | 83 | MIT, see `LICENSE.md`. -------------------------------------------------------------------------------- /npm-debug.log: -------------------------------------------------------------------------------- 1 | 0 info it worked if it ends with ok 2 | 1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'publish' ] 3 | 2 info using npm@3.10.8 4 | 3 info using node@v6.9.1 5 | 4 verbose publish [ '.' ] 6 | 5 silly cache add args [ '.', null ] 7 | 6 verbose cache add spec . 8 | 7 silly cache add parsed spec Result { 9 | 7 silly cache add raw: '.', 10 | 7 silly cache add scope: null, 11 | 7 silly cache add escapedName: null, 12 | 7 silly cache add name: null, 13 | 7 silly cache add rawSpec: '.', 14 | 7 silly cache add spec: '/Users/vh/localhost/redux-pagination', 15 | 7 silly cache add type: 'directory' } 16 | 8 verbose addLocalDirectory /Users/vh/.npm/redux-pagination/0.1.1/package.tgz not in flight; packing 17 | 9 verbose correctMkdir /Users/vh/.npm correctMkdir not in flight; initializing 18 | 10 info lifecycle redux-pagination@0.1.1~prepublish: redux-pagination@0.1.1 19 | 11 silly lifecycle redux-pagination@0.1.1~prepublish: no script for prepublish, continuing 20 | 12 verbose tar pack [ '/Users/vh/.npm/redux-pagination/0.1.1/package.tgz', 21 | 12 verbose tar pack '/Users/vh/localhost/redux-pagination' ] 22 | 13 verbose tarball /Users/vh/.npm/redux-pagination/0.1.1/package.tgz 23 | 14 verbose folder /Users/vh/localhost/redux-pagination 24 | 15 verbose addLocalTarball adding from inside cache /Users/vh/.npm/redux-pagination/0.1.1/package.tgz 25 | 16 verbose correctMkdir /Users/vh/.npm correctMkdir not in flight; initializing 26 | 17 silly cache afterAdd redux-pagination@0.1.1 27 | 18 verbose afterAdd /Users/vh/.npm/redux-pagination/0.1.1/package/package.json not in flight; writing 28 | 19 verbose correctMkdir /Users/vh/.npm correctMkdir not in flight; initializing 29 | 20 verbose afterAdd /Users/vh/.npm/redux-pagination/0.1.1/package/package.json written 30 | 21 silly publish { name: 'redux-pagination', 31 | 21 silly publish version: '0.1.1', 32 | 21 silly publish description: 'Pagination utilities (action creators, higher order reducer) for redux', 33 | 21 silly publish main: 'lib/index.js', 34 | 21 silly publish scripts: { build: './scripts/build.sh' }, 35 | 21 silly publish files: [ 'lib', 'src' ], 36 | 21 silly publish repository: 37 | 21 silly publish { type: 'git', 38 | 21 silly publish url: 'git+https://github.com/vhpoet/redux-pagination.git' }, 39 | 21 silly publish keywords: 40 | 21 silly publish [ 'redux', 41 | 21 silly publish 'pagination', 42 | 21 silly publish 'react', 43 | 21 silly publish 'higher order reducer', 44 | 21 silly publish 'action creator' ], 45 | 21 silly publish author: 46 | 21 silly publish { name: 'Vahe Hovhannisyan', 47 | 21 silly publish email: 'vhpoet@gmail.com', 48 | 21 silly publish url: 'http://github.com/vhpoet' }, 49 | 21 silly publish license: 'MIT', 50 | 21 silly publish bugs: { url: 'https://github.com/vhpoet/redux-pagination/issues' }, 51 | 21 silly publish homepage: 'https://github.com/vhpoet/redux-pagination', 52 | 21 silly publish devDependencies: 53 | 21 silly publish { babel: '^6.5.2', 54 | 21 silly publish 'babel-cli': '^6.6.5', 55 | 21 silly publish 'babel-loader': '^6.2.4', 56 | 21 silly publish 'babel-preset-es2015': '^6.18.0', 57 | 21 silly publish 'babel-preset-stage-0': '^6.16.0', 58 | 21 silly publish webpack: '^1.12.14' }, 59 | 21 silly publish gitHead: '4a8d962071129ebd237a5ef1a3291b18e9b14c44', 60 | 21 silly publish readme: 'Redux Pagination\n=========================\n\nPagination utilities (action creators, higher order reducer) for [Redux](https://github.com/rackt/redux). \n\n## Note!\n\nThe current implementation makes a lot of assumptions about your api, heavily \ndepends on [redux-thunk](https://github.com/gaearon/redux-thunk) and this [middleware](https://github.com/erikras/react-redux-universal-hot-example/blob/master/src/redux/middleware/clientMiddleware.js)!\nYou should really look at the [code](https://github.com/vhpoet/redux-pagination/blob/master/src/index.js) before trying to use this.\n\n## Installation \n\n```\nnpm install --save redux-pagination\n```\n\n## API\n\n```js\nimport paginate from \'redux-pagination\'\npaginate(reducer)\npaginate(reducer, config)\n```\n\n## Adding pagination to your reducers\n\n`redux-pagination` is a reducer enhancer (higher-order reducer), it provides the \npaginate function, which takes an existing reducer and a configuration object \nand enhances your existing reducer with pagination functionality.\n\nTo install, firstly import `redux-pagination`\n\n```js\n// Redux utility functions\nimport { combineReducers } from \'redux\'\n// redux-pagination higher-order reducer\nimport paginate from \'redux-pagination\';\n```\n\nThen, add `paginate` to your reducer(s) like this:\n\n```js\ncombineReducers({\n history: paginate(history)\n})\n```\n\n## Pagination Actions\n\nImport the action creator init function:\n\n```js\nimport { initActionCreators } from \'redux-pagination\';\n```\n\nInit the Action Creators\n\n```js\nconst paginationActionCreators = initActionCreators({\n limit: 30, // number of items per page\n path: \'/payments\' // path to the make the call to\n})\n```\n\nThen, you can use `store.dispatch()` and the pagination action creators to\nperform pagination operations on your state:\n\n```js\nstore.dispatch(paginationActionCreators.getPage(pageNumber))\n```\n\n## TODO\n- add tests\n- add caching\n- don\'t make assumptions about the api\n- don\'t depend on a specific client\n- don\'t depend on specific url params\n- don\'t depend on that middleware I mentioned above\n\n## License\n\nMIT, see `LICENSE.md`.', 61 | 21 silly publish readmeFilename: 'README.md', 62 | 21 silly publish _id: 'redux-pagination@0.1.1', 63 | 21 silly publish _shasum: '93e3bd5f83c943c974eab85dbd5a5b1ec7639a4f', 64 | 21 silly publish _from: '.' } 65 | 22 verbose getPublishConfig undefined 66 | 23 silly mapToRegistry name redux-pagination 67 | 24 silly mapToRegistry using default registry 68 | 25 silly mapToRegistry registry https://registry.npmjs.org/ 69 | 26 silly mapToRegistry data Result { 70 | 26 silly mapToRegistry raw: 'redux-pagination', 71 | 26 silly mapToRegistry scope: null, 72 | 26 silly mapToRegistry escapedName: 'redux-pagination', 73 | 26 silly mapToRegistry name: 'redux-pagination', 74 | 26 silly mapToRegistry rawSpec: '', 75 | 26 silly mapToRegistry spec: 'latest', 76 | 26 silly mapToRegistry type: 'tag' } 77 | 27 silly mapToRegistry uri https://registry.npmjs.org/redux-pagination 78 | 28 verbose publish registryBase https://registry.npmjs.org/ 79 | 29 silly publish uploading /Users/vh/.npm/redux-pagination/0.1.1/package.tgz 80 | 30 verbose request uri https://registry.npmjs.org/redux-pagination 81 | 31 verbose request sending authorization for write operation 82 | 32 info attempt registry request try #1 at 11:51:47 AM 83 | 33 verbose request id 63e69ab561ee4a88 84 | 34 http request PUT https://registry.npmjs.org/redux-pagination 85 | 35 http 403 https://registry.npmjs.org/redux-pagination 86 | 36 verbose headers { 'content-type': 'application/json', 87 | 36 verbose headers 'cache-control': 'max-age=300', 88 | 36 verbose headers 'content-length': '95', 89 | 36 verbose headers 'accept-ranges': 'bytes', 90 | 36 verbose headers date: 'Wed, 01 Feb 2017 19:51:47 GMT', 91 | 36 verbose headers via: '1.1 varnish', 92 | 36 verbose headers connection: 'keep-alive', 93 | 36 verbose headers 'x-served-by': 'cache-lax8621-LAX', 94 | 36 verbose headers 'x-cache': 'MISS', 95 | 36 verbose headers 'x-cache-hits': '0', 96 | 36 verbose headers 'x-timer': 'S1485978707.464020,VS0,VE196', 97 | 36 verbose headers vary: 'Accept-Encoding' } 98 | 37 verbose request invalidating /Users/vh/.npm/registry.npmjs.org/redux-pagination on PUT 99 | 38 error publish Failed PUT 403 100 | 39 verbose stack Error: "You cannot publish over the previously published version 0.1.1." : redux-pagination 101 | 39 verbose stack at makeError (/usr/local/lib/node_modules/npm/node_modules/npm-registry-client/lib/request.js:302:12) 102 | 39 verbose stack at CachingRegistryClient. (/usr/local/lib/node_modules/npm/node_modules/npm-registry-client/lib/request.js:290:14) 103 | 39 verbose stack at Request._callback (/usr/local/lib/node_modules/npm/node_modules/npm-registry-client/lib/request.js:210:14) 104 | 39 verbose stack at Request.self.callback (/usr/local/lib/node_modules/npm/node_modules/request/request.js:187:22) 105 | 39 verbose stack at emitTwo (events.js:106:13) 106 | 39 verbose stack at Request.emit (events.js:191:7) 107 | 39 verbose stack at Request. (/usr/local/lib/node_modules/npm/node_modules/request/request.js:1044:10) 108 | 39 verbose stack at emitOne (events.js:96:13) 109 | 39 verbose stack at Request.emit (events.js:188:7) 110 | 39 verbose stack at IncomingMessage. (/usr/local/lib/node_modules/npm/node_modules/request/request.js:965:12) 111 | 40 verbose statusCode 403 112 | 41 verbose pkgid redux-pagination 113 | 42 verbose cwd /Users/vh/localhost/redux-pagination 114 | 43 error Darwin 16.3.0 115 | 44 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "publish" 116 | 45 error node v6.9.1 117 | 46 error npm v3.10.8 118 | 47 error code E403 119 | 48 error "You cannot publish over the previously published version 0.1.1." : redux-pagination 120 | 49 error If you need help, you may report this error at: 121 | 49 error 122 | 50 verbose exit [ 1, true ] 123 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "redux-pagination", 3 | "version": "0.2.0", 4 | "description": "Pagination utilities (action creators, higher order reducer) for redux", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "build": "./scripts/build.sh" 8 | }, 9 | "files": [ 10 | "lib", 11 | "src" 12 | ], 13 | "repository": { 14 | "type": "git", 15 | "url": "https://github.com/vhpoet/redux-pagination.git" 16 | }, 17 | "keywords": [ 18 | "redux", 19 | "pagination", 20 | "react", 21 | "higher order reducer", 22 | "action creator" 23 | ], 24 | "author": "Vahe Hovhannisyan (http://github.com/vhpoet)", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/vhpoet/redux-pagination/issues" 28 | }, 29 | "homepage": "https://github.com/vhpoet/redux-pagination", 30 | "devDependencies": { 31 | "babel": "^6.5.2", 32 | "babel-cli": "^6.6.5", 33 | "babel-loader": "^6.2.4", 34 | "babel-preset-es2015": "^6.18.0", 35 | "babel-preset-stage-0": "^6.16.0", 36 | "webpack": "^1.12.14" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /scripts/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # build minified standalone version in dist 4 | rm -rf dist 5 | ./node_modules/.bin/webpack --output-filename=dist/redux-pagination.js 6 | ./node_modules/.bin/webpack --output-filename=dist/redux-pagination.min.js --optimize-minimize 7 | 8 | # build ES5 modules to lib 9 | rm -rf lib 10 | ./node_modules/.bin/babel src --out-dir lib -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | "use strict" 2 | 3 | export const ActionTypes = { 4 | CHANGE_PAGE_NUMBER: '@@redux-pagination/CHANGE_PAGE_NUMBER', 5 | GET_PAGE: '@@redux-pagination/GET_PAGE', 6 | GET_PAGE_SUCCESS: '@@redux-pagination/GET_PAGE_SUCCESS', 7 | GET_PAGE_FAIL: '@@redux-pagination/GET_PAGE_FAIL' 8 | } 9 | 10 | export const initActionCreators = (config) => { 11 | const path = config.path 12 | const limit = config.limit || 50 13 | 14 | return { 15 | getPage(pageNumber) { 16 | return (dispatch) => { 17 | // Change the page number 18 | dispatch({ 19 | type: ActionTypes.CHANGE_PAGE_NUMBER, 20 | result: pageNumber 21 | }) 22 | 23 | // Get the page 24 | return dispatch({ 25 | types: [ActionTypes.GET_PAGE, ActionTypes.GET_PAGE_SUCCESS, ActionTypes.GET_PAGE_FAIL], 26 | promise: (client) => client.get(path, { 27 | params: { 28 | page: pageNumber, 29 | limit: limit 30 | } 31 | }) 32 | }) 33 | } 34 | } 35 | } 36 | } 37 | 38 | // higher order reducer 39 | export default function paginate (reducer) { 40 | const initialState = { 41 | ...reducer(undefined, {}), 42 | loadingPage: false, 43 | initialLoad: false, 44 | currentPage: 1, 45 | totalPages: 1, 46 | fail: {}, 47 | list: [] 48 | } 49 | 50 | return (state = initialState, action = {}) => { 51 | switch (action.type) { 52 | case ActionTypes.CHANGE_PAGE_NUMBER: 53 | return { 54 | ...state, 55 | fail: {}, 56 | currentPage: action.result 57 | } 58 | case ActionTypes.GET_PAGE: 59 | return { 60 | ...state, 61 | fail: {}, 62 | loadingPage: true 63 | } 64 | case ActionTypes.GET_PAGE_SUCCESS: 65 | return { 66 | ...state, 67 | fail: {}, 68 | loadingPage: false, 69 | initialLoad: true, 70 | list: action.result.list, 71 | totalPages: action.result.totalPages 72 | } 73 | case ActionTypes.GET_PAGE_FAIL: 74 | return { 75 | ...state, 76 | initialLoad: true, 77 | fail: action.result 78 | } 79 | default: 80 | return reducer(state, action) 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | var webpack = require('webpack'); 2 | 3 | module.exports = { 4 | entry: './src/index', 5 | module: { 6 | loaders: [ 7 | { test: /\.js$/, loader: 'babel', exclude: /node_modules/ } 8 | ] 9 | }, 10 | output: { 11 | filename: 'dist/redux-pagination.min.js', 12 | libraryTarget: 'umd', 13 | library: 'redux-pagination' 14 | } 15 | }; --------------------------------------------------------------------------------