UNPKG

@gpa-gemstone/common-pages

Version:
112 lines (111 loc) 5.85 kB
"use strict"; //****************************************************************************************************** // useSearchData.tsx - Gbtc // // Copyright (c) 2024, 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 may license 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: // ---------------------------------------------------------------------------------------------------- // 03/24/2025 - Preston Crawford // 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; }; Object.defineProperty(exports, "__esModule", { value: true }); var React = __importStar(require("react")); var ControllerFunctions_1 = require("../ControllerFunctions"); var Gemstone_1 = require("../Gemstone"); /** * Custom hook to fetch and paginate searchable data from a read-only controller endpoint. * @typeParam T - The type of the records being searched. * @param page - The current page number to fetch. * @param sortField - The field to sort the results by. * @param searchFilter - The search filters to apply to the query. * @param ascending - Whether the results should be sorted in ascending order. * @param controllerPath - The path to the read-only controller endpoint. * @param ReFetchDataCounter - Optional counter that triggers a re-fetch whenever its value changes. * @returns An object containing the search results, the search status, the pagination status and the pagination info. */ var useSearchData = function (page, sortField, searchFilter, ascending, controllerPath, ReFetchDataCounter) { var fetchHandle = React.useRef(null); var paginationHandle = React.useRef(null); var _a = React.useState([]), results = _a[0], setResults = _a[1]; var _b = React.useState('uninitiated'), searchStatus = _b[0], setSearchStatus = _b[1]; var _c = React.useState('uninitiated'), paginationStatus = _c[0], setPaginationStatus = _c[1]; var _d = React.useState({ TotalCount: 0, PageCount: 0, PageSize: 0 }), pageInfo = _d[0], setPageInfo = _d[1]; var Queries = React.useMemo(function () { return new ControllerFunctions_1.ReadOnlyControllerFunctions(controllerPath); }, [controllerPath]); var stringifiedFilter = React.useMemo(function () { return JSON.stringify(searchFilter); }, [searchFilter]); // Memoize the searchFilter to avoid unnecessary re-fetches. var memoizedSearchFilter = React.useMemo(function () { return Gemstone_1.Gemstone.HelperFunctions.getSearchFilter(JSON.parse(stringifiedFilter)); }, [stringifiedFilter]); var reFetchData = React.useCallback(function () { setSearchStatus('loading'); setPaginationStatus('loading'); fetchHandle.current = Queries.SearchPage(page, sortField, ascending, memoizedSearchFilter).done(function (data) { setResults(data); setSearchStatus('idle'); }).fail(function () { return setSearchStatus('error'); }); paginationHandle.current = Queries.GetPageInfo(memoizedSearchFilter).done(function (d) { setPageInfo(d); setPaginationStatus('idle'); }).fail(function () { return setPaginationStatus('error'); }); var cleanup = function () { var _a, _b; if (((_a = fetchHandle.current) === null || _a === void 0 ? void 0 : _a.abort) != null) fetchHandle.current.abort(); if (((_b = paginationHandle.current) === null || _b === void 0 ? void 0 : _b.abort) != null) paginationHandle.current.abort(); }; return { fetchHandle: fetchHandle.current, cleanup: cleanup }; }, [Queries, page, sortField, ascending, memoizedSearchFilter]); React.useEffect(function () { var cleanup = reFetchData().cleanup; return cleanup; }, [reFetchData, ReFetchDataCounter]); return { SearchResults: results, SearchStatus: searchStatus, PaginationStatus: paginationStatus, PageInfo: pageInfo }; }; exports.default = useSearchData;