Merge branch 'main' into 15397-remove-obsolete-apis
[arvados.git] / services / workbench2 / src / views-components / search-bar / search-bar-advanced-view.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import 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';
9 import {
10     SEARCH_BAR_ADVANCED_FORM_NAME, SEARCH_BAR_ADVANCED_FORM_PICKER_ID,
11     searchAdvancedData,
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 { SearchBarAdvancedFormData } from 'models/search-bar';
17 import {
18     SearchBarTypeField, SearchBarClusterField, SearchBarProjectField, SearchBarTrashField,
19     SearchBarDateFromField, SearchBarDateToField, SearchBarPropertiesField,
20     SearchBarSaveSearchField, SearchBarQuerySearchField, SearchBarPastVersionsField
21 } from 'views-components/form-fields/search-bar-form-fields';
22 import { treePickerActions } from "store/tree-picker/tree-picker-actions";
23
24 type CssRules = 'container' | 'closeIcon' | 'label' | 'buttonWrapper'
25     | 'button' | 'circularProgress' | 'searchView' | 'selectGrid';
26
27 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
28     container: {
29         padding: theme.spacing.unit * 2,
30         borderBottom: `1px solid ${theme.palette.grey["200"]}`,
31         position: 'relative',
32     },
33     closeIcon: {
34         position: 'absolute',
35         top: '12px',
36         right: '12px'
37     },
38     label: {
39         color: theme.palette.grey["500"],
40         fontSize: '0.8125rem',
41         alignSelf: 'center'
42     },
43     buttonWrapper: {
44         marginRight: '14px',
45         marginTop: '14px',
46         position: 'relative',
47     },
48     button: {
49         boxShadow: 'none'
50     },
51     circularProgress: {
52         position: 'absolute',
53         top: 0,
54         bottom: 0,
55         left: 0,
56         right: 0,
57         margin: 'auto'
58     },
59     searchView: {
60         color: theme.palette.common.black,
61         borderRadius: `0 0 ${theme.spacing.unit / 2}px ${theme.spacing.unit / 2}px`
62     },
63     selectGrid: {
64         marginBottom: theme.spacing.unit * 2
65     }
66 });
67
68 // ToDo: maybe we should remove invalid and prostine
69 interface SearchBarAdvancedViewFormDataProps {
70     submitting: boolean;
71     invalid: boolean;
72     pristine: boolean;
73 }
74
75 // ToDo: maybe we should remove tags
76 export interface SearchBarAdvancedViewDataProps {
77     tags: any;
78     saveQuery: boolean;
79 }
80
81 export interface SearchBarAdvancedViewActionProps {
82     closeAdvanceView: () => void;
83 }
84
85 type SearchBarAdvancedViewProps = SearchBarAdvancedViewActionProps & SearchBarAdvancedViewDataProps;
86
87 type SearchBarAdvancedViewFormProps = SearchBarAdvancedViewProps & SearchBarAdvancedViewFormDataProps
88     & InjectedFormProps & WithStyles<CssRules>;
89
90 const validate = (values: any) => {
91     const errors: any = {};
92
93     if (values.dateFrom && values.dateTo) {
94         if (new Date(values.dateFrom).getTime() > new Date(values.dateTo).getTime()) {
95             errors.dateFrom = 'Invalid date';
96         }
97     }
98
99     return errors;
100 };
101
102 export const SearchBarAdvancedView = compose(
103     reduxForm<SearchBarAdvancedFormData, SearchBarAdvancedViewProps>({
104         form: SEARCH_BAR_ADVANCED_FORM_NAME,
105         validate,
106         onSubmit: (data: SearchBarAdvancedFormData, dispatch: Dispatch) => {
107             dispatch<any>(searchAdvancedData(data));
108             dispatch(reset(SEARCH_BAR_ADVANCED_FORM_NAME));
109             dispatch(treePickerActions.DEACTIVATE_TREE_PICKER_NODE({ pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID }));
110         },
111         onChange: (data: SearchBarAdvancedFormData, dispatch: Dispatch, props: any, prevData: SearchBarAdvancedFormData) => {
112             dispatch<any>(setSearchValueFromAdvancedData(data, prevData));
113         },
114     }),
115     withStyles(styles))(
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>
123                                 <Grid item xs={5}>
124                                     <SearchBarTypeField />
125                                 </Grid>
126                             </Grid>
127                             <Grid item container xs={12} className={classes.selectGrid}>
128                                 <Grid item xs={2} className={classes.label}>Cluster</Grid>
129                                 <Grid item xs={5}>
130                                     <SearchBarClusterField />
131                                 </Grid>
132                             </Grid>
133                             <Grid item container xs={12}>
134                                 <Grid item xs={2} className={classes.label}>Project</Grid>
135                                 <Grid item xs={10}>
136                                     <SearchBarProjectField />
137                                 </Grid>
138                             </Grid>
139                             <Grid item container xs={12}>
140                                 <Grid item xs={2} className={classes.label} />
141                                 <Grid item xs={5}>
142                                     <SearchBarTrashField />
143                                 </Grid>
144                                 <Grid item xs={5}>
145                                     <SearchBarPastVersionsField />
146                                 </Grid>
147                             </Grid>
148                             <IconButton onClick={closeAdvanceView} className={classes.closeIcon}>
149                                 <CloseIcon />
150                             </IconButton>
151                         </Grid>
152                         <Grid container item xs={12} className={classes.container} spacing={16}>
153                             <Grid item xs={2} className={classes.label}>Date modified</Grid>
154                             <Grid item xs={4}>
155                                 <SearchBarDateFromField />
156                             </Grid>
157                             <Grid item xs={4}>
158                                 <SearchBarDateToField />
159                             </Grid>
160                         </Grid>
161                         <Grid container item xs={12} className={classes.container}>
162                             <SearchBarPropertiesField />
163                             <Grid container item xs={12} justify="flex-start" alignItems="center" spacing={16}>
164                                 <Grid item xs={2} className={classes.label} />
165                                 <Grid item xs={4}>
166                                     <SearchBarSaveSearchField />
167                                 </Grid>
168                                 <Grid item xs={4}>
169                                     {saveQuery && <SearchBarQuerySearchField />}
170                                 </Grid>
171                             </Grid>
172                             <Grid container item xs={12} justify='flex-end'>
173                                 <div className={classes.buttonWrapper}>
174                                     <Button type="submit" className={classes.button}
175                                         // ToDo: create easier condition
176                                         // Question: do we need this condition?
177                                         // disabled={invalid || submitting || pristine || !!(tags && tags.values && ((tags.values.key) || (tags.values.value)) && !Object.keys(tags.values).find(el => el !== 'value' && el !== 'key'))}
178                                         color="primary"
179                                         size='small'
180                                         variant="contained">
181                                         Search
182                                     </Button>
183                                     {submitting && <CircularProgress size={20} className={classes.circularProgress} />}
184                                 </div>
185                             </Grid>
186                         </Grid>
187                     </Grid>
188                 </form>
189             </Paper>
190     );