@lessondesk/react-table-context
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -1,1 +0,1 @@ | ||
function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var t=require("react"),a=e(t),i=e(require("hash-sum"));module.exports=function(e){void 0===e&&(e=function(){return Promise.resolve([])});var r=t.createContext(),n=function(t){function n(){var a=this;t.call(this),this.checkIfSame=function(e,t){return e.every(function(e){var a=e.id;return t.some(function(e){return e.id===a})})&&t.every(function(t){var a=t.id;return e.some(function(e){return e.id===a})})},this.handleUpdate=function(){var t=a.state,i=a.props.getCacheKey({meta:t.meta,page:t.page,pageSize:t.pageSize,search:t.search,filters:t.filters,sorting:t.sorting,key:a.key});if(a.cache.has(i)){var r=a.cache.get(i);a.setState(Object.assign({},r))}else a.setState({isLoading:!0},function(){e(Object.assign({},a.state)).then(function(e){var t=[],r={};if(e.data&&e.meta?(t=e.data,r=e.meta):r={count:(t=e).length},!Array.isArray(t))throw new Error("Invalid data provided. Expected array, but got "+typeof t);a.setState(function(e){var a=e.page*e.pageSize;return Object.assign({},e,{data:t,meta:r,firstPage:0===e.page,pageData:t.slice(a,a+e.pageSize),isEmpty:0===t.length,isLoading:!1})},function(){var e=a.state;a.cache.set(i,{data:a.state.data,firstPage:e.firstPage,isEmpty:e.isEmpty,isLoading:e.isLoading,pageData:e.pageData,unappliedFilters:e.unappliedFilters,meta:e.meta,page:e.page,pageSize:e.pageSize,search:e.search,filters:e.filters})})}).catch(function(e){console.error(e),a.props.onError(e),a.setState(e)})})},this.setSearch=function(e){a.setState({search:e,page:0},function(){return a.handleUpdate()})},this.setPage=function(e){a.setState({page:e},function(){return a.handleUpdate()})},this.setPageSize=function(e){a.setState({pageSize:e},function(){return a.handleUpdate()})},this.setSelected=function(e){a.setState({selected:e})},this.toggleSelectAll=function(e){var t=a.state,i=t.data,r=t.selected,n=i.every(function(e){var t=e.id;return r.some(function(e){return e.id===t})})?r.filter(function(e){var t=e.id;return!i.some(function(e){return e.id===t})}):r.concat(i),s=n.filter(function(e,t){var a=e.id;return n.findIndex(function(e){return e.id===a})===t}),o=e?s.map(function(t){return Object.assign({},t,{tableName:t.tableName||e})}):s;a.setState({selected:o})},this.setFilters=function(e,t){void 0===t&&(t=!1),a.setState({filters:e,unappliedFilters:e,page:0,data:t?[]:a.state.data},function(){return a.handleUpdate()})},this.setUnappliedFilters=function(e){a.setState({unappliedFilters:e})},this.applyFilters=function(){a.setFilters(a.state.unappliedFilters)},this.setSorting=function(e){a.setState({sorting:e},function(){return a.handleUpdate()})},this.refresh=function(){a.handleUpdate()},this.state={page:0,pageSize:10,firstPage:!0,isLoading:!1,isEmpty:!0,data:[],meta:{},error:null,filters:{},unappliedFilters:{},sorting:{},pageData:[],selected:[],search:""},this.key=i(Date.now()),this.cache=new Map}return t&&(n.__proto__=t),(n.prototype=Object.create(t&&t.prototype)).constructor=n,n.prototype.componentDidMount=function(){var e=this,t=this.props;this.setState({pageSize:t.pageSize,filters:t.filters,selected:t.selected},function(){return e.handleUpdate()})},n.prototype.componentDidUpdate=function(e){var t=this.props.selected;this.checkIfSame(t,e.selected)||this.setState({selected:t})},n.prototype.render=function(){var e=Object.assign({},this.state,{setSearch:this.setSearch,setPage:this.setPage,setSorting:this.setSorting,setPageSize:this.setPageSize,refresh:this.refresh,setSelected:this.setSelected,toggleSelectAll:this.toggleSelectAll,setFilters:this.setFilters,setUnappliedFilters:this.setUnappliedFilters,applyFilters:this.applyFilters});return a.createElement(r.Provider,{value:e},this.props.children)},n}(t.Component);return n.defaultProps={onError:console.error,pageSize:10,filters:{},selected:[],getCacheKey:function(e){return i(e)}},[n,r.Consumer]}; | ||
function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var t=require("react"),a=e(t),i=e(require("hash-sum"));module.exports=function(e){void 0===e&&(e=function(){return Promise.resolve([])});var r=t.createContext(),n=function(t){function n(){var a=this;t.call(this),this.checkIfSame=function(e,t){return e.every(function(e){var a=e.id;return t.some(function(e){return e.id===a})})&&t.every(function(t){var a=t.id;return e.some(function(e){return e.id===a})})},this.handleUpdate=function(){var t=a.state,i=a.props.getCacheKey({meta:t.meta,page:t.page,pageSize:t.pageSize,search:t.search,filters:t.filters,sorting:t.sorting,key:a.key});if(a.cache.has(i)){var r=a.cache.get(i);a.setState(Object.assign({},r))}else a.setState({isLoading:!0},function(){e(Object.assign({},a.state)).then(function(e){var t=[],r={};if(e.data&&e.meta?(t=e.data,r=e.meta):r={count:(t=e).length},!Array.isArray(t))throw new Error("Invalid data provided. Expected array, but got "+typeof t);a.setState(function(e){var a=e.page*e.pageSize;return Object.assign({},e,{data:t,meta:r,firstPage:0===e.page,pageData:t.slice(a,a+e.pageSize),isEmpty:0===t.length,isLoading:!1})},function(){var e=a.state;a.cache.set(i,{data:a.state.data,firstPage:e.firstPage,isEmpty:e.isEmpty,isLoading:e.isLoading,pageData:e.pageData,unappliedFilters:e.unappliedFilters,meta:e.meta,page:e.page,pageSize:e.pageSize,search:e.search,filters:e.filters})})}).catch(function(e){console.error("Init table context error: ",e),a.props.onError(e),a.setState({error:e,isLoading:!1})})})},this.setSearch=function(e){a.setState({search:e,page:0},function(){return a.handleUpdate()})},this.setPage=function(e){a.setState({page:e},function(){return a.handleUpdate()})},this.setPageSize=function(e){a.setState({pageSize:e},function(){return a.handleUpdate()})},this.setSelected=function(e){a.setState({selected:e})},this.toggleSelectAll=function(e){var t=a.state,i=t.data,r=t.selected,n=i.every(function(e){var t=e.id;return r.some(function(e){return e.id===t})})?r.filter(function(e){var t=e.id;return!i.some(function(e){return e.id===t})}):r.concat(i),s=n.filter(function(e,t){var a=e.id;return n.findIndex(function(e){return e.id===a})===t}),o=e?s.map(function(t){return Object.assign({},t,{tableName:t.tableName||e})}):s;a.setState({selected:o})},this.setFilters=function(e,t){void 0===t&&(t=!1),a.setState({filters:e,unappliedFilters:e,page:0,data:t?[]:a.state.data},function(){return a.handleUpdate()})},this.setUnappliedFilters=function(e){a.setState({unappliedFilters:e})},this.applyFilters=function(){a.setFilters(a.state.unappliedFilters)},this.setSorting=function(e){a.setState({sorting:e},function(){return a.handleUpdate()})},this.refresh=function(){a.handleUpdate()},this.state={page:0,pageSize:10,firstPage:!0,isLoading:!1,isEmpty:!0,data:[],meta:{},error:null,filters:{},unappliedFilters:{},sorting:{},pageData:[],selected:[],search:""},this.key=i(Date.now()),this.cache=new Map}return t&&(n.__proto__=t),(n.prototype=Object.create(t&&t.prototype)).constructor=n,n.prototype.componentDidMount=function(){var e=this,t=this.props;this.setState({pageSize:t.pageSize,filters:t.filters,selected:t.selected},function(){return e.handleUpdate()})},n.prototype.componentDidUpdate=function(e){var t=this.props.selected;this.checkIfSame(t,e.selected)||this.setState({selected:t})},n.prototype.render=function(){var e=Object.assign({},this.state,{setSearch:this.setSearch,setPage:this.setPage,setSorting:this.setSorting,setPageSize:this.setPageSize,refresh:this.refresh,setSelected:this.setSelected,toggleSelectAll:this.toggleSelectAll,setFilters:this.setFilters,setUnappliedFilters:this.setUnappliedFilters,applyFilters:this.applyFilters});return a.createElement(r.Provider,{value:e},this.props.children)},n}(t.Component);return n.defaultProps={onError:console.error,pageSize:10,filters:{},selected:[],getCacheKey:function(e){return i(e)}},[n,r.Consumer]}; |
{ | ||
"name": "@lessondesk/react-table-context", | ||
"description": "Flexible Table Provider & Consumer", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"main": "dist/react-table-context.js", | ||
@@ -6,0 +6,0 @@ "source": "src/index.js", |
import React, { createContext, Component } from 'react' | ||
import hash from 'hash-sum' | ||
export default function initTableContext (getData = () => Promise.resolve([])) { | ||
export default function initTableContext(getData = () => Promise.resolve([])) { | ||
const TableContext = createContext() | ||
class TableProvider extends Component { | ||
constructor () { | ||
constructor() { | ||
super() | ||
@@ -39,3 +39,3 @@ this.state = { | ||
componentDidMount () { | ||
componentDidMount() { | ||
const { pageSize, filters, selected } = this.props | ||
@@ -45,3 +45,3 @@ this.setState({ pageSize, filters, selected }, () => this.handleUpdate()) | ||
componentDidUpdate (prevProps) { | ||
componentDidUpdate(prevProps) { | ||
const { selected } = this.props | ||
@@ -53,4 +53,4 @@ const isSame = this.checkIfSame(selected, prevProps.selected) | ||
checkIfSame = (selected, prevSelected) => ( | ||
selected.every(({id}) => prevSelected.some(s => s.id === id)) | ||
&& prevSelected.every(({id}) => selected.some(s => s.id === id)) | ||
selected.every(({ id }) => prevSelected.some(s => s.id === id)) | ||
&& prevSelected.every(({ id }) => selected.some(s => s.id === id)) | ||
) | ||
@@ -140,5 +140,5 @@ | ||
.catch(error => { | ||
console.error(error) | ||
console.error('Init table context error: ', error) | ||
this.props.onError(error) | ||
this.setState(error) | ||
this.setState({ error, isLoading: false }) | ||
}) | ||
@@ -220,3 +220,3 @@ }) | ||
render () { | ||
render() { | ||
const value = { | ||
@@ -223,0 +223,0 @@ ...this.state, |
18436
303