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_ADVANCE_FORM_NAME,
13 changeAdvanceFormProperty,
14 resetAdvanceFormProperty,
15 updateAdvanceFormProperties
16 } from '~/store/search-bar/search-bar-actions';
17 import { PropertyValue } from '~/models/search-bar';
18 import { ArvadosTheme } from '~/common/custom-theme';
19 import { SearchBarKeyField, SearchBarValueField } from '~/views-components/form-fields/search-bar-form-fields';
20 import { Chips } from '~/components/chips/chips';
21 import { formatPropertyValue } from "~/common/formatters";
22 import { Vocabulary } from '~/models/vocabulary';
23 import { connectVocabulary } from '../resource-properties-form/property-field-common';
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) => void;
50 getAllFields: (propertyValues: PropertyValue[]) => PropertyValue[] | [];
53 type SearchBarAdvancedPropertiesViewProps = SearchBarAdvancedPropertiesViewDataProps
54 & SearchBarAdvancedPropertiesViewActionProps
55 & InjectedFormProps & WithStyles<CssRules>;
57 const selector = formValueSelector(SEARCH_BAR_ADVANCE_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>(changeAdvanceFormProperty('properties', propertyValues));
68 addProp: (propertyValues: PropertyValue) => {
69 dispatch<any>(updateAdvanceFormProperties(propertyValues));
70 dispatch<any>(resetAdvanceFormProperty('key'));
71 dispatch<any>(resetAdvanceFormProperty('value'));
72 dispatch<any>(resetAdvanceFormProperty('keyID'));
73 dispatch<any>(resetAdvanceFormProperty('valueID'));
75 getAllFields: (fields: any) => {
76 return fields.getAll() || [];
80 export const SearchBarAdvancedPropertiesView = compose(
82 connect(mapStateToProps, mapDispatchToProps))(
84 ({ classes, fields, propertyValues, setProps, addProp, getAllFields, vocabulary }: SearchBarAdvancedPropertiesViewProps) =>
85 <Grid container item xs={12} spacing={16}>
86 <Grid item xs={2} className={classes.label}>Properties</Grid>
91 <SearchBarValueField />
93 <Grid container item xs={2} justify='flex-end' alignItems="center">
94 <Button className={classes.button} onClick={() => addProp(propertyValues)}
98 disabled={!Boolean(propertyValues.key && propertyValues.value)}>
103 <Grid container item xs={10} spacing={8}>
104 <Chips values={getAllFields(fields)}
107 getLabel={(field: PropertyValue) => formatPropertyValue(field, vocabulary)} />