UNPKG

@gpa-gemstone/react-interactive

Version:
508 lines (507 loc) 30.6 kB
"use strict"; // ****************************************************************************************************** // GenericSlice.tsx - Gbtc // // Copyright © 2020, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this // file except in compliance with the License. You may obtain a copy of the License at: // // http://opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 11/04/2020 - Billy Ernest // Generated original version of source code. // ****************************************************************************************************** var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var toolkit_1 = require("@reduxjs/toolkit"); var _ = __importStar(require("lodash")); var GenericController_1 = __importDefault(require("./GenericController")); /** * A generic class providing functionalities related to a slice of data. */ var GenericSlice = /** @class */ (function () { /** * Creates a new GenericSlice of type T, which can be used to perform basic CRUD operations against * a specified web api. * @typeParam T - Model of Generic Slice * @param {string} name - string defining the name of the slice in the store * @param {string} apiPath - string containing relative path to web api * @param {keyof T} defaultSort - string showing default sort field * @param {boolean} ascending - (optional) default sort direction - defaults to true * @returns a new GenericSlice<T> */ function GenericSlice(name, apiPath, defaultSort, ascending, options) { if (ascending === void 0) { ascending = true; } if (options === void 0) { options = null; } var _this = this; this.Name = ""; this.APIPath = ""; this.Data = function (state) { return state[_this.Name].Data; }; this.Error = function (state) { return state[_this.Name].Error; }; this.Datum = function (state, id) { return state[_this.Name].Data.find(function (d) { return d.ID === id; }); }; this.Status = function (state) { return state[_this.Name].Status; }; this.SortField = function (state) { return state[_this.Name].SortField; }; this.Ascending = function (state) { return state[_this.Name].Ascending; }; this.ParentID = function (state) { return state[_this.Name].ParentID; }; this.SearchResults = function (state) { return state[_this.Name].SearchResults; }; this.SearchStatus = function (state) { return state[_this.Name].SearchStatus; }; this.SearchFilters = function (state) { return state[_this.Name].Filter; }; this.PagedResults = function (state) { return state[_this.Name].PagedData; }; this.PagedStatus = function (state) { return state[_this.Name].PagedStatus; }; this.PagedFilters = function (state) { return state[_this.Name].PagedFilter; }; this.PagedSortField = function (state) { return state[_this.Name].PagedSortField; }; this.PagedAscending = function (state) { return state[_this.Name].PagedAscending; }; this.CurrentPage = function (state) { return state[_this.Name].CurrentPage; }; this.TotalPages = function (state) { return state[_this.Name].TotalPages; }; this.TotalRecords = function (state) { return state[_this.Name].TotalRecords; }; this.Name = name; this.APIPath = apiPath; this.fetchHandle = null; this.searchHandle = null; this.pageHandle = null; this.actionDependency = null; this.controller = new GenericController_1.default(apiPath, defaultSort, ascending); this.actionPendingDependency = null; this.actionFullfilledDependency = null; this.actionErrorDependency = null; if (options !== null && options.ActionDependencies !== undefined) this.actionDependency = options.ActionDependencies; if (options !== null && options.ActionPendingDependencies !== undefined) this.actionPendingDependency = options.ActionPendingDependencies; if (options !== null && options.ActionFullfilledDependencies !== undefined) this.actionFullfilledDependency = options.ActionFullfilledDependencies; if (options !== null && options.ActionErrorDependencies !== undefined) this.actionErrorDependency = options.ActionErrorDependencies; var additionalThunks = {}; var additionalBuilder = function () { _.noop(); }; if (options !== null && options.AddionalThunks !== undefined) { options.AddionalThunks.forEach(function (thunk) { additionalThunks[thunk.Name] = (0, toolkit_1.createAsyncThunk)("".concat(name, "/").concat(thunk.Name), function (arg_1, _a) { return __awaiter(_this, [arg_1, _a], void 0, function (arg, _b) { var state, handle; var getState = _b.getState; return __generator(this, function (_c) { switch (_c.label) { case 0: state = getState()[name]; if (this.actionDependency !== null) this.actionDependency(state, "".concat(name, "/").concat(thunk.Name), arg); handle = thunk.Fetch(state, arg); if (!(handle != null)) return [3 /*break*/, 2]; return [4 /*yield*/, handle]; case 1: return [2 /*return*/, _c.sent()]; case 2: return [2 /*return*/]; } }); }); }); }); additionalBuilder = function (builder) { var _a; (_a = options.AddionalThunks) === null || _a === void 0 ? void 0 : _a.forEach(function (thunk) { builder.addCase(fetch.fulfilled, function (state, action) { if (thunk.OnSuccess !== undefined) thunk.OnSuccess(state, action.meta.requestId, action.payload, action.meta.arg); if (_this.actionFullfilledDependency !== null) _this.actionFullfilledDependency(state, "".concat(name, "/").concat(thunk.Name), action.meta.arg, action.meta.requestId); }); builder.addCase(fetch.pending, function (state, action) { if (thunk.OnPending !== undefined) thunk.OnPending(state, action.meta.requestId, action.meta.arg); if (_this.actionPendingDependency !== null) _this.actionPendingDependency(state, "".concat(name, "/").concat(thunk.Name), action.meta.arg, action.meta.requestId); }); builder.addCase(fetch.rejected, function (state, action) { if (thunk.OnFailure !== undefined) thunk.OnFailure(state, action.meta.requestId, action.payload, action.meta.arg); if (_this.actionErrorDependency !== null) _this.actionErrorDependency(state, "".concat(name, "/").concat(thunk.Name), action.meta.arg, action.meta.requestId); }); }); }; } var fetch = (0, toolkit_1.createAsyncThunk)("".concat(name, "/Fetch").concat(name), function (parentID_1, _a) { return __awaiter(_this, [parentID_1, _a], void 0, function (parentID, _b) { var state, handle; var signal = _b.signal, getState = _b.getState; return __generator(this, function (_c) { switch (_c.label) { case 0: state = getState()[name]; if (this.actionDependency !== null) this.actionDependency(state, "".concat(name, "/Fetch").concat(name), parentID); if (this.fetchHandle != null && this.fetchHandle.abort != null) this.fetchHandle.abort('Prev'); handle = this.controller.Fetch(parentID, state.SortField, state.Ascending); this.fetchHandle = handle; signal.addEventListener('abort', function () { if (handle.abort !== undefined) handle.abort(); }); return [4 /*yield*/, handle]; case 1: return [2 /*return*/, _c.sent()]; } }); }); }); var dBAction = (0, toolkit_1.createAsyncThunk)("".concat(name, "/DBAction").concat(name), function (args_1, _a) { return __awaiter(_this, [args_1, _a], void 0, function (args, _b) { var handle, state; var signal = _b.signal, getState = _b.getState; return __generator(this, function (_c) { switch (_c.label) { case 0: handle = this.controller.DBAction(args.verb, args.record); state = getState()[name]; if (this.actionDependency !== null) this.actionDependency(state, "".concat(name, "/DBAction").concat(name), args); signal.addEventListener('abort', function () { if (handle.abort !== undefined) handle.abort(); }); return [4 /*yield*/, handle]; case 1: return [2 /*return*/, _c.sent()]; } }); }); }); var dBSearch = (0, toolkit_1.createAsyncThunk)("".concat(name, "/Search").concat(name), function (args_1, _a) { return __awaiter(_this, [args_1, _a], void 0, function (args, _b) { var state, sortfield, asc, handle; var _c; var getState = _b.getState, signal = _b.signal; return __generator(this, function (_d) { switch (_d.label) { case 0: state = getState()[name]; if (this.actionDependency !== null) this.actionDependency(state, "".concat(name, "/Search").concat(name), args); sortfield = args.sortField; asc = args.ascending; sortfield = sortfield === undefined ? state.SortField : sortfield; asc = asc === undefined ? state.Ascending : asc; if (this.searchHandle != null && this.searchHandle.abort != null) this.searchHandle.abort('Prev'); handle = this.controller.DBSearch(args.filter, sortfield, asc, (_c = state.ParentID) !== null && _c !== void 0 ? _c : undefined); this.searchHandle = handle; signal.addEventListener('abort', function () { if (handle.abort !== undefined) handle.abort(); }); return [4 /*yield*/, handle]; case 1: return [2 /*return*/, _d.sent()]; } }); }); }); var dBSort = (0, toolkit_1.createAsyncThunk)("".concat(name, "/DBSort").concat(name), function (args_1, _a) { return __awaiter(_this, [args_1, _a], void 0, function (args, _b) { var state, sortFld, asc, handle; var signal = _b.signal, getState = _b.getState, dispatch = _b.dispatch; return __generator(this, function (_c) { switch (_c.label) { case 0: state = getState()[name]; if (this.actionDependency !== null) this.actionDependency(state, "".concat(name, "/DBSort").concat(name), args); sortFld = state.SortField; asc = state.Ascending; if (state.SortField === args.SortField) asc = !args.Ascending; else sortFld = args.SortField; dispatch(dBSearch({ filter: state.Filter, sortField: sortFld, ascending: asc })); if (this.fetchHandle != null && this.fetchHandle.abort != null) this.fetchHandle.abort('Prev'); handle = this.controller.Fetch(state.ParentID, sortFld, asc); this.fetchHandle = handle; signal.addEventListener('abort', function () { if (handle.abort !== undefined) handle.abort(); }); return [4 /*yield*/, handle]; case 1: return [2 /*return*/, _c.sent()]; } }); }); }); var dBPage = (0, toolkit_1.createAsyncThunk)("".concat(name, "/Page").concat(name), function (args_1, _a) { return __awaiter(_this, [args_1, _a], void 0, function (args, _b) { var state, sortfield, asc, page, filts, handle; var _c, _d, _e; var getState = _b.getState, signal = _b.signal; return __generator(this, function (_f) { switch (_f.label) { case 0: state = getState()[name]; if (this.actionDependency !== null) this.actionDependency(state, "".concat(name, "/Page").concat(name), args); sortfield = args.sortField; asc = args.ascending; page = (_c = args.page) !== null && _c !== void 0 ? _c : 0; filts = (_d = args.filter) !== null && _d !== void 0 ? _d : state.Filter; sortfield = sortfield === undefined ? state.PagedSortField : sortfield; asc = asc === undefined ? state.PagedAscending : asc; if (this.pageHandle != null && this.pageHandle.abort != null) this.pageHandle.abort('Prev'); handle = this.controller.PagedSearch(filts, sortfield, asc, page, (_e = state.ParentID) !== null && _e !== void 0 ? _e : undefined); this.pageHandle = handle; signal.addEventListener('abort', function () { if (handle.abort !== undefined) handle.abort(); }); return [4 /*yield*/, handle]; case 1: return [2 /*return*/, _f.sent()]; } }); }); }); var setChanged = (0, toolkit_1.createAsyncThunk)("".concat(name, "/SetChanged").concat(name), function (args_1, _a) { return __awaiter(_this, [args_1, _a], void 0, function (args, _b) { return __generator(this, function (_c) { return [2 /*return*/]; }); }); }); var slice = (0, toolkit_1.createSlice)({ name: this.Name, initialState: { Status: 'unintiated', SearchStatus: 'unintiated', Error: null, Data: [], SortField: defaultSort, Ascending: ascending, ParentID: null, SearchResults: [], Filter: [], ActiveFetchID: [], ActiveSearchID: [], PagedStatus: 'unintiated', ActivePagedID: [], CurrentPage: 0, TotalPages: 0, TotalRecords: 0, PagedData: [], PagedSortField: defaultSort, PagedAscending: ascending, PagedFilter: [] }, reducers: {}, extraReducers: function (builder) { builder.addCase(fetch.fulfilled, function (state, action) { state.ActiveFetchID = state.ActiveFetchID.filter(function (id) { return id !== action.meta.requestId; }); state.Status = 'idle'; state.Error = null; state.Data = action.payload; if (_this.actionFullfilledDependency !== null) _this.actionFullfilledDependency(state, "".concat(name, "/Fetch").concat(name), action.meta.arg, action.meta.requestId); }); builder.addCase(fetch.pending, function (state, action) { if (state.ParentID !== (action.meta.arg == null ? null : action.meta.arg)) state.SearchStatus = 'changed'; state.ParentID = (action.meta.arg == null ? null : action.meta.arg); state.Status = 'loading'; state.ActiveFetchID.push(action.meta.requestId); if (_this.actionPendingDependency !== null) _this.actionPendingDependency(state, "".concat(name, "/Fetch").concat(name), action.meta.arg, action.meta.requestId); }); builder.addCase(fetch.rejected, function (state, action) { state.ActiveFetchID = state.ActiveFetchID.filter(function (id) { return id !== action.meta.requestId; }); if (state.ActiveFetchID.length > 0) return; state.Status = 'error'; state.Error = { Message: (action.error.message == null ? '' : action.error.message), Verb: 'FETCH', Time: new Date().toString() }; if (_this.actionErrorDependency !== null) _this.actionErrorDependency(state, "".concat(name, "/Fetch").concat(name), action.meta.arg, action.meta.requestId); }); builder.addCase(dBAction.pending, function (state, action) { state.Status = 'loading'; if (_this.actionPendingDependency !== null) _this.actionPendingDependency(state, "".concat(name, "/DBAction").concat(name), action.meta.arg, action.meta.requestId); }); builder.addCase(dBAction.rejected, function (state, action) { state.Status = 'error'; state.Error = { Message: (action.error.message == null ? '' : action.error.message), Verb: action.meta.arg.verb, Time: new Date().toString() }; if (_this.actionErrorDependency !== null) _this.actionErrorDependency(state, "".concat(name, "/DBAction").concat(name), action.meta.arg, action.meta.requestId); }); builder.addCase(dBAction.fulfilled, function (state, action) { state.Status = 'changed'; state.SearchStatus = 'changed'; state.Error = null; if (_this.actionFullfilledDependency !== null) _this.actionFullfilledDependency(state, "".concat(name, "/DBAction").concat(name), action.meta.arg, action.meta.requestId); }); builder.addCase(dBSearch.pending, function (state, action) { state.SearchStatus = 'loading'; state.ActiveSearchID.push(action.meta.requestId); if (_this.actionPendingDependency !== null) _this.actionPendingDependency(state, "".concat(name, "/Search").concat(name), action.meta.arg, action.meta.requestId); }); builder.addCase(dBSearch.rejected, function (state, action) { state.ActiveSearchID = state.ActiveSearchID.filter(function (id) { return id !== action.meta.requestId; }); if (state.ActiveSearchID.length > 0) return; state.SearchStatus = 'error'; state.Error = { Message: (action.error.message == null ? '' : action.error.message), Verb: 'SEARCH', Time: new Date().toString() }; if (_this.actionErrorDependency !== null) _this.actionErrorDependency(state, "".concat(name, "/Search").concat(name), action.meta.arg, action.meta.requestId); }); builder.addCase(dBSearch.fulfilled, function (state, action) { state.ActiveSearchID = state.ActiveSearchID.filter(function (id) { return id !== action.meta.requestId; }); state.SearchStatus = 'idle'; state.SearchResults = action.payload; state.Filter = action.meta.arg.filter; if (_this.actionFullfilledDependency !== null) _this.actionFullfilledDependency(state, "".concat(name, "/Search").concat(name), action.meta.arg, action.meta.requestId); }); builder.addCase(dBPage.pending, function (state, action) { state.PagedStatus = 'loading'; state.ActivePagedID.push(action.meta.requestId); if (_this.actionPendingDependency !== null) _this.actionPendingDependency(state, "".concat(name, "/Page").concat(name), action.meta.arg, action.meta.requestId); }); builder.addCase(dBPage.rejected, function (state, action) { state.ActivePagedID = state.ActivePagedID.filter(function (id) { return id !== action.meta.requestId; }); if (state.ActivePagedID.length > 0) return; state.PagedStatus = 'error'; state.Error = { Message: (action.error.message == null ? '' : action.error.message), Verb: 'PAGE', Time: new Date().toString() }; if (_this.actionErrorDependency !== null) _this.actionErrorDependency(state, "".concat(name, "/Page").concat(name), action.meta.arg, action.meta.requestId); }); builder.addCase(dBPage.fulfilled, function (state, action) { state.ActivePagedID = state.ActivePagedID.filter(function (id) { return id !== action.meta.requestId; }); state.PagedStatus = 'idle'; state.TotalPages = action.payload.NumberOfPages; state.SearchResults = JSON.parse(action.payload.Data); if (action.meta.arg.filter != null) state.Filter = action.meta.arg.filter; if (action.meta.arg.page !== undefined) state.CurrentPage = action.meta.arg.page; state.TotalRecords = action.payload.TotalRecords; if (_this.actionFullfilledDependency !== null) _this.actionFullfilledDependency(state, "".concat(name, "/Page").concat(name), action.meta.arg, action.meta.requestId); }); builder.addCase(dBSort.pending, function (state, action) { state.Status = 'loading'; state.ActiveFetchID.push(action.meta.requestId); if (_this.actionPendingDependency !== null) _this.actionPendingDependency(state, "".concat(name, "/DBSort").concat(name), action.meta.arg, action.meta.requestId); }); builder.addCase(dBSort.rejected, function (state, action) { state.ActiveFetchID = state.ActiveFetchID.filter(function (id) { return id !== action.meta.requestId; }); if (state.ActiveFetchID.length > 0) return; state.Status = 'error'; state.Error = { Message: (action.error.message == null ? '' : action.error.message), Verb: 'FETCH', Time: new Date().toString() }; if (_this.actionErrorDependency !== null) _this.actionErrorDependency(state, "".concat(name, "/DBSort").concat(name), action.meta.arg, action.meta.requestId); }); builder.addCase(dBSort.fulfilled, function (state, action) { state.ActiveFetchID = state.ActiveFetchID.filter(function (id) { return id !== action.meta.requestId; }); state.Status = 'idle'; state.Error = null; state.Data = action.payload; if (state.SortField === action.meta.arg.SortField) state.Ascending = !state.Ascending; else state.SortField = action.meta.arg.SortField; if (_this.actionFullfilledDependency !== null) _this.actionFullfilledDependency(state, "".concat(name, "/DBSort").concat(name), action.meta.arg, action.meta.requestId); }); builder.addCase(setChanged.pending, function (state) { state.Status = 'changed'; state.SearchStatus = 'changed'; }); additionalBuilder(builder); } }); this.AdditionalThunk = additionalThunks; this.Fetch = fetch; this.DBAction = dBAction; this.Slice = slice; this.DBSearch = dBSearch; this.PagedSearch = dBPage; this.Sort = dBSort; this.Reducer = slice.reducer; this.SetChanged = setChanged; } return GenericSlice; }()); exports.default = GenericSlice;