Merge branch 'master' into 14254-triggering-vie-details-from-more-options-does-not...
[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 }
73
74 type SearchBarAdvancedViewProps = SearchBarAdvancedViewActionProps & SearchBarAdvancedViewDataProps 
75     & InjectedFormProps & WithStyles<CssRules>;
76
77 const validate = (values: any) => {
78     const errors: any = {};
79
80     if (values.dateFrom && values.dateTo ) {
81         if (new Date(values.dateFrom).getTime() > new Date(values.dateTo).getTime()) {
82             errors.dateFrom = 'Invalid date';
83         }
84     } 
85
86     return errors;
87 };
88
89 export const SearchBarAdvancedView = compose(
90     reduxForm<SearchBarAdvanceFormData, SearchBarAdvancedViewActionProps>({
91         form: SEARCH_BAR_ADVANCE_FORM_NAME,
92         validate,
93         onSubmit: (data: SearchBarAdvanceFormData, dispatch: Dispatch) => {
94             dispatch<any>(saveQuery(data));
95             dispatch(reset(SEARCH_BAR_ADVANCE_FORM_NAME));
96         }
97     }),
98     withStyles(styles))(
99         ({ classes, setView, handleSubmit, submitting, invalid, pristine }: SearchBarAdvancedViewProps) =>
100             <Paper className={classes.searchView}>
101                 <form onSubmit={handleSubmit}>
102                     <Grid container direction="column" justify="flex-start" alignItems="flex-start">
103                         <Grid item xs={12} container className={classes.container}>
104                             <Grid item container xs={12} className={classes.selectGrid}>
105                                 <Grid item xs={2} className={classes.label}>Type</Grid>
106                                 <Grid item xs={5}>
107                                     <SearchBarTypeField />
108                                 </Grid>
109                             </Grid>
110                             <Grid item container xs={12} className={classes.selectGrid}>
111                                 <Grid item xs={2} className={classes.label}>Cluster</Grid>
112                                 <Grid item xs={5}>
113                                     <SearchBarClusterField />
114                                 </Grid>
115                             </Grid>
116                             <Grid item container xs={12}>
117                                 <Grid item xs={2} className={classes.label}>Project</Grid>
118                                 <Grid item xs={5}>
119                                     <SearchBarProjectField />
120                                 </Grid>
121                             </Grid>
122                             <Grid item container xs={12}>
123                                 <Grid item xs={2} className={classes.label} />
124                                 <Grid item xs={5}>
125                                     <SearchBarTrashField />
126                                 </Grid>
127                             </Grid>
128                             <IconButton onClick={() => setView(SearchView.BASIC)} className={classes.closeIcon}>
129                                 <CloseIcon />
130                             </IconButton>
131                         </Grid>
132                         <Grid container item xs={12} className={classes.container} spacing={16}>
133                             <Grid item xs={2} className={classes.label}>Date modified</Grid>
134                             <Grid item xs={4}>
135                                 <SearchBarDateFromField />
136                             </Grid>
137                             <Grid item xs={4}>
138                                 <SearchBarDateToField />
139                             </Grid>
140                         </Grid>
141                         <Grid container item xs={12} className={classes.container}>
142                             <SearchBarPropertiesField />
143                             <Grid container item xs={12} justify="flex-start" alignItems="center" spacing={16}>
144                                 <Grid item xs={2} className={classes.label} />
145                                 <Grid item xs={4}>
146                                     <SearchBarSaveSearchField />
147                                 </Grid>
148                                 <Grid item xs={4}>
149                                     <SearchBarQuerySearchField />
150                                 </Grid>
151                             </Grid>
152                             <Grid container item xs={12} justify='flex-end'>
153                                 <div className={classes.buttonWrapper}>
154                                     <Button type="submit" className={classes.button}
155                                         disabled={invalid || submitting || pristine}
156                                         color="primary"
157                                         size='small'
158                                         variant="contained">
159                                         Search
160                                     </Button>
161                                     {submitting && <CircularProgress size={20} className={classes.circularProgress} />}
162                                 </div>
163                             </Grid>
164                         </Grid>
165                     </Grid>
166                 </form>
167         </Paper>
168     );