1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import { Dispatch, compose } from 'redux';
7 import { connect } from 'react-redux';
8 import { InjectedFormProps, formValueSelector } from 'redux-form';
9 import { Grid, withStyles, StyleRulesCallback, WithStyles, Button } from '@material-ui/core';
10 import { RootState } from '~/store/store';
12 SEARCH_BAR_ADVANCED_FORM_NAME,
13 changeAdvancedFormProperty,
14 resetAdvancedFormProperty
15 } from '~/store/search-bar/search-bar-actions';
16 import { PropertyValue } from '~/models/search-bar';
17 import { ArvadosTheme } from '~/common/custom-theme';
18 import { SearchBarKeyField, SearchBarValueField } from '~/views-components/form-fields/search-bar-form-fields';
19 import { Chips } from '~/components/chips/chips';
20 import { formatPropertyValue } from "~/common/formatters";
21 import { Vocabulary } from '~/models/vocabulary';
22 import { connectVocabulary } from '../resource-properties-form/property-field-common';
23 import * as _ from 'lodash';
25 type CssRules = 'label' | 'button';
27 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
29 color: theme.palette.grey["500"],
30 fontSize: '0.8125rem',
38 interface SearchBarAdvancedPropertiesViewDataProps {
42 propertyValues: PropertyValue;
43 fields: PropertyValue[];
44 vocabulary: Vocabulary;
47 interface SearchBarAdvancedPropertiesViewActionProps {
49 addProp: (propertyValues: PropertyValue, properties: PropertyValue[]) => void;
50 getAllFields: (propertyValues: PropertyValue[]) => PropertyValue[] | [];
53 type SearchBarAdvancedPropertiesViewProps = SearchBarAdvancedPropertiesViewDataProps
54 & SearchBarAdvancedPropertiesViewActionProps
55 & InjectedFormProps & WithStyles<CssRules>;
57 const selector = formValueSelector(SEARCH_BAR_ADVANCED_FORM_NAME);
58 const mapStateToProps = (state: RootState) => {
60 propertyValues: selector(state, 'key', 'value', 'keyID', 'valueID')
64 const mapDispatchToProps = (dispatch: Dispatch) => ({
65 setProps: (propertyValues: PropertyValue[]) => {
66 dispatch<any>(changeAdvancedFormProperty('properties', propertyValues));
68 addProp: (propertyValue: PropertyValue, properties: PropertyValue[]) => {
69 // Remove potential duplicates
70 properties = properties.filter(x => ! _.isEqual(
72 key: x.keyID || x.key,
73 value: x.valueID || x.value
75 key: propertyValue.keyID || propertyValue.key,
76 value: propertyValue.valueID || propertyValue.value
78 dispatch<any>(changeAdvancedFormProperty(
80 [...properties, propertyValue]
82 dispatch<any>(resetAdvancedFormProperty('key'));
83 dispatch<any>(resetAdvancedFormProperty('value'));
84 dispatch<any>(resetAdvancedFormProperty('keyID'));
85 dispatch<any>(resetAdvancedFormProperty('valueID'));
87 getAllFields: (fields: any) => {
88 return fields.getAll() || [];
92 export const SearchBarAdvancedPropertiesView = compose(
94 connect(mapStateToProps, mapDispatchToProps))(
96 ({ classes, fields, propertyValues, setProps, addProp, getAllFields, vocabulary }: SearchBarAdvancedPropertiesViewProps) =>
97 <Grid container item xs={12} spacing={16}>
98 <Grid item xs={2} className={classes.label}>Properties</Grid>
100 <SearchBarKeyField />
103 <SearchBarValueField />
105 <Grid container item xs={2} justify='flex-end' alignItems="center">
106 <Button className={classes.button} onClick={() => addProp(propertyValues, getAllFields(fields))}
110 disabled={!Boolean(propertyValues.key && propertyValues.value)}>
115 <Grid container item xs={10} spacing={8}>
116 <Chips values={getAllFields(fields)}
119 getLabel={(field: PropertyValue) => formatPropertyValue(field, vocabulary)} />