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, reset, InjectedFormProps } from 'redux-form';
7 import { compose, Dispatch } from 'redux';
8 import { Paper, StyleRulesCallback, withStyles, WithStyles, Button, Grid, IconButton, CircularProgress } from '@material-ui/core';
9 import { SearchView } from '~/store/search-bar/search-bar-reducer';
10 import { SEARCH_BAR_ADVANCE_FORM_NAME, saveQuery } from '~/store/search-bar/search-bar-actions';
11 import { ArvadosTheme } from '~/common/custom-theme';
12 import { CloseIcon } from '~/components/icon/icon';
13 import { SearchBarAdvanceFormData } from '~/models/search-bar';
15 SearchBarTypeField, SearchBarClusterField, SearchBarProjectField, SearchBarTrashField,
16 SearchBarDateFromField, SearchBarDateToField, SearchBarPropertiesField,
17 SearchBarSaveSearchField, SearchBarQuerySearchField
18 } from '~/views-components/form-fields/search-bar-form-fields';
20 type CssRules = 'container' | 'closeIcon' | 'label' | 'buttonWrapper'
21 | 'button' | 'circularProgress' | 'searchView' | 'selectGrid';
23 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
25 padding: theme.spacing.unit * 2,
26 borderBottom: `1px solid ${theme.palette.grey["200"]}`
34 color: theme.palette.grey["500"],
35 fontSize: '0.8125rem',
55 color: theme.palette.common.black,
56 borderRadius: `0 0 ${theme.spacing.unit / 2}px ${theme.spacing.unit / 2}px`
59 marginBottom: theme.spacing.unit * 2
63 interface SearchBarAdvancedViewDataProps {
69 interface SearchBarAdvancedViewActionProps {
70 setView: (currentView: string) => void;
71 saveQuery: (data: SearchBarAdvanceFormData) => void;
75 type SearchBarAdvancedViewProps = SearchBarAdvancedViewActionProps & SearchBarAdvancedViewDataProps
76 & InjectedFormProps & WithStyles<CssRules>;
78 const validate = (values: any) => {
79 const errors: any = {};
81 if (values.dateFrom && values.dateTo) {
82 if (new Date(values.dateFrom).getTime() > new Date(values.dateTo).getTime()) {
83 errors.dateFrom = 'Invalid date';
90 export const SearchBarAdvancedView = compose(
91 reduxForm<SearchBarAdvanceFormData, SearchBarAdvancedViewActionProps>({
92 form: SEARCH_BAR_ADVANCE_FORM_NAME,
94 onSubmit: (data: SearchBarAdvanceFormData, dispatch: Dispatch) => {
95 dispatch<any>(saveQuery(data));
96 dispatch(reset(SEARCH_BAR_ADVANCE_FORM_NAME));
100 ({ classes, setView, handleSubmit, submitting, invalid, pristine, tags }: SearchBarAdvancedViewProps) =>
101 <Paper className={classes.searchView}>
102 <form onSubmit={handleSubmit}>
103 <Grid container direction="column" justify="flex-start" alignItems="flex-start">
104 <Grid item xs={12} container className={classes.container}>
105 <Grid item container xs={12} className={classes.selectGrid}>
106 <Grid item xs={2} className={classes.label}>Type</Grid>
108 <SearchBarTypeField />
111 <Grid item container xs={12} className={classes.selectGrid}>
112 <Grid item xs={2} className={classes.label}>Cluster</Grid>
114 <SearchBarClusterField />
117 <Grid item container xs={12}>
118 <Grid item xs={2} className={classes.label}>Project</Grid>
120 <SearchBarProjectField />
123 <Grid item container xs={12}>
124 <Grid item xs={2} className={classes.label} />
126 <SearchBarTrashField />
129 <IconButton onClick={() => setView(SearchView.BASIC)} className={classes.closeIcon}>
133 <Grid container item xs={12} className={classes.container} spacing={16}>
134 <Grid item xs={2} className={classes.label}>Date modified</Grid>
136 <SearchBarDateFromField />
139 <SearchBarDateToField />
142 <Grid container item xs={12} className={classes.container}>
143 <SearchBarPropertiesField />
144 <Grid container item xs={12} justify="flex-start" alignItems="center" spacing={16}>
145 <Grid item xs={2} className={classes.label} />
147 <SearchBarSaveSearchField />
150 <SearchBarQuerySearchField />
153 <Grid container item xs={12} justify='flex-end'>
154 <div className={classes.buttonWrapper}>
155 <Button type="submit" className={classes.button}
156 // ToDo: create easier condition
157 disabled={invalid || submitting || pristine || !!(tags && tags.values && ((tags.values.key) || (tags.values.value)) && !Object.keys(tags.values).find(el => el !== 'value' && el !== 'key'))}
163 {submitting && <CircularProgress size={20} className={classes.circularProgress} />}