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