Browse Source

move generateIdByHash to common utils

czhen 4 years ago
parent
commit
2fcd04f957
2 changed files with 12 additions and 13 deletions
  1. 8 13
      client/src/components/advancedSearch/Filter.tsx
  2. 4 0
      client/src/utils/Common.ts

+ 8 - 13
client/src/components/advancedSearch/Filter.tsx

@@ -10,7 +10,7 @@ import {
 import FilterListIcon from '@material-ui/icons/FilterList';
 import FilterListIcon from '@material-ui/icons/FilterList';
 import AdvancedDialog from './Dialog';
 import AdvancedDialog from './Dialog';
 import { FilterProps, ConditionData } from './Types';
 import { FilterProps, ConditionData } from './Types';
-import { generateHashCode } from '../../utils/Common';
+import { generateIdByHash } from '../../utils/Common';
 
 
 const Filter = function Filter(props: FilterProps) {
 const Filter = function Filter(props: FilterProps) {
   const {
   const {
@@ -90,11 +90,6 @@ const Filter = function Filter(props: FilterProps) {
     func(expression);
     func(expression);
   };
   };
 
 
-  // Generate id for each element.
-  const generateId = (salt?: string) => {
-    return generateHashCode(`${new Date().getTime().toString()}-${salt}`);
-  };
-
   /**
   /**
    * Insert "OR" operator into specified position.
    * Insert "OR" operator into specified position.
    * @param targetId The break operator will be inserted after the target one.
    * @param targetId The break operator will be inserted after the target one.
@@ -103,9 +98,9 @@ const Filter = function Filter(props: FilterProps) {
     if (!targetId) {
     if (!targetId) {
       setFilteredFlatConditions([
       setFilteredFlatConditions([
         ...flatConditions,
         ...flatConditions,
-        { id: generateId('break'), type: 'break' },
+        { id: generateIdByHash('break'), type: 'break' },
         {
         {
-          id: generateId('condition'),
+          id: generateIdByHash('condition'),
           type: 'condition',
           type: 'condition',
           field: '',
           field: '',
           operator: '<',
           operator: '<',
@@ -117,7 +112,7 @@ const Filter = function Filter(props: FilterProps) {
     const formerConditons = [...flatConditions];
     const formerConditons = [...flatConditions];
     const newConditions = formerConditons.reduce((prev, item) => {
     const newConditions = formerConditons.reduce((prev, item) => {
       if (item.id === targetId) {
       if (item.id === targetId) {
-        return [...prev, item, { id: generateId('break'), type: 'break' }];
+        return [...prev, item, { id: generateIdByHash('break'), type: 'break' }];
       }
       }
       return [...prev, item];
       return [...prev, item];
     }, []);
     }, []);
@@ -139,7 +134,7 @@ const Filter = function Filter(props: FilterProps) {
   const addCondition = (targetId?: string, beforeTarget?: boolean) => {
   const addCondition = (targetId?: string, beforeTarget?: boolean) => {
     const formerConditons = [...flatConditions];
     const formerConditons = [...flatConditions];
     const newItem = {
     const newItem = {
-      id: generateId('condition'),
+      id: generateIdByHash('condition'),
       type: 'condition',
       type: 'condition',
       field: '',
       field: '',
       operator: '<',
       operator: '<',
@@ -155,7 +150,7 @@ const Filter = function Filter(props: FilterProps) {
         const newItems = [
         const newItems = [
           item,
           item,
           {
           {
-            id: generateId('condition'),
+            id: generateIdByHash('condition'),
             type: 'condition',
             type: 'condition',
             field: '',
             field: '',
             operator: '<',
             operator: '<',
@@ -204,7 +199,7 @@ const Filter = function Filter(props: FilterProps) {
     setIsConditionsLegal(false);
     setIsConditionsLegal(false);
     setFilteredFlatConditions([
     setFilteredFlatConditions([
       {
       {
-        id: generateId(),
+        id: generateIdByHash(),
         type: 'condition',
         type: 'condition',
         field: '',
         field: '',
         operator: '<',
         operator: '<',
@@ -254,7 +249,7 @@ const Filter = function Filter(props: FilterProps) {
   const handleReset = () => {
   const handleReset = () => {
     setFilteredFlatConditions([
     setFilteredFlatConditions([
       {
       {
-        id: generateId('condition'),
+        id: generateIdByHash('condition'),
         type: 'condition',
         type: 'condition',
         field: '',
         field: '',
         operator: '<',
         operator: '<',

+ 4 - 0
client/src/utils/Common.ts

@@ -57,3 +57,7 @@ export const generateHashCode = (source: string) => {
   }
   }
   return hash.toString();
   return hash.toString();
 };
 };
+
+export const generateIdByHash = (salt?: string) => {
+  return generateHashCode(`${new Date().getTime().toString()}-${salt}`);
+};