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