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"]}`
38 color: theme.palette.grey["500"],
39 fontSize: '0.8125rem',
59 color: theme.palette.common.black,
60 borderRadius: `0 0 ${theme.spacing.unit / 2}px ${theme.spacing.unit / 2}px`
63 marginBottom: theme.spacing.unit * 2
67 // ToDo: maybe we should remove invalid and prostine
68 interface SearchBarAdvancedViewFormDataProps {
74 // ToDo: maybe we should remove tags
75 export interface SearchBarAdvancedViewDataProps {
80 export interface SearchBarAdvancedViewActionProps {
81 closeAdvanceView: () => void;
84 type SearchBarAdvancedViewProps = SearchBarAdvancedViewActionProps & SearchBarAdvancedViewDataProps;
86 type SearchBarAdvancedViewFormProps = SearchBarAdvancedViewProps & SearchBarAdvancedViewFormDataProps
87 & InjectedFormProps & WithStyles<CssRules>;
89 const validate = (values: any) => {
90 const errors: any = {};
92 if (values.dateFrom && values.dateTo) {
93 if (new Date(values.dateFrom).getTime() > new Date(values.dateTo).getTime()) {
94 errors.dateFrom = 'Invalid date';
101 export const SearchBarAdvancedView = compose(
102 reduxForm<SearchBarAdvanceFormData, SearchBarAdvancedViewProps>({
103 form: SEARCH_BAR_ADVANCE_FORM_NAME,
105 onSubmit: (data: SearchBarAdvanceFormData, dispatch: Dispatch) => {
106 dispatch<any>(searchAdvanceData(data));
107 dispatch(reset(SEARCH_BAR_ADVANCE_FORM_NAME));
108 dispatch(treePickerActions.DEACTIVATE_TREE_PICKER_NODE({ pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID }));
110 onChange: (data: SearchBarAdvanceFormData, dispatch: Dispatch, props: any, prevData: SearchBarAdvanceFormData) => {
111 dispatch<any>(setSearchValueFromAdvancedData(data, prevData));
115 ({ classes, closeAdvanceView, handleSubmit, submitting, invalid, pristine, tags, saveQuery }: SearchBarAdvancedViewFormProps) =>
116 <Paper className={classes.searchView}>
117 <form onSubmit={handleSubmit}>
118 <Grid container direction="column" justify="flex-start" alignItems="flex-start">
119 <Grid item xs={12} container className={classes.container}>
120 <Grid item container xs={12} className={classes.selectGrid}>
121 <Grid item xs={2} className={classes.label}>Type</Grid>
123 <SearchBarTypeField />
126 <Grid item container xs={12} className={classes.selectGrid}>
127 <Grid item xs={2} className={classes.label}>Cluster</Grid>
129 <SearchBarClusterField />
132 <Grid item container xs={12}>
133 <Grid item xs={2} className={classes.label}>Project</Grid>
135 <SearchBarProjectField />
138 <Grid item container xs={12}>
139 <Grid item xs={2} className={classes.label} />
141 <SearchBarTrashField />
144 <IconButton onClick={closeAdvanceView} className={classes.closeIcon}>
148 <Grid container item xs={12} className={classes.container} spacing={16}>
149 <Grid item xs={2} className={classes.label}>Date modified</Grid>
151 <SearchBarDateFromField />
154 <SearchBarDateToField />
157 <Grid container item xs={12} className={classes.container}>
158 <SearchBarPropertiesField />
159 <Grid container item xs={12} justify="flex-start" alignItems="center" spacing={16}>
160 <Grid item xs={2} className={classes.label} />
162 <SearchBarSaveSearchField />
165 {saveQuery && <SearchBarQuerySearchField />}
168 <Grid container item xs={12} justify='flex-end'>
169 <div className={classes.buttonWrapper}>
170 <Button type="submit" className={classes.button}
171 // ToDo: create easier condition
172 // Question: do we need this condition?
173 // disabled={invalid || submitting || pristine || !!(tags && tags.values && ((tags.values.key) || (tags.values.value)) && !Object.keys(tags.values).find(el => el !== 'value' && el !== 'key'))}
179 {submitting && <CircularProgress size={20} className={classes.circularProgress} />}