UNPKG

@gpa-gemstone/react-interactive

Version:
165 lines (164 loc) 7.69 kB
"use strict"; // ****************************************************************************************************** // GenericController.tsx - Gbtc // // Copyright © 2023, 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: // ---------------------------------------------------------------------------------------------------- // 04/05/2024 - C. Lackner // Generated original version of source code. // ****************************************************************************************************** var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; 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; }; Object.defineProperty(exports, "__esModule", { value: true }); var $ = __importStar(require("jquery")); var GenericController = /** @class */ (function () { /** * Creates a new Controler of type T, which can be used to perform basic CRUD operations against * a specified web api. without the Reduxstore associated with GenericSlice * @typeParam T - Model of Generic Slice * @param {string} apiPath - string containing relative path to web api * @returns a new GenericController<T> */ function GenericController(apiPath, defaultSort, ascending) { if (ascending === void 0) { ascending = true; } var _this = this; this.APIPath = ""; this.APIPath = apiPath; this.DefaultSort = defaultSort; this.Ascending = ascending; var fetch = function (parentID, sortField, ascending) { var sort = sortField; var asc = ascending; sort = sort === undefined ? _this.DefaultSort : sort; asc = asc === undefined ? _this.Ascending : asc; var handle = _this.GetRecords(asc, sort, parentID !== null && parentID !== void 0 ? parentID : undefined); return handle.then(function (d) { return $.Deferred().resolve(JSON.parse(d.toString())).promise(); }).promise(handle); }; var dBAction = function (verb, record) { var handle = _this.Action(verb, record); return handle; }; var dBSearch = function (filter, sortField, ascending, parentID) { var sort = sortField; var asc = ascending; sort = sort === undefined ? _this.DefaultSort : sort; asc = asc === undefined ? _this.Ascending : asc; var handle = _this.Search(filter, asc, sort, parentID); return handle.then(function (d) { return $.Deferred().resolve(JSON.parse(d)).promise(); }).promise(handle); }; var dBPage = function (filter, sortField, ascending, page, parentID) { var sort = sortField; var asc = ascending; var pg = page !== null && page !== void 0 ? page : 0; sort = sort === undefined ? _this.DefaultSort : sort; asc = asc === undefined ? _this.Ascending : asc; var handle = _this.FetchPage(filter, asc, sort, pg, parentID); return handle.done(function (d) { return ({ NumberOfPages: d.NumberOfPages, Data: JSON.parse(d.Data), RecordsPerPage: d.RecordsPerPage, TotalRecords: d.TotalRecords }); }); }; this.Fetch = fetch; this.DBAction = dBAction; this.DBSearch = dBSearch; this.PagedSearch = dBPage; } GenericController.prototype.GetRecords = function (ascending, sortField, parentID) { return $.ajax({ type: "GET", url: "".concat(this.APIPath).concat((parentID != null ? '/' + parentID : ''), "/").concat(sortField.toString(), "/").concat((ascending !== null && ascending !== void 0 ? ascending : false) ? '1' : '0'), contentType: "application/json; charset=utf-8", dataType: 'json', cache: true, async: true }); }; GenericController.prototype.Action = function (verb, record) { var action = ''; if (verb === 'POST') action = 'Add'; else if (verb === 'DELETE') action = 'Delete'; else if (verb === 'PATCH') action = 'Update'; return $.ajax({ type: verb, url: "".concat(this.APIPath, "/").concat(action), contentType: "application/json; charset=utf-8", dataType: 'json', data: JSON.stringify(__assign({}, record)), cache: false, async: true }); }; GenericController.prototype.Search = function (filter, ascending, sortField, parentID) { return $.ajax({ type: 'POST', url: "".concat(this.APIPath, "/").concat(parentID != null ? "".concat(parentID, "/") : '', "SearchableList"), contentType: "application/json; charset=utf-8", dataType: 'json', data: JSON.stringify({ Searches: filter, OrderBy: sortField, Ascending: ascending }), cache: false, async: true }); }; GenericController.prototype.FetchPage = function (filter, ascending, sortField, page, parentID) { return $.ajax({ type: 'POST', url: "".concat(this.APIPath, "/").concat(parentID != null ? "".concat(parentID, "/") : '', "PagedList/").concat(page), contentType: "application/json; charset=utf-8", dataType: 'json', data: JSON.stringify({ Searches: filter, OrderBy: sortField, Ascending: ascending }), cache: false, async: true }); }; return GenericController; }()); exports.default = GenericController;