All files / src/kernel ServerSideFinder.tsx

92.3% Statements 24/26
85.71% Branches 18/21
83.33% Functions 5/6
92% Lines 23/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71                                10x 10x 8x     8x 8x 8x 8x 8x 8x 8x 5x 5x 15x 5x 5x 5x 5x       10x                 13x 11x             28x                       6x 6x    
import { SERVER_URL } from '@config/base';
import { SearchConditionProps, SearchConditions, SearchValue, TableMetaProps } from '@props/RecordProps';
import { getDefaultMatcher } from "@kernel/ServerSideSearcherMapping";
 
/**
 * Set finder condition user inputed to session storage User inputed conditions
 * contains only value, and this method will transfer them to valid search
 * condition object, and set to session storage
 * @param domainName domain name
 * @param conditions conditions
 * @param columns columns meta of the to search domain
 */
export function setFinderCondition(domainName: string,
  conditions: {
    [propName: string]: SearchValue
  }, columns: Array<TableMetaProps>): void {
  const searchConditions: SearchConditions = {} as SearchConditions;
  for (const property in conditions) {
    Iif (!Object.prototype.hasOwnProperty.call(conditions, property)) {
      continue;
    }
    const val = conditions[property];
    const isNonEmptyString = (typeof val === 'string' && val !== '');
    const isNonEmptyNumber = (typeof val === 'number' && !isNaN(val));
    const isBoolean = (typeof val === 'boolean');
    const notNull = (val != null);
    const isNonEmptyArray = (Array.isArray(val) && (val as Array<unknown>).length > 0);
    if (notNull && (isNonEmptyString || isNonEmptyArray || isNonEmptyNumber || isBoolean)) {
      const condition: SearchConditionProps = {} as SearchConditionProps;
      condition.columnKey = property;
      const columnMeta = columns.find(column => column.key === property);
      Eif (columnMeta != null) {
        condition.matchMode = getDefaultMatcher(columnMeta);
        condition.value = val;
        searchConditions[property] = condition;
      }
    }
  }
  saveFinderConditionsToCache(domainName, searchConditions);
}
 
/**
 * Save finder conditions to session storage cache
 * @param domainName domain to search
 * @param conditions Conditions
 */
export function saveFinderConditionsToCache(domainName: string, conditions: SearchConditions): void {
  if (conditions != null) {
    sessionStorage.setItem(
      getFinderConditionKey(domainName), JSON.stringify(conditions)
    );
  }
}
 
export function getFinderConditionKey(domainName: string): string {
  return `${domainName}_${SERVER_URL}_finderConditions`;
}
 
/**
 * clear finder conditions from sessionStorage
 * @param domainName name of the domain to clear
 */
export function clearFinderConditions(domainName: string): void {
  sessionStorage.removeItem(getFinderConditionKey(domainName));
}
 
export function getFinderConditions(domainName: string): SearchConditions {
  const cache = sessionStorage.getItem(getFinderConditionKey(domainName));
  return (cache == null)? cache: JSON.parse(cache);
}