Merge branch '14316-improve-collection-copy-with-link-to'
[arvados.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, 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';
14 import {
15     SearchBarTypeField, SearchBarClusterField, SearchBarProjectField, SearchBarTrashField,
16     SearchBarDateFromField, SearchBarDateToField, SearchBarPropertiesField,
17     SearchBarSaveSearchField, SearchBarQuerySearchField
18 } from '~/views-components/form-fields/search-bar-form-fields';
19
20 type CssRules = 'container' | 'closeIcon' | 'label' | 'buttonWrapper'
21     | 'button' | 'circularProgress' | 'searchView' | 'selectGrid';
22
23 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
24     container: {
25         padding: theme.spacing.unit * 2,
26         borderBottom: `1px solid ${theme.palette.grey["200"]}`
27     },
28     closeIcon: {
29         position: 'absolute',
30         top: '12px',
31         right: '12px'
32     },
33     label: {
34         color: theme.palette.grey["500"],
35         fontSize: '0.8125rem',
36         alignSelf: 'center'
37     },
38     buttonWrapper: {
39         paddingRight: '14px',
40         paddingTop: '14px',
41         position: 'relative',
42     },
43     button: {
44         boxShadow: 'none'
45     },
46     circularProgress: {
47         position: 'absolute',
48         top: -9,
49         bottom: 0,
50         left: 0,
51         right: 0,
52         margin: 'auto'
53     },
54     searchView: {
55         color: theme.palette.common.black,
56         borderRadius: `0 0 ${theme.spacing.unit / 2}px ${theme.spacing.unit / 2}px`
57     },
58     selectGrid: {
59         marginBottom: theme.spacing.unit * 2
60     }
61 });
62
63 interface SearchBarAdvancedViewDataProps {
64     submitting: boolean;
65     invalid: boolean;
66     pristine: boolean;
67 }
68
69 interface SearchBarAdvancedViewActionProps {
70     setView: (currentView: string) => void;
71     saveQuery: (data: SearchBarAdvanceFormData) => void;
72     tags: any;
73 }
74
75 type SearchBarAdvancedViewProps = SearchBarAdvancedViewActionProps & SearchBarAdvancedViewDataProps
76     & InjectedFormProps & WithStyles<CssRules>;
77
78 const validate = (values: any) => {
79     const errors: any = {};
80
81     if (values.dateFrom && values.dateTo) {
82         if (new Date(values.dateFrom).getTime() > new Date(values.dateTo).getTime()) {
83             errors.dateFrom = 'Invalid date';
84         }
85     }
86
87     return errors;
88 };
89
90 export const SearchBarAdvancedView = compose(
91     reduxForm<SearchBarAdvanceFormData, SearchBarAdvancedViewActionProps>({
92         form: SEARCH_BAR_ADVANCE_FORM_NAME,
93         validate,
94         onSubmit: (data: SearchBarAdvanceFormData, dispatch: Dispatch) => {
95             dispatch<any>(saveQuery(data));
96             dispatch(reset(SEARCH_BAR_ADVANCE_FORM_NAME));
97         }
98     }),
99     withStyles(styles))(
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>
107                                 <Grid item xs={5}>
108                                     <SearchBarTypeField />
109                                 </Grid>
110                             </Grid>
111                             <Grid item container xs={12} className={classes.selectGrid}>
112                                 <Grid item xs={2} className={classes.label}>Cluster</Grid>
113                                 <Grid item xs={5}>
114                                     <SearchBarClusterField />
115                                 </Grid>
116                             </Grid>
117                             <Grid item container xs={12}>
118                                 <Grid item xs={2} className={classes.label}>Project</Grid>
119                                 <Grid item xs={5}>
120                                     <SearchBarProjectField />
121                                 </Grid>
122                             </Grid>
123                             <Grid item container xs={12}>
124                                 <Grid item xs={2} className={classes.label} />
125                                 <Grid item xs={5}>
126                                     <SearchBarTrashField />
127                                 </Grid>
128                             </Grid>
129                             <IconButton onClick={() => setView(SearchView.BASIC)} className={classes.closeIcon}>
130                                 <CloseIcon />
131                             </IconButton>
132                         </Grid>
133                         <Grid container item xs={12} className={classes.container} spacing={16}>
134                             <Grid item xs={2} className={classes.label}>Date modified</Grid>
135                             <Grid item xs={4}>
136                                 <SearchBarDateFromField />
137                             </Grid>
138                             <Grid item xs={4}>
139                                 <SearchBarDateToField />
140                             </Grid>
141                         </Grid>
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} />
146                                 <Grid item xs={4}>
147                                     <SearchBarSaveSearchField />
148                                 </Grid>
149                                 <Grid item xs={4}>
150                                     <SearchBarQuerySearchField />
151                                 </Grid>
152                             </Grid>
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'))}
158                                         color="primary"
159                                         size='small'
160                                         variant="contained">
161                                         Search
162                                     </Button>
163                                     {submitting && <CircularProgress size={20} className={classes.circularProgress} />}
164                                 </div>
165                             </Grid>
166                         </Grid>
167                     </Grid>
168                 </form>
169             </Paper>
170     );