1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import { reduxForm, InjectedFormProps, reset } from 'redux-form';
7 import { compose, Dispatch } from 'redux';
8 import { Paper, StyleRulesCallback, withStyles, WithStyles, Button, Grid, IconButton, CircularProgress } from '@material-ui/core';
10 SEARCH_BAR_ADVANCE_FORM_NAME, SEARCH_BAR_ADVANCE_FORM_PICKER_ID,
12 setSearchValueFromAdvancedData
13 } from '~/store/search-bar/search-bar-actions';
14 import { ArvadosTheme } from '~/common/custom-theme';
15 import { CloseIcon } from '~/components/icon/icon';
16 import { SearchBarAdvanceFormData } from '~/models/search-bar';
18 SearchBarTypeField, SearchBarClusterField, SearchBarProjectField, SearchBarTrashField,
19 SearchBarDateFromField, SearchBarDateToField, SearchBarPropertiesField,
20 SearchBarSaveSearchField, SearchBarQuerySearchField
21 } from '~/views-components/form-fields/search-bar-form-fields';
22 import { treePickerActions } from "~/store/tree-picker/tree-picker-actions";
24 type CssRules = 'container' | 'closeIcon' | 'label' | 'buttonWrapper'
25 | 'button' | 'circularProgress' | 'searchView' | 'selectGrid';
27 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
29 padding: theme.spacing.unit * 2,
30 borderBottom: `1px solid ${theme.palette.grey["200"]}`,
39 color: theme.palette.grey["500"],
40 fontSize: '0.8125rem',
60 color: theme.palette.common.black,
61 borderRadius: `0 0 ${theme.spacing.unit / 2}px ${theme.spacing.unit / 2}px`
64 marginBottom: theme.spacing.unit * 2
68 // ToDo: maybe we should remove invalid and prostine
69 interface SearchBarAdvancedViewFormDataProps {
75 // ToDo: maybe we should remove tags
76 export interface SearchBarAdvancedViewDataProps {
81 export interface SearchBarAdvancedViewActionProps {
82 closeAdvanceView: () => void;
85 type SearchBarAdvancedViewProps = SearchBarAdvancedViewActionProps & SearchBarAdvancedViewDataProps;
87 type SearchBarAdvancedViewFormProps = SearchBarAdvancedViewProps & SearchBarAdvancedViewFormDataProps
88 & InjectedFormProps & WithStyles<CssRules>;
90 const validate = (values: any) => {
91 const errors: any = {};
93 if (values.dateFrom && values.dateTo) {
94 if (new Date(values.dateFrom).getTime() > new Date(values.dateTo).getTime()) {
95 errors.dateFrom = 'Invalid date';
102 export const SearchBarAdvancedView = compose(
103 reduxForm<SearchBarAdvanceFormData, SearchBarAdvancedViewProps>({
104 form: SEARCH_BAR_ADVANCE_FORM_NAME,
106 onSubmit: (data: SearchBarAdvanceFormData, dispatch: Dispatch) => {
107 dispatch<any>(searchAdvanceData(data));
108 dispatch(reset(SEARCH_BAR_ADVANCE_FORM_NAME));
109 dispatch(treePickerActions.DEACTIVATE_TREE_PICKER_NODE({ pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID }));
111 onChange: (data: SearchBarAdvanceFormData, dispatch: Dispatch, props: any, prevData: SearchBarAdvanceFormData) => {
112 dispatch<any>(setSearchValueFromAdvancedData(data, prevData));
116 ({ classes, closeAdvanceView, handleSubmit, submitting, invalid, pristine, tags, saveQuery }: SearchBarAdvancedViewFormProps) =>
117 <Paper className={classes.searchView}>
118 <form onSubmit={handleSubmit}>
119 <Grid container direction="column" justify="flex-start" alignItems="flex-start">
120 <Grid item xs={12} container className={classes.container}>
121 <Grid item container xs={12} className={classes.selectGrid}>
122 <Grid item xs={2} className={classes.label}>Type</Grid>
124 <SearchBarTypeField />
127 <Grid item container xs={12} className={classes.selectGrid}>
128 <Grid item xs={2} className={classes.label}>Cluster</Grid>
130 <SearchBarClusterField />
133 <Grid item container xs={12}>
134 <Grid item xs={2} className={classes.label}>Project</Grid>
136 <SearchBarProjectField />
139 <Grid item container xs={12}>
140 <Grid item xs={2} className={classes.label} />
142 <SearchBarTrashField />
145 <IconButton onClick={closeAdvanceView} className={classes.closeIcon}>
149 <Grid container item xs={12} className={classes.container} spacing={16}>
150 <Grid item xs={2} className={classes.label}>Date modified</Grid>
152 <SearchBarDateFromField />
155 <SearchBarDateToField />
158 <Grid container item xs={12} className={classes.container}>
159 <SearchBarPropertiesField />
160 <Grid container item xs={12} justify="flex-start" alignItems="center" spacing={16}>
161 <Grid item xs={2} className={classes.label} />
163 <SearchBarSaveSearchField />
166 {saveQuery && <SearchBarQuerySearchField />}
169 <Grid container item xs={12} justify='flex-end'>
170 <div className={classes.buttonWrapper}>
171 <Button type="submit" className={classes.button}
172 // ToDo: create easier condition
173 // Question: do we need this condition?
174 // disabled={invalid || submitting || pristine || !!(tags && tags.values && ((tags.values.key) || (tags.values.value)) && !Object.keys(tags.values).find(el => el !== 'value' && el !== 'key'))}
180 {submitting && <CircularProgress size={20} className={classes.circularProgress} />}