UNPKG

@vlsergey/react-bootstrap-pagetable

Version:

Complex solution to work with pageable data, including sorting, filtering, actions, changing displayed columns, etc.

90 lines (89 loc) 4.97 kB
import { __assign, __awaiter, __extends, __generator, __rest } from "tslib"; import React, { PureComponent } from 'react'; import { emptyPage } from './Page'; export var FetchReason; (function (FetchReason) { FetchReason[FetchReason["FIRST_TIME_FETCH"] = 0] = "FIRST_TIME_FETCH"; FetchReason[FetchReason["FETCH_ARGS_CHANGE"] = 1] = "FETCH_ARGS_CHANGE"; FetchReason[FetchReason["REFRESH_REQUIRED"] = 2] = "REFRESH_REQUIRED"; })(FetchReason || (FetchReason = {})); var withPageInState = function (Child) { return /** @class */ (function (_super) { __extends(WithPageInState, _super); function WithPageInState() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.prevFetchArgs = undefined; _this.prevAbortController = undefined; _this.state = { error: null, hasError: false, loading: true, page: emptyPage(), }; _this.handleRefreshRequired = function () { _this.scheduleRefreshNow(FetchReason.REFRESH_REQUIRED); }; _this.refresh = function (fetchReason) { return __awaiter(_this, void 0, void 0, function () { var _a, fetch, fetchArgs, newAbortController, fetchOptions, page, error_1; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = this.props, fetch = _a.fetch, fetchArgs = _a.fetchArgs; if (fetchArgs === this.prevFetchArgs && fetchReason !== FetchReason.REFRESH_REQUIRED) { return [2 /*return*/]; } _b.label = 1; case 1: _b.trys.push([1, 3, , 4]); if (fetch === undefined) throw new Error('fetch() implementation was not provided to PageTable. ' + 'Check PageTable properties or switch to Controllable variant of PageTable'); this.setState({ loading: true }); if (this.prevAbortController != undefined) { this.prevAbortController.abort(); this.prevAbortController = undefined; } // do not use fetch result from outdated query // remember query here... this.prevFetchArgs = fetchArgs; newAbortController = this.prevAbortController = window.AbortController ? new AbortController() : undefined; fetchOptions = newAbortController != undefined ? { signal: newAbortController.signal } : {}; return [4 /*yield*/, fetch(fetchArgs, fetchOptions, fetchReason)]; case 2: page = _b.sent(); // ...to compare query here if (this.prevFetchArgs === fetchArgs) { this.setState({ error: undefined, loading: false, hasError: false, page: page }); this.prevAbortController = undefined; } return [3 /*break*/, 4]; case 3: error_1 = _b.sent(); this.setState({ error: error_1, loading: false, hasError: true }); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }; _this.scheduleRefreshNow = function (reason) { return setTimeout(function () { return void _this.refresh(reason); }, 0); }; return _this; } WithPageInState.prototype.componentDidMount = function () { void this.refresh(FetchReason.FIRST_TIME_FETCH); }; WithPageInState.prototype.componentDidUpdate = function (prevProps) { if (this.props.fetchArgs !== prevProps.fetchArgs) { this.scheduleRefreshNow(FetchReason.FETCH_ARGS_CHANGE); } }; WithPageInState.prototype.render = function () { /* eslint @typescript-eslint/no-unused-vars: ["error", { "varsIgnorePattern": "fetch|onFetchArgsChange" }] */ var _a = this.props, fetch = _a.fetch, etcProps = __rest(_a, ["fetch"]); return React.createElement(Child, __assign({ onRefreshRequired: this.handleRefreshRequired }, etcProps, this.state)); }; return WithPageInState; }(PureComponent)); }; export default withPageInState;