|
1 | 1 | // Libraries
|
2 |
| -import React, {useState, useEffect} from 'react'; |
| 2 | +import React from 'react'; |
3 | 3 | import {useParams} from 'react-router-dom';
|
4 |
| -import {observer} from 'mobx-react-lite'; |
5 |
| -import {toJS} from 'mobx'; |
6 | 4 |
|
7 | 5 | // Partial components
|
8 |
| -import ProjectsFilter from './projectViews/projectsFilter'; |
9 |
| -import ProjectsFieldsSelector from './projectViews/projectsFieldsSelector'; |
10 |
| -import ViewSwitcher from './projectViews/viewSwitcher'; |
11 |
| -import ProjectsTable from 'js/projects/projectsTable/projectsTable'; |
12 |
| -import Button from 'js/components/common/button'; |
13 |
| -import ProjectQuickActionsEmpty from './projectsTable/projectQuickActionsEmpty'; |
14 |
| -import ProjectQuickActions from './projectsTable/projectQuickActions'; |
15 |
| -import LimitNotifications from 'js/components/usageLimits/limitNotifications.component'; |
16 |
| -import ProjectBulkActions from './projectsTable/projectBulkActions'; |
17 |
| - |
18 |
| -// Stores, hooks and utilities |
19 |
| -import {notify} from 'js/utils'; |
20 |
| -import {handleApiFail, fetchPostUrl} from 'js/api'; |
21 |
| -import customViewStore from './customViewStore'; |
22 |
| -import projectViewsStore from './projectViews/projectViewsStore'; |
| 6 | +import UniversalProjectsRoute from './universalProjectsRoute'; |
23 | 7 |
|
24 | 8 | // Constants and types
|
25 |
| -import type { |
26 |
| - ProjectsFilterDefinition, |
27 |
| - ProjectFieldName, |
28 |
| -} from './projectViews/constants'; |
29 | 9 | import {
|
30 | 10 | DEFAULT_VISIBLE_FIELDS,
|
31 | 11 | DEFAULT_ORDERABLE_FIELDS,
|
| 12 | + DEFAULT_EXCLUDED_FIELDS, |
32 | 13 | } from './projectViews/constants';
|
33 | 14 | import {ROOT_URL} from 'js/constants';
|
34 | 15 |
|
35 |
| -// Styles |
36 |
| -import styles from './projectViews.module.scss'; |
37 |
| - |
38 | 16 | /**
|
39 | 17 | * Component responsible for rendering a custom project view route (`#/projects/<vid>`).
|
40 | 18 | */
|
41 |
| -function CustomViewRoute() { |
| 19 | +export default function CustomViewRoute() { |
42 | 20 | const {viewUid} = useParams();
|
43 | 21 |
|
| 22 | + // This condition is here to satisfy TS, as without it the code below would |
| 23 | + // need to be unnecessarily more lengthy. |
44 | 24 | if (viewUid === undefined) {
|
45 | 25 | return null;
|
46 | 26 | }
|
47 | 27 |
|
48 |
| - const [projectViews] = useState(projectViewsStore); |
49 |
| - const [customView] = useState(customViewStore); |
50 |
| - const [selectedRows, setSelectedRows] = useState<string[]>([]); |
51 |
| - |
52 |
| - useEffect(() => { |
53 |
| - customView.setUp( |
54 |
| - viewUid, |
55 |
| - `${ROOT_URL}/api/v2/project-views/${viewUid}/assets/`, |
56 |
| - DEFAULT_VISIBLE_FIELDS, |
57 |
| - false |
58 |
| - ); |
59 |
| - }, [viewUid]); |
60 |
| - |
61 |
| - // Whenever we do a full page (of results) reload, we need to clear up |
62 |
| - // `selectedRows` to not end up with a project selected (e.g. on page of |
63 |
| - // results that wasn't loaded/scrolled down into yet) and user not knowing |
64 |
| - // about it. |
65 |
| - useEffect(() => { |
66 |
| - setSelectedRows([]); |
67 |
| - }, [customView.isFirstLoadComplete]); |
68 |
| - |
69 |
| - /** Returns a list of names for fields that have at least 1 filter defined. */ |
70 |
| - const getFilteredFieldsNames = () => { |
71 |
| - const outcome: ProjectFieldName[] = []; |
72 |
| - customView.filters.forEach((item: ProjectsFilterDefinition) => { |
73 |
| - if (item.fieldName !== undefined) { |
74 |
| - outcome.push(item.fieldName); |
75 |
| - } |
76 |
| - }); |
77 |
| - return outcome; |
78 |
| - }; |
79 |
| - |
80 |
| - const exportAllData = () => { |
81 |
| - const foundView = projectViews.getView(viewUid); |
82 |
| - if (foundView) { |
83 |
| - fetchPostUrl(foundView.assets_export, {uid: viewUid}).then(() => { |
84 |
| - notify.warning( |
85 |
| - t( |
86 |
| - "Export is being generated, you will receive an email when it's done" |
87 |
| - ) |
88 |
| - ); |
89 |
| - }, handleApiFail); |
90 |
| - } else { |
91 |
| - notify.error( |
92 |
| - t( |
93 |
| - "We couldn't create the export, please try again later or contact support" |
94 |
| - ) |
95 |
| - ); |
96 |
| - } |
97 |
| - }; |
98 |
| - |
99 |
| - const selectedAssets = customView.assets.filter((asset) => |
100 |
| - selectedRows.includes(asset.uid) |
101 |
| - ); |
102 |
| - |
103 | 28 | return (
|
104 |
| - <section className={styles.root}> |
105 |
| - <header className={styles.header}> |
106 |
| - <ViewSwitcher selectedViewUid={viewUid} /> |
107 |
| - |
108 |
| - <ProjectsFilter |
109 |
| - onFiltersChange={customView.setFilters.bind(customView)} |
110 |
| - filters={toJS(customView.filters)} |
111 |
| - /> |
112 |
| - |
113 |
| - <ProjectsFieldsSelector |
114 |
| - onFieldsChange={customView.setFields.bind(customView)} |
115 |
| - selectedFields={toJS(customView.fields)} |
116 |
| - /> |
117 |
| - |
118 |
| - <Button |
119 |
| - type='secondary' |
120 |
| - size='s' |
121 |
| - startIcon='download' |
122 |
| - label={t('Export all data')} |
123 |
| - onClick={exportAllData} |
124 |
| - /> |
125 |
| - |
126 |
| - {selectedAssets.length === 0 && ( |
127 |
| - <div className={styles.actions}> |
128 |
| - <ProjectQuickActionsEmpty /> |
129 |
| - </div> |
130 |
| - )} |
131 |
| - |
132 |
| - {selectedAssets.length === 1 && ( |
133 |
| - <div className={styles.actions}> |
134 |
| - <ProjectQuickActions |
135 |
| - asset={selectedAssets[0]} |
136 |
| - /> |
137 |
| - </div> |
138 |
| - )} |
139 |
| - |
140 |
| - {selectedAssets.length > 1 && ( |
141 |
| - <div className={styles.actions}> |
142 |
| - <ProjectBulkActions assets={selectedAssets} /> |
143 |
| - </div> |
144 |
| - )} |
145 |
| - </header> |
146 |
| - |
147 |
| - <LimitNotifications useModal /> |
148 |
| - |
149 |
| - <ProjectsTable |
150 |
| - assets={customView.assets} |
151 |
| - isLoading={!customView.isFirstLoadComplete} |
152 |
| - highlightedFields={getFilteredFieldsNames()} |
153 |
| - visibleFields={ |
154 |
| - toJS(customView.fields) || customView.defaultVisibleFields |
155 |
| - } |
156 |
| - orderableFields={DEFAULT_ORDERABLE_FIELDS} |
157 |
| - order={customView.order} |
158 |
| - onChangeOrderRequested={customView.setOrder.bind(customView)} |
159 |
| - onHideFieldRequested={customView.hideField.bind(customView)} |
160 |
| - onRequestLoadNextPage={customView.fetchMoreAssets.bind(customView)} |
161 |
| - hasMorePages={customView.hasMoreAssets} |
162 |
| - selectedRows={selectedRows} |
163 |
| - onRowsSelected={setSelectedRows} |
164 |
| - /> |
165 |
| - </section> |
| 29 | + <UniversalProjectsRoute |
| 30 | + viewUid={viewUid} |
| 31 | + baseUrl={`${ROOT_URL}/api/v2/project-views/${viewUid}/assets/`} |
| 32 | + defaultVisibleFields={DEFAULT_VISIBLE_FIELDS} |
| 33 | + includeTypeFilter={false} |
| 34 | + defaultOrderableFields={DEFAULT_ORDERABLE_FIELDS} |
| 35 | + defaultExcludedFields={DEFAULT_EXCLUDED_FIELDS} |
| 36 | + isExportButtonVisible |
| 37 | + /> |
166 | 38 | );
|
167 | 39 | }
|
168 |
| - |
169 |
| -export default observer(CustomViewRoute); |
0 commit comments