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