@antscorp/antsomi-ui 1.3.5-beta.623 → 1.3.5-beta.624

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -41,6 +41,8 @@ export type TConfig<TTableType = any> = {
41
41
  type: number;
42
42
  objectId: number;
43
43
  };
44
+ /** Disable local storage cache */
45
+ disabledCache?: boolean;
44
46
  };
45
47
  type TGeneralTableColumnType<TTableType> = Omit<ColumnType<TTableType>, 'render'> & {
46
48
  link?: ((record: TTableType) => string) | string;
@@ -104,6 +106,8 @@ export interface UseDataTableListingProps<TTableType = any, TSearchType = any> {
104
106
  };
105
107
  search?: TSearchProps<TSearchType>;
106
108
  filter?: {
109
+ /** Init default filters, this filter will show in UI instead of external filters */
110
+ defaultFilters?: FilterItem[];
107
111
  externalFilters?: FilterItem[];
108
112
  includeDataType?: boolean;
109
113
  matchesAny?: {
@@ -78,14 +78,14 @@ export function useDataTableListing(props) {
78
78
  // State
79
79
  const [state, setState] = useState(initialState);
80
80
  // Variables
81
- const { env = appConfig === null || appConfig === void 0 ? void 0 : appConfig.env, auth = appConfig === null || appConfig === void 0 ? void 0 : appConfig.auth, api, object } = config;
81
+ const { env = appConfig === null || appConfig === void 0 ? void 0 : appConfig.env, auth = appConfig === null || appConfig === void 0 ? void 0 : appConfig.auth, api, object, disabledCache } = config;
82
82
  const { column: apiColumn, filter: apiFilter, listing: apiListing, search: apiSearch, matchesAny: apiMatchesAny, } = api || {};
83
83
  const _d = apiColumn || {}, { url: columnUrl = `${COLUMN_DOMAIN[env || 'development']}/api/column`, enabled: enabledApiColumn = true } = _d, restOfApiColumnRequest = __rest(_d, ["url", "enabled"]);
84
84
  const _e = apiFilter || {}, { url: filterUrl = `${COLUMN_DOMAIN[env || 'development']}/api/filter`, enabled: enabledApiFilter = true } = _e, restOfApiFilterRequest = __rest(_e, ["url", "enabled"]);
85
85
  const _f = apiListing || {}, { url: listingUrl = '', enabled: enabledApiListing = true } = _f, restOfApiListingRequest = __rest(_f, ["url", "enabled"]);
86
86
  const _g = apiSearch || {}, { url: searchUrl = '', enabled: enabledApiSearch = true } = _g, restOfApiSearchRequest = __rest(_g, ["url", "enabled"]);
87
87
  const { filters, selectedFilterId, pagination, selectedRowKeys, searchValue, table, sorter, selectedRow, selectedFilterItem, expandedRowKeys, groupBy, modeView, } = state;
88
- const { externalFilters, includeDataType = false, matchesAny, formatFilterValue, } = filterProps || {};
88
+ const { externalFilters, defaultFilters, includeDataType = false, matchesAny, formatFilterValue, } = filterProps || {};
89
89
  const { sortDirectionKey = 'az', mainColumnKey, expandColumnKey, expandable } = tableProps || {};
90
90
  const { itemProps: gridViewItemProps } = gridViewProps || {};
91
91
  const { columnKeys: groupByColumnKeys, defaultSelectedKey: groupByDefaultSelectedKey } = groupByProps || {};
@@ -325,12 +325,21 @@ export function useDataTableListing(props) {
325
325
  }
326
326
  return children;
327
327
  }, [expandColumnKey, expandable, expandedRowKeys, mainColumnKey]);
328
+ const handleUpdateLocalStorage = useCallback((dataTableLocalStorage) => {
329
+ if (disabledCache) {
330
+ return;
331
+ }
332
+ const localStorageValues = parseJSONFromLocalStorage(localStorageKey) || {};
333
+ // Set Local Storage
334
+ localStorage.setItem(localStorageKey, JSON.stringify(Object.assign(Object.assign({}, localStorageValues), dataTableLocalStorage)));
335
+ }, [disabledCache, localStorageKey]);
328
336
  const handleUpdateGroupBy = useCallback((groupBy) => {
329
337
  const localStorageValues = parseJSONFromLocalStorage(localStorageKey) || {};
330
338
  setState(prev => (Object.assign(Object.assign({}, prev), { groupBy: Object.assign(Object.assign({}, prev.groupBy), groupBy) })));
331
- // Set Local Storage
332
- localStorage.setItem(localStorageKey, JSON.stringify(Object.assign(Object.assign({}, localStorageValues), { groupBy: Object.assign(Object.assign({}, localStorageValues.groupBy), groupBy) })));
333
- }, [localStorageKey]);
339
+ handleUpdateLocalStorage({
340
+ groupBy: Object.assign(Object.assign({}, localStorageValues.groupBy), groupBy),
341
+ });
342
+ }, [handleUpdateLocalStorage, localStorageKey]);
334
343
  const tableColumns = useDeepCompareMemo(() => {
335
344
  let data = [];
336
345
  const { columns } = tableProps || {};
@@ -478,6 +487,10 @@ export function useDataTableListing(props) {
478
487
  */
479
488
  useDeepCompareEffect(() => {
480
489
  const { filter, pagination, table, sorter, groupBy } = localStorageValues || {};
490
+ // If disabled cache then do nothing
491
+ if (disabledCache) {
492
+ return;
493
+ }
481
494
  if (!isEmpty(filter)) {
482
495
  const { filters, selectedFilterId } = filter || {};
483
496
  setState(prev => (Object.assign(Object.assign({}, prev), { selectedFilterId,
@@ -575,30 +588,33 @@ export function useDataTableListing(props) {
575
588
  const localStorageValues = parseJSONFromLocalStorage(localStorageKey) || {};
576
589
  // Set State
577
590
  setState(prev => (Object.assign(Object.assign({}, prev), { pagination: Object.assign(Object.assign({}, prev.pagination), pagination) })));
578
- // Set Local Storage
579
- localStorage.setItem(localStorageKey, JSON.stringify(Object.assign(Object.assign({}, localStorageValues), { pagination: Object.assign(Object.assign({}, localStorageValues === null || localStorageValues === void 0 ? void 0 : localStorageValues.pagination), pagination) })));
580
- }, [localStorageKey]);
591
+ handleUpdateLocalStorage({
592
+ pagination: Object.assign(Object.assign({}, localStorageValues === null || localStorageValues === void 0 ? void 0 : localStorageValues.pagination), pagination),
593
+ });
594
+ }, [handleUpdateLocalStorage, localStorageKey]);
581
595
  const handleUpdateFilter = useCallback((filter) => {
582
596
  const localStorageValues = parseJSONFromLocalStorage(localStorageKey) || {};
583
597
  // Set State
584
598
  setState(prev => (Object.assign(Object.assign(Object.assign({}, prev), filter), { pagination: Object.assign(Object.assign({}, prev.pagination), { page: 1 }) })));
585
- // Set Local Storage
586
- localStorage.setItem(localStorageKey, JSON.stringify(Object.assign(Object.assign({}, localStorageValues), { filter: Object.assign(Object.assign({}, localStorageValues === null || localStorageValues === void 0 ? void 0 : localStorageValues.filter), filter),
599
+ handleUpdateLocalStorage({
600
+ filter: Object.assign(Object.assign({}, localStorageValues === null || localStorageValues === void 0 ? void 0 : localStorageValues.filter), filter),
587
601
  // Reset page to 1
588
- pagination: Object.assign(Object.assign({}, localStorageValues === null || localStorageValues === void 0 ? void 0 : localStorageValues.pagination), { page: 1 }) })));
589
- }, [localStorageKey]);
602
+ pagination: Object.assign(Object.assign({}, localStorageValues === null || localStorageValues === void 0 ? void 0 : localStorageValues.pagination), { page: 1 }),
603
+ });
604
+ }, [handleUpdateLocalStorage, localStorageKey]);
590
605
  const handleUpdateTable = useCallback((table) => {
591
606
  const localStorageValues = parseJSONFromLocalStorage(localStorageKey) || {};
592
607
  setState(prev => (Object.assign(Object.assign({}, prev), { table: Object.assign(Object.assign({}, prev.table), table) })));
593
- // Set Local Storage
594
- localStorage.setItem(localStorageKey, JSON.stringify(Object.assign(Object.assign({}, localStorageValues), { table: Object.assign(Object.assign({}, localStorageValues === null || localStorageValues === void 0 ? void 0 : localStorageValues.table), table) })));
595
- }, [localStorageKey]);
608
+ handleUpdateLocalStorage({
609
+ table: Object.assign(Object.assign({}, localStorageValues === null || localStorageValues === void 0 ? void 0 : localStorageValues.table), table),
610
+ });
611
+ }, [handleUpdateLocalStorage, localStorageKey]);
596
612
  const handleUpdateSorter = useCallback((sorter) => {
597
- const localStorageValues = parseJSONFromLocalStorage(localStorageKey) || {};
598
613
  setState(prev => (Object.assign(Object.assign({}, prev), { sorter })));
599
- // Set Local Storage
600
- localStorage.setItem(localStorageKey, JSON.stringify(Object.assign(Object.assign({}, localStorageValues), { sorter })));
601
- }, [localStorageKey]);
614
+ handleUpdateLocalStorage({
615
+ sorter,
616
+ });
617
+ }, [handleUpdateLocalStorage]);
602
618
  /* Filter */
603
619
  const onChangeFilters = useCallback((filters) => {
604
620
  handleUpdateFilter({
@@ -689,6 +705,15 @@ export function useDataTableListing(props) {
689
705
  fetchNextPage();
690
706
  }
691
707
  }, [fetchNextPage, isFetchingNextPage]);
708
+ // Handle Effects
709
+ /**
710
+ * Update filters from default filters
711
+ * */
712
+ useDeepCompareEffect(() => {
713
+ if (defaultFilters) {
714
+ handleUpdateFilter({ filters: defaultFilters || [] });
715
+ }
716
+ }, [defaultFilters, handleUpdateFilter]);
692
717
  return {
693
718
  /* Modify Column */
694
719
  modifyColumn: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antscorp/antsomi-ui",
3
- "version": "1.3.5-beta.623",
3
+ "version": "1.3.5-beta.624",
4
4
  "description": "An enterprise-class UI design language and React UI library.",
5
5
  "sideEffects": [
6
6
  "dist/*",