UNPKG

@gpa-gemstone/common-pages

Version:
231 lines (230 loc) 13 kB
"use strict"; // ****************************************************************************************************** // ReadOnlyGenericSlice.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: // ---------------------------------------------------------------------------------------------------- // 08/12/2024 - Preston Crawford // Generated original version of source code. // ****************************************************************************************************** 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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); var toolkit_1 = require("@reduxjs/toolkit"); var ControllerFunctions_1 = require("../ControllerFunctions"); /** * A generic class providing functionalities related to a slice of data. */ var ReadOnlyGenericSlice = /** @class */ (function () { function ReadOnlyGenericSlice(name, defaultSortField, ascending, apiPathOrReadOnlyController) { var _this = this; this.Data = function (state) { return state[_this.Name].Data; }; this.Error = function (state) { return state[_this.Name].Error; }; this.FetchStatus = function (state) { return state[_this.Name].FetchStatus; }; 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.Name = name; this.fetchHandle = null; if (typeof apiPathOrReadOnlyController === 'string') this.controller = new ControllerFunctions_1.ReadOnlyControllerFunctions(apiPathOrReadOnlyController); else this.controller = apiPathOrReadOnlyController; this.Fetch = (0, toolkit_1.createAsyncThunk)("".concat(this.Name, "/Fetch").concat(this.Name), function () { var args_1 = []; for (var _i = 0; _i < arguments.length; _i++) { args_1[_i] = arguments[_i]; } return __awaiter(_this, __spreadArray([], args_1, true), void 0, function (arg, _a) { var stateMap, state, fetchHandle, sortField, ascending, filter, parentID, paginationHandle; var _b, _c, _d, _e, _f; if (arg === void 0) { arg = undefined; } var signal = _a.signal, getState = _a.getState; return __generator(this, function (_g) { stateMap = getState(); state = stateMap[this.Name]; if (this.fetchHandle != null && this.fetchHandle.abort != null) this.fetchHandle.abort('Prev'); sortField = (_b = arg === null || arg === void 0 ? void 0 : arg.sortField) !== null && _b !== void 0 ? _b : state.SortField; ascending = (_c = arg === null || arg === void 0 ? void 0 : arg.ascending) !== null && _c !== void 0 ? _c : state.Ascending; filter = (_d = arg === null || arg === void 0 ? void 0 : arg.filter) !== null && _d !== void 0 ? _d : state.Filter; parentID = (_f = (_e = arg === null || arg === void 0 ? void 0 : arg.parentID) !== null && _e !== void 0 ? _e : state.ParentID) !== null && _f !== void 0 ? _f : undefined; if ((arg === null || arg === void 0 ? void 0 : arg.filter) == null) fetchHandle = this.controller.GetPage(0, sortField, ascending, parentID); else fetchHandle = this.controller.SearchPage(0, sortField, ascending, filter, parentID); this.fetchHandle = fetchHandle; paginationHandle = this.controller.GetPageInfo(); signal.addEventListener('abort', function () { if (fetchHandle.abort !== undefined) fetchHandle.abort(); if (paginationHandle.abort !== undefined) paginationHandle.abort(); }); return [2 /*return*/, Promise.all([fetchHandle]).then(function (responses) { return ({ Data: responses[0] }); })]; }); }); }); this.SetStatusToChanged = (0, toolkit_1.createAsyncThunk)("".concat(this.Name, "/SetChanged").concat(this.Name), function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/]; }); }); }); this.initializeSlice(defaultSortField, ascending); } ReadOnlyGenericSlice.prototype.initializeSlice = function (defaultSortField, ascending) { var _this = this; if (ascending === void 0) { ascending = true; } var initialState = { FetchStatus: 'uninitiated', Error: null, Data: [], SortField: defaultSortField, Ascending: ascending, Filter: [], ActiveID: [], PageInfo: { PageCount: 0, TotalCount: 0, PageSize: 0 }, ParentID: null }; this.Slice = (0, toolkit_1.createSlice)({ name: this.Name, initialState: initialState, reducers: {}, extraReducers: function (builder) { builder.addCase(_this.Fetch.pending, function (state, action) { state.FetchStatus = 'loading'; state.ActiveID.push(action.meta.requestId); }); builder.addCase(_this.Fetch.rejected, function (state, action) { var _a; state.ActiveID = state.ActiveID.filter(function (id) { return id !== action.meta.requestId; }); if (state.ActiveID.length > 0) return; state.FetchStatus = 'error'; state.Error = { Message: (_a = action.error.message) !== null && _a !== void 0 ? _a : '', Action: 'FETCH', Time: new Date().toString() }; }); builder.addCase(_this.Fetch.fulfilled, function (state, action) { var _a, _b; state.ActiveID = state.ActiveID.filter(function (id) { return id !== action.meta.requestId; }); state.FetchStatus = 'idle'; state.Error = null; state.Data = action.payload.Data; if (action.meta.arg == null) return; if (action.meta.arg.filter != null) state.Filter = (_a = action.meta.arg) === null || _a === void 0 ? void 0 : _a.filter; if (action.meta.arg.parentID != null) state.ParentID = (_b = action.meta.arg) === null || _b === void 0 ? void 0 : _b.parentID; if (state.SortField === action.meta.arg.sortField) state.Ascending = !state.Ascending; else if (action.meta.arg.sortField != null) state.SortField = action.meta.arg.sortField; }); builder.addCase(_this.SetStatusToChanged.pending, function (state) { state.FetchStatus = 'changed'; }); } }); this.Reducer = this.Slice.reducer; }; ReadOnlyGenericSlice.prototype.addExtraReducers = function (builder) { builder.addCase(this.Fetch.pending, function (state, action) { state.FetchStatus = 'loading'; state.ActiveID.push(action.meta.requestId); }); builder.addCase(this.Fetch.rejected, function (state, action) { var _a; state.ActiveID = state.ActiveID.filter(function (id) { return id !== action.meta.requestId; }); if (state.ActiveID.length > 0) return; state.FetchStatus = 'error'; state.Error = { Message: (_a = action.error.message) !== null && _a !== void 0 ? _a : '', Action: 'FETCH', Time: new Date().toString() }; }); builder.addCase(this.Fetch.fulfilled, function (state, action) { var _a, _b; state.ActiveID = state.ActiveID.filter(function (id) { return id !== action.meta.requestId; }); state.FetchStatus = 'idle'; state.Error = null; state.Data = action.payload.Data; if (action.meta.arg.filter != null) state.Filter = (_a = action.meta.arg) === null || _a === void 0 ? void 0 : _a.filter; if (action.meta.arg.parentID != null) state.ParentID = (_b = action.meta.arg) === null || _b === void 0 ? void 0 : _b.parentID; if (state.SortField === action.meta.arg.sortField) state.Ascending = !state.Ascending; else if (action.meta.arg.sortField != null) state.SortField = action.meta.arg.sortField; }); builder.addCase(this.SetStatusToChanged.pending, function (state) { state.FetchStatus = 'changed'; }); }; return ReadOnlyGenericSlice; }()); exports.default = ReadOnlyGenericSlice;