-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathMain.tsx
More file actions
231 lines (207 loc) · 7.47 KB
/
Main.tsx
File metadata and controls
231 lines (207 loc) · 7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// SPDX-FileCopyrightText: © 2020 EteSync Authors
// SPDX-License-Identifier: AGPL-3.0-only
import * as React from "react";
import { Switch, Route, useHistory } from "react-router";
import * as Etebase from "etebase";
import { Button, useTheme } from "@material-ui/core";
import IconEdit from "@material-ui/icons/Edit";
import IconChangeHistory from "@material-ui/icons/ChangeHistory";
import { TaskType, PimType, PimChanges } from "../pim-types";
import { useCredentials } from "../credentials";
import { useItems, useCollections } from "../etebase-helpers";
import { routeResolver } from "../App";
import TaskList from "./TaskList";
import Task from "./Task";
import LoadingIndicator from "../widgets/LoadingIndicator";
import TaskEdit from "./TaskEdit";
import PageNotFound, { PageNotFoundRoute } from "../PageNotFound";
import { CachedCollection, getItemNavigationUid, getDecryptCollectionsFunction, getDecryptItemsFunction, PimFab, itemSave, itemDelete } from "../Pim/helpers";
import ItemChangeHistory from "../Pim/ItemChangeHistory";
const colType = "etebase.vtodo";
const decryptCollections = getDecryptCollectionsFunction(colType);
const decryptItems = getDecryptItemsFunction(colType, TaskType.parse);
export default function TasksMain() {
const [entries, setEntries] = React.useState<Map<string, Map<string, TaskType>>>();
const [cachedCollections, setCachedCollections] = React.useState<CachedCollection[]>();
const theme = useTheme();
const history = useHistory();
const etebase = useCredentials()!;
const collections = useCollections(etebase, colType);
const items = useItems(etebase, colType);
React.useEffect(() => {
if (!collections || !items) {
return;
}
(async () => {
const colEntries = await decryptCollections(collections);
const entries = await decryptItems(items);
setCachedCollections(colEntries);
setEntries(entries);
})();
}, [items, collections]);
if (!entries || !cachedCollections) {
return (
<LoadingIndicator />
);
}
async function onItemSave(item: PimType, collectionUid: string, originalItem?: PimType): Promise<void> {
await onMultipleItemsSave([{
original: originalItem,
new: item,
}], collectionUid);
}
async function onMultipleItemsSave(changes: PimChanges[], collectionUid: string): Promise<void> {
const collection = collections!.find((x) => x.uid === collectionUid)!;
await itemSave(etebase, collection, items!, collectionUid, changes);
}
function onCancel() {
history.goBack();
}
const flatEntries: TaskType[] = [];
for (const col of entries.values()) {
for (const item of col.values()) {
flatEntries.push(item);
}
}
async function onItemDelete(item: PimType, collectionUid: string, redirect = true, recursive = false) {
const collection = collections!.find((x) => x.uid === collectionUid)!;
if (recursive) {
let index = 0;
const deleteTarget = [item];
while (index < deleteTarget.length) {
const current = deleteTarget[index++];
const children = flatEntries.filter((i) => i.relatedTo === current.uid);
deleteTarget.push(...children);
}
await itemDelete(etebase, collection, items!, deleteTarget, collectionUid);
} else {
await itemDelete(etebase, collection, items!, [item], collectionUid);
}
if (redirect) {
history.push(routeResolver.getRoute("pim.tasks"));
}
}
const styles = {
button: {
marginLeft: theme.spacing(1),
},
leftIcon: {
marginRight: theme.spacing(1),
},
};
return (
<Switch>
<Route
path={routeResolver.getRoute("pim.tasks")}
exact
>
<TaskList
entries={flatEntries}
collections={cachedCollections}
onItemClick={(item: TaskType) => history.push(
routeResolver.getRoute("pim.tasks._id", { itemUid: getItemNavigationUid(item) })
)}
onItemSave={onItemSave}
/>
<PimFab
onClick={() => history.push(
routeResolver.getRoute("pim.tasks.new")
)}
/>
</Route>
<Route
path={routeResolver.getRoute("pim.tasks.new")}
exact
>
<TaskEdit
directChildren={[]}
entries={flatEntries}
collections={cachedCollections}
onSave={onMultipleItemsSave}
onDelete={onItemDelete}
onCancel={onCancel}
history={history}
/>
</Route>
<Route
path={routeResolver.getRoute("pim.tasks._id.log")}
render={({ match }) => {
// We have this path outside because we don't want the item existing check
const [colUid, itemUid] = match.params.itemUid.split("|");
const cachedCollection = cachedCollections!.find((x) => x.collection.uid === colUid)!;
if (!cachedCollection) {
return (<PageNotFound />);
}
return (
<ItemChangeHistory collection={cachedCollection} itemUid={itemUid} />
);
}}
/>
<Route
path={routeResolver.getRoute("pim.tasks._id")}
render={({ match }) => {
const [colUid, itemUid] = match.params.itemUid.split("|");
const item = entries.get(colUid)?.get(itemUid);
if (!item) {
return (<PageNotFound />);
}
const collection = collections!.find((x) => x.uid === colUid)!;
const readOnly = collection.accessLevel === Etebase.CollectionAccessLevel.ReadOnly;
return (
<Switch>
<Route
path={routeResolver.getRoute("pim.tasks._id.edit")}
exact
>
<TaskEdit
directChildren={flatEntries.filter((t) => t.relatedTo === item.uid)}
entries={flatEntries}
key={itemUid}
initialCollection={item.collectionUid}
item={item}
collections={cachedCollections}
onSave={onMultipleItemsSave}
onDelete={onItemDelete}
onCancel={onCancel}
history={history}
/>
</Route>
<Route
path={routeResolver.getRoute("pim.tasks._id")}
exact
>
<div style={{ textAlign: "right", marginBottom: 15 }}>
<Button
variant="contained"
style={styles.button}
onClick={() =>
history.push(routeResolver.getRoute("pim.tasks._id.log", { itemUid: getItemNavigationUid(item) }))
}
>
<IconChangeHistory style={styles.leftIcon} />
Change History
</Button>
<Button
color="secondary"
variant="contained"
disabled={readOnly}
style={{ ...styles.button, marginLeft: 15 }}
onClick={() =>
history.push(routeResolver.getRoute("pim.tasks._id.edit", { itemUid: getItemNavigationUid(item) }))
}
>
<IconEdit style={styles.leftIcon} />
Edit
</Button>
</div>
<Task item={item} />
</Route>
<PageNotFoundRoute />
</Switch>
);
}}
/>
<PageNotFoundRoute />
</Switch>
);
}