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 { PropertyValues } 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';
21 type CssRules = 'label' | 'button';
23 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
25 color: theme.palette.grey["500"],
26 fontSize: '0.8125rem',
34 interface SearchBarAdvancedPropertiesViewDataProps {
38 propertyValues: PropertyValues;
39 fields: PropertyValues[];
42 interface SearchBarAdvancedPropertiesViewActionProps {
44 addProp: (propertyValues: PropertyValues) => void;
45 getAllFields: (propertyValues: PropertyValues[]) => PropertyValues[] | [];
48 type SearchBarAdvancedPropertiesViewProps = SearchBarAdvancedPropertiesViewDataProps
49 & SearchBarAdvancedPropertiesViewActionProps
50 & InjectedFormProps & WithStyles<CssRules>;
52 const selector = formValueSelector(SEARCH_BAR_ADVANCE_FORM_NAME);
53 const mapStateToProps = (state: RootState) => {
55 propertyValues: selector(state, 'key', 'value')
59 const mapDispatchToProps = (dispatch: Dispatch) => ({
60 setProps: (propertyValues: PropertyValues[]) => {
61 dispatch<any>(changeAdvanceFormProperty('properties', propertyValues));
63 addProp: (propertyValues: PropertyValues) => {
64 dispatch<any>(updateAdvanceFormProperties(propertyValues));
65 dispatch<any>(changeAdvanceFormProperty('key'));
66 dispatch<any>(changeAdvanceFormProperty('value'));
68 getAllFields: (fields: any) => {
69 return fields.getAll() || [];
73 export const SearchBarAdvancedPropertiesView = connect(mapStateToProps, mapDispatchToProps)(
75 ({ classes, fields, propertyValues, setProps, addProp, getAllFields }: SearchBarAdvancedPropertiesViewProps) =>
76 <Grid container item xs={12} spacing={16}>
77 <Grid item xs={2} className={classes.label}>Properties</Grid>
82 <SearchBarValueField />
84 <Grid container item xs={2} justify='flex-end' alignItems="center">
85 <Button className={classes.button} onClick={() => addProp(propertyValues)}
89 disabled={!Boolean(propertyValues.key && propertyValues.value)}>
94 <Grid container item xs={10} spacing={8}>
95 <Chips values={getAllFields(fields)}
98 getLabel={(field: PropertyValues) => `${field.key}: ${field.value}`} />