UNPKG

@etsoo/materialui

Version:

TypeScript Material-UI Implementation

91 lines (90 loc) 2.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GridUtils = void 0; const react_1 = require("@etsoo/react"); /** * Grid utilities */ var GridUtils; (function (GridUtils) { /** * Create data loader * @param props Props * @param template Field template * @param cacheKey Cache key * @param keepSource Keep source or not * @returns Request data */ function createLoader(props, template, cacheKey, keepSource) { const { data, ...rest } = props; const formData = (0, react_1.GridDataGetData)(data, template, keepSource); if (cacheKey) sessionStorage.setItem(`${cacheKey}-searchbar`, JSON.stringify(formData)); return { ...formData, ...rest }; } GridUtils.createLoader = createLoader; /** * Get cache data * @param cacheKey Cache key * @param cacheMinutes Cache minutes * @returns */ function getCacheData(cacheKey, cacheMinutes) { const cacheSource = sessionStorage.getItem(cacheKey); if (cacheSource) { const cacheData = JSON.parse(cacheSource); if (new Date().valueOf() - cacheData.creation > cacheMinutes * 60000) { sessionStorage.removeItem(`${cacheKey}-searchbar`); sessionStorage.removeItem(cacheKey); return; } return cacheData; } } GridUtils.getCacheData = getCacheData; /** * Get search data * @param cacheKey Cache key * @returns Result */ function getSearchData(cacheKey) { if (cacheKey) { const data = sessionStorage.getItem(`${cacheKey}-searchbar`); if (data) { return JSON.parse(data); } } } GridUtils.getSearchData = getSearchData; /** * Get update rows handler * @param cacheKey Cache key * @returns Handler */ function getUpdateRowsHandler(cacheKey) { return (rows, state) => { const page = state.queryPaging.currentPage ?? 0; if (page > 0 && cacheKey) { const data = { rows, state, creation: new Date().valueOf() }; sessionStorage.setItem(cacheKey, JSON.stringify(data)); } }; } GridUtils.getUpdateRowsHandler = getUpdateRowsHandler; /** * Merget search date to state * @param state State * @param searchData Search data */ function mergeSearchData(state, searchData) { if (searchData == null) return; state.data ??= {}; Object.assign(state.data, searchData); } GridUtils.mergeSearchData = mergeSearchData; })(GridUtils || (exports.GridUtils = GridUtils = {}));