15781: Avoids showing duplicate property 'chips' on search editor.
authorLucas Di Pentima <lucas@di-pentima.com.ar>
Fri, 14 Feb 2020 19:31:44 +0000 (16:31 -0300)
committerLucas Di Pentima <lucas@di-pentima.com.ar>
Fri, 14 Feb 2020 19:31:44 +0000 (16:31 -0300)
Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas@di-pentima.com.ar>

src/views-components/search-bar/search-bar-advanced-properties-view.tsx

index 8add4b025b4dfb29e7f8a957287bf91a442360f1..f3509a028988f7f829c018c8b176df55f86f3418 100644 (file)
@@ -20,6 +20,7 @@ import { Chips } from '~/components/chips/chips';
 import { formatPropertyValue } from "~/common/formatters";
 import { Vocabulary } from '~/models/vocabulary';
 import { connectVocabulary } from '../resource-properties-form/property-field-common';
+import * as _ from 'lodash';
 
 type CssRules = 'label' | 'button';
 
@@ -45,7 +46,7 @@ interface SearchBarAdvancedPropertiesViewDataProps {
 
 interface SearchBarAdvancedPropertiesViewActionProps {
     setProps: () => void;
-    setProp: (propertyValues: PropertyValue, properties: PropertyValue[]) => void;
+    addProp: (propertyValues: PropertyValue, properties: PropertyValue[]) => void;
     getAllFields: (propertyValues: PropertyValue[]) => PropertyValue[] | [];
 }
 
@@ -64,7 +65,16 @@ const mapDispatchToProps = (dispatch: Dispatch) => ({
     setProps: (propertyValues: PropertyValue[]) => {
         dispatch<any>(changeAdvancedFormProperty('properties', propertyValues));
     },
-    setProp: (propertyValue: PropertyValue, properties: PropertyValue[]) => {
+    addProp: (propertyValue: PropertyValue, properties: PropertyValue[]) => {
+        // Remove potential duplicates
+        properties = properties.filter(x => ! _.isEqual(
+            {
+                key: x.keyID || x.key,
+                value: x.valueID || x.value
+            }, {
+                key: propertyValue.keyID || propertyValue.key,
+                value: propertyValue.valueID || propertyValue.value
+            }));
         dispatch<any>(changeAdvancedFormProperty(
             'properties',
             [...properties, propertyValue]
@@ -83,7 +93,7 @@ export const SearchBarAdvancedPropertiesView = compose(
     connectVocabulary,
     connect(mapStateToProps, mapDispatchToProps))(
     withStyles(styles)(
-        ({ classes, fields, propertyValues, setProps, setProp, getAllFields, vocabulary }: SearchBarAdvancedPropertiesViewProps) =>
+        ({ classes, fields, propertyValues, setProps, addProp, getAllFields, vocabulary }: SearchBarAdvancedPropertiesViewProps) =>
             <Grid container item xs={12} spacing={16}>
                 <Grid item xs={2} className={classes.label}>Properties</Grid>
                 <Grid item xs={4}>
@@ -93,7 +103,7 @@ export const SearchBarAdvancedPropertiesView = compose(
                     <SearchBarValueField />
                 </Grid>
                 <Grid container item xs={2} justify='flex-end' alignItems="center">
-                    <Button className={classes.button} onClick={() => setProp(propertyValues, getAllFields(fields))}
+                    <Button className={classes.button} onClick={() => addProp(propertyValues, getAllFields(fields))}
                         color="primary"
                         size='small'
                         variant="contained"