Merge branch '14652_api_tokens_pagination'
[arvados-workbench2.git] / 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 * 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';
9 import {
10     SEARCH_BAR_ADVANCE_FORM_NAME, SEARCH_BAR_ADVANCE_FORM_PICKER_ID,
11     searchAdvanceData,
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';
17 import {
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";
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<SearchBarAdvanceFormData, SearchBarAdvancedViewProps>({
104         form: SEARCH_BAR_ADVANCE_FORM_NAME,
105         validate,
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 }));
110         },
111         onChange: (data: SearchBarAdvanceFormData, dispatch: Dispatch, props: any, prevData: SearchBarAdvanceFormData) => {
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>
145                             <IconButton onClick={closeAdvanceView} className={classes.closeIcon}>
146                                 <CloseIcon />
147                             </IconButton>
148                         </Grid>
149                         <Grid container item xs={12} className={classes.container} spacing={16}>
150                             <Grid item xs={2} className={classes.label}>Date modified</Grid>
151                             <Grid item xs={4}>
152                                 <SearchBarDateFromField />
153                             </Grid>
154                             <Grid item xs={4}>
155                                 <SearchBarDateToField />
156                             </Grid>
157                         </Grid>
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} />
162                                 <Grid item xs={4}>
163                                     <SearchBarSaveSearchField />
164                                 </Grid>
165                                 <Grid item xs={4}>
166                                     {saveQuery && <SearchBarQuerySearchField />}
167                                 </Grid>
168                             </Grid>
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'))}
175                                         color="primary"
176                                         size='small'
177                                         variant="contained">
178                                         Search
179                                     </Button>
180                                     {submitting && <CircularProgress size={20} className={classes.circularProgress} />}
181                                 </div>
182                             </Grid>
183                         </Grid>
184                     </Grid>
185                 </form>
186             </Paper>
187     );