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 } 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 updateAdvanceFormProperties
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";
22 type CssRules = 'label' | 'button';
24 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
26 color: theme.palette.grey["500"],
27 fontSize: '0.8125rem',
35 interface SearchBarAdvancedPropertiesViewDataProps {
39 propertyValues: PropertyValue;
40 fields: PropertyValue[];
43 interface SearchBarAdvancedPropertiesViewActionProps {
45 addProp: (propertyValues: PropertyValue) => void;
46 getAllFields: (propertyValues: PropertyValue[]) => PropertyValue[] | [];
49 type SearchBarAdvancedPropertiesViewProps = SearchBarAdvancedPropertiesViewDataProps
50 & SearchBarAdvancedPropertiesViewActionProps
51 & InjectedFormProps & WithStyles<CssRules>;
53 const selector = formValueSelector(SEARCH_BAR_ADVANCE_FORM_NAME);
54 const mapStateToProps = (state: RootState) => {
56 propertyValues: selector(state, 'key', 'value')
60 const mapDispatchToProps = (dispatch: Dispatch) => ({
61 setProps: (propertyValues: PropertyValue[]) => {
62 dispatch<any>(changeAdvanceFormProperty('properties', propertyValues));
64 addProp: (propertyValues: PropertyValue) => {
65 dispatch<any>(updateAdvanceFormProperties(propertyValues));
66 dispatch<any>(changeAdvanceFormProperty('key'));
67 dispatch<any>(changeAdvanceFormProperty('value'));
69 getAllFields: (fields: any) => {
70 return fields.getAll() || [];
74 export const SearchBarAdvancedPropertiesView = connect(mapStateToProps, mapDispatchToProps)(
76 ({ classes, fields, propertyValues, setProps, addProp, getAllFields }: SearchBarAdvancedPropertiesViewProps) =>
77 <Grid container item xs={12} spacing={16}>
78 <Grid item xs={2} className={classes.label}>Properties</Grid>
83 <SearchBarValueField />
85 <Grid container item xs={2} justify='flex-end' alignItems="center">
86 <Button className={classes.button} onClick={() => addProp(propertyValues)}
90 disabled={!Boolean(propertyValues.key && propertyValues.value)}>
95 <Grid container item xs={10} spacing={8}>
96 <Chips values={getAllFields(fields)}
99 getLabel={(field: PropertyValue) => formatPropertyValue(field)} />