Connect project explorer to the store
[arvados-workbench2.git] / src / views-components / project-explorer / project-explorer.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 { ProjectExplorerItem } from './project-explorer-item';
7 import { Grid, Typography } from '@material-ui/core';
8 import { formatDate, formatFileSize } from '../../common/formatters';
9 import DataExplorer from '../data-explorer/data-explorer';
10 import { DataColumn, toggleSortDirection, resetSortDirection } from '../../components/data-table/data-column';
11 import { DataTableFilterItem } from '../../components/data-table-filters/data-table-filters';
12 import { ContextMenuAction } from '../../components/context-menu/context-menu';
13 import { DispatchProp, connect } from 'react-redux';
14 import actions from "../../store/data-explorer/data-explorer-action";
15
16 export const PROJECT_EXPLORER_ID = "projectExplorer";
17 class ProjectExplorer extends React.Component<DispatchProp> {
18
19     contextMenuActions = [[{
20         icon: "fas fa-users fa-fw",
21         name: "Share"
22     }, {
23         icon: "fas fa-sign-out-alt fa-fw",
24         name: "Move to"
25     }, {
26         icon: "fas fa-star fa-fw",
27         name: "Add to favourite"
28     }, {
29         icon: "fas fa-edit fa-fw",
30         name: "Rename"
31     }, {
32         icon: "fas fa-copy fa-fw",
33         name: "Make a copy"
34     }, {
35         icon: "fas fa-download fa-fw",
36         name: "Download"
37     }], [{
38         icon: "fas fa-trash-alt fa-fw",
39         name: "Remove"
40     }
41     ]];
42
43     render() {
44         return <DataExplorer
45             id={PROJECT_EXPLORER_ID}
46             contextActions={this.contextMenuActions}
47             onColumnToggle={this.toggleColumn}
48             onFiltersChange={this.changeFilters}
49             onRowClick={console.log}
50             onSortToggle={this.toggleSort}
51             onSearch={this.search}
52             onContextAction={this.executeAction}
53             onChangePage={this.changePage}
54             onChangeRowsPerPage={this.changeRowsPerPage} />;
55     }
56
57     componentDidMount() {
58         this.props.dispatch(actions.SET_COLUMNS({ id: PROJECT_EXPLORER_ID, columns }));
59     }
60
61     toggleColumn = (toggledColumn: DataColumn<ProjectExplorerItem>) => {
62         this.props.dispatch(actions.TOGGLE_COLUMN({ id: PROJECT_EXPLORER_ID, columnName: toggledColumn.name }));
63     }
64
65     toggleSort = (toggledColumn: DataColumn<ProjectExplorerItem>) => {
66         this.props.dispatch(actions.TOGGLE_SORT({ id: PROJECT_EXPLORER_ID, columnName: toggledColumn.name }));
67     }
68
69     changeFilters = (filters: DataTableFilterItem[], updatedColumn: DataColumn<ProjectExplorerItem>) => {
70         this.props.dispatch(actions.SET_FILTERS({ id: PROJECT_EXPLORER_ID, columnName: updatedColumn.name, filters }));
71     }
72
73     executeAction = (action: ContextMenuAction, item: ProjectExplorerItem) => {
74         alert(`Executing ${action.name} on ${item.name}`);
75     }
76
77     search = (searchValue: string) => {
78         this.props.dispatch(actions.SET_SEARCH_VALUE({ id: PROJECT_EXPLORER_ID, searchValue }));
79     }
80
81     changePage = (page: number) => {
82         this.props.dispatch(actions.SET_PAGE({ id: PROJECT_EXPLORER_ID, page }));
83     }
84
85     changeRowsPerPage = (rowsPerPage: number) => {
86         this.props.dispatch(actions.SET_ROWS_PER_PAGE({ id: PROJECT_EXPLORER_ID, rowsPerPage }));
87     }
88 }
89
90 const renderName = (item: ProjectExplorerItem) =>
91     <Grid
92         container
93         alignItems="center"
94         wrap="nowrap"
95         spacing={16}>
96         <Grid item>
97             {renderIcon(item)}
98         </Grid>
99         <Grid item>
100             <Typography color="primary">
101                 {item.name}
102             </Typography>
103         </Grid>
104     </Grid>;
105
106 const renderIcon = (item: ProjectExplorerItem) => {
107     switch (item.type) {
108         case "arvados#group":
109             return <i className="fas fa-folder fa-lg" />;
110         case "arvados#groupList":
111             return <i className="fas fa-th fa-lg" />;
112         default:
113             return <i />;
114     }
115 };
116
117 const renderDate = (date: string) =>
118     <Typography noWrap>
119         {formatDate(date)}
120     </Typography>;
121
122 const renderFileSize = (fileSize?: number) =>
123     <Typography noWrap>
124         {formatFileSize(fileSize)}
125     </Typography>;
126
127 const renderOwner = (owner: string) =>
128     <Typography noWrap color="primary">
129         {owner}
130     </Typography>;
131
132 const renderType = (type: string) =>
133     <Typography noWrap>
134         {type}
135     </Typography>;
136
137 const renderStatus = (item: ProjectExplorerItem) =>
138     <Typography noWrap align="center">
139         {item.status || "-"}
140     </Typography>;
141
142 const columns: Array<DataColumn<ProjectExplorerItem>> = [{
143     name: "Name",
144     selected: true,
145     sortDirection: "asc",
146     render: renderName
147 }, {
148     name: "Status",
149     selected: true,
150     filters: [{
151         name: "In progress",
152         selected: true
153     }, {
154         name: "Complete",
155         selected: true
156     }],
157     render: renderStatus
158 }, {
159     name: "Type",
160     selected: true,
161     filters: [{
162         name: "Collection",
163         selected: true
164     }, {
165         name: "Group",
166         selected: true
167     }],
168     render: item => renderType(item.type)
169 }, {
170     name: "Owner",
171     selected: true,
172     render: item => renderOwner(item.owner)
173 }, {
174     name: "File size",
175     selected: true,
176     sortDirection: "none",
177     render: item => renderFileSize(item.fileSize)
178 }, {
179     name: "Last modified",
180     selected: true,
181     render: item => renderDate(item.lastModified)
182 }];
183
184 export default connect()(ProjectExplorer);