Implement contacts editing.
parent
2edc95cce7
commit
b796217cd1
@ -0,0 +1,225 @@
|
||||
// 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 { ContactType, PimType } from "../pim-types";
|
||||
import { useCredentials } from "../credentials";
|
||||
import { useItems, useCollections, getCollectionManager } from "../etebase-helpers";
|
||||
import { routeResolver } from "../App";
|
||||
import SearchableAddressBook from "../components/SearchableAddressBook";
|
||||
import Contact from "../components/Contact";
|
||||
import LoadingIndicator from "../widgets/LoadingIndicator";
|
||||
import ContactEdit from "../components/ContactEdit";
|
||||
import PageNotFound from "../PageNotFound";
|
||||
|
||||
import { CachedCollection, getItemNavigationUid, getDecryptCollectionsFunction, getDecryptItemsFunction } from "../Pim/helpers";
|
||||
|
||||
const colType = "etebase.vcard";
|
||||
|
||||
const decryptCollections = getDecryptCollectionsFunction(colType);
|
||||
const decryptItems = getDecryptItemsFunction(colType, ContactType.parse);
|
||||
|
||||
export default function ContactsMain() {
|
||||
const [entries, setEntries] = React.useState<Map<string, Map<string, ContactType>>>();
|
||||
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 (items) {
|
||||
decryptItems(items)
|
||||
.then((entries) => setEntries(entries));
|
||||
// FIXME: handle failure to decrypt items
|
||||
}
|
||||
if (collections) {
|
||||
decryptCollections(collections)
|
||||
.then((entries) => setCachedCollections(entries));
|
||||
// FIXME: handle failure to decrypt collections
|
||||
}
|
||||
}, [items, collections]);
|
||||
|
||||
if (!entries || !cachedCollections) {
|
||||
return (
|
||||
<LoadingIndicator />
|
||||
);
|
||||
}
|
||||
|
||||
async function onItemSave(item: PimType, collectionUid: string, originalItem?: PimType): Promise<void> {
|
||||
const itemUid = originalItem?.itemUid;
|
||||
const colMgr = getCollectionManager(etebase);
|
||||
const collection = collections!.find((x) => x.uid === collectionUid)!;
|
||||
const itemMgr = colMgr.getItemManager(collection);
|
||||
|
||||
const mtime = (new Date()).getUTCMilliseconds();
|
||||
const content = item.toIcal();
|
||||
|
||||
let eteItem;
|
||||
if (itemUid) {
|
||||
// Existing item
|
||||
eteItem = items!.get(collectionUid)?.get(itemUid)!;
|
||||
await eteItem.setContent(content);
|
||||
const meta = await eteItem.getMeta();
|
||||
meta.mtime = mtime;
|
||||
await eteItem.setMeta(meta);
|
||||
} else {
|
||||
// New
|
||||
const meta: Etebase.CollectionItemMetadata = {
|
||||
mtime,
|
||||
name: item.uid,
|
||||
};
|
||||
eteItem = await itemMgr.create(meta, content);
|
||||
}
|
||||
|
||||
await itemMgr.batch([eteItem]);
|
||||
}
|
||||
|
||||
async function onItemDelete(item: PimType, collectionUid: string) {
|
||||
const itemUid = item.itemUid!;
|
||||
const colMgr = getCollectionManager(etebase);
|
||||
const collection = collections!.find((x) => x.uid === collectionUid)!;
|
||||
const itemMgr = colMgr.getItemManager(collection);
|
||||
|
||||
const eteItem = items!.get(collectionUid)?.get(itemUid)!;
|
||||
const mtime = (new Date()).getUTCMilliseconds();
|
||||
const meta = await eteItem.getMeta();
|
||||
meta.mtime = mtime;
|
||||
await eteItem.setMeta(meta);
|
||||
await eteItem.delete();
|
||||
await itemMgr.batch([eteItem]);
|
||||
|
||||
history.push(routeResolver.getRoute("pim.contacts"));
|
||||
}
|
||||
|
||||
function onCancel() {
|
||||
history.goBack();
|
||||
}
|
||||
|
||||
const flatEntries = [];
|
||||
for (const col of entries.values()) {
|
||||
for (const item of col.values()) {
|
||||
flatEntries.push(item);
|
||||
}
|
||||
}
|
||||
|
||||
const styles = {
|
||||
button: {
|
||||
marginLeft: theme.spacing(1),
|
||||
},
|
||||
leftIcon: {
|
||||
marginRight: theme.spacing(1),
|
||||
},
|
||||
};
|
||||
|
||||
return (
|
||||
<Switch>
|
||||
<Route
|
||||
path={routeResolver.getRoute("pim.contacts")}
|
||||
exact
|
||||
>
|
||||
<SearchableAddressBook
|
||||
entries={flatEntries}
|
||||
onItemClick={(item) => history.push(
|
||||
routeResolver.getRoute("pim.contacts._id", { itemUid: getItemNavigationUid(item) })
|
||||
)}
|
||||
/>
|
||||
</Route>
|
||||
<Route
|
||||
path={routeResolver.getRoute("pim.contacts.new")}
|
||||
exact
|
||||
>
|
||||
<ContactEdit
|
||||
collections={cachedCollections}
|
||||
onSave={onItemSave}
|
||||
onDelete={onItemDelete}
|
||||
onCancel={onCancel}
|
||||
history={history}
|
||||
/>
|
||||
</Route>
|
||||
<Route
|
||||
path={routeResolver.getRoute("pim.contacts._id")}
|
||||
render={({ match }) => {
|
||||
const [colUid, itemUid] = match.params.itemUid.split("|");
|
||||
const item = entries.get(colUid)?.get(itemUid);
|
||||
if (!item) {
|
||||
return (<PageNotFound />);
|
||||
}
|
||||
|
||||
/* FIXME:
|
||||
const collection = collections!.find((x) => x.uid === colUid)!;
|
||||
const readOnly = collection.accessLevel;
|
||||
*/
|
||||
const readOnly = false;
|
||||
|
||||
return (
|
||||
<Switch>
|
||||
<Route
|
||||
path={routeResolver.getRoute("pim.contacts._id.edit")}
|
||||
exact
|
||||
>
|
||||
<ContactEdit
|
||||
key={itemUid}
|
||||
initialCollection={item.collectionUid}
|
||||
item={item}
|
||||
collections={cachedCollections}
|
||||
onSave={onItemSave}
|
||||
onDelete={onItemDelete}
|
||||
onCancel={onCancel}
|
||||
history={history}
|
||||
/>
|
||||
</Route>
|
||||
<Route
|
||||
path={routeResolver.getRoute("pim.contacts._id.log")}
|
||||
>
|
||||
<h1>Not currently implemented.</h1>
|
||||
</Route>
|
||||
<Route
|
||||
path={routeResolver.getRoute("pim.contacts._id")}
|
||||
exact
|
||||
>
|
||||
<div style={{ textAlign: "right", marginBottom: 15 }}>
|
||||
<Button
|
||||
variant="contained"
|
||||
style={styles.button}
|
||||
onClick={() =>
|
||||
history.push(routeResolver.getRoute("pim.contacts._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.contacts._id.edit", { itemUid: getItemNavigationUid(item) }))
|
||||
}
|
||||
>
|
||||
<IconEdit style={styles.leftIcon} />
|
||||
Edit
|
||||
</Button>
|
||||
|
||||
</div>
|
||||
<Contact item={item} />
|
||||
</Route>
|
||||
</Switch>
|
||||
);
|
||||
}}
|
||||
/>
|
||||
</Switch>
|
||||
);
|
||||
}
|
||||
|
@ -0,0 +1,14 @@
|
||||
// SPDX-FileCopyrightText: © 2020 EteSync Authors
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import * as React from "react";
|
||||
|
||||
import Container from "./widgets/Container";
|
||||
|
||||
export default function PageNotFound() {
|
||||
return (
|
||||
<Container>
|
||||
<h1>404 Page Not Found</h1>
|
||||
</Container>
|
||||
);
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
// SPDX-FileCopyrightText: © 2020 EteSync Authors
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import memoize from "memoizee";
|
||||
|
||||
import * as Etebase from "etebase";
|
||||
|
||||
import { PimType } from "../pim-types";
|
||||
|
||||
export interface CachedCollection {
|
||||
collection: Etebase.Collection;
|
||||
metadata: Etebase.CollectionMetadata;
|
||||
}
|
||||
|
||||
export function getItemNavigationUid(item: PimType) {
|
||||
// Both collectionUid and itemUid are url safe
|
||||
return `${item.collectionUid}|${item.itemUid}`;
|
||||
}
|
||||
|
||||
export function getDecryptCollectionsFunction(_colType: string) {
|
||||
return memoize(
|
||||
async function (collections: Etebase.Collection[]) {
|
||||
const entries: CachedCollection[] = [];
|
||||
if (collections) {
|
||||
for (const collection of collections) {
|
||||
entries.push({
|
||||
collection,
|
||||
metadata: await collection.getMeta(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return entries;
|
||||
},
|
||||
{ max: 1 }
|
||||
);
|
||||
}
|
||||
|
||||
export function getDecryptItemsFunction<T extends PimType>(_colType: string, parseFunc: (str: string) => T) {
|
||||
return memoize(
|
||||
async function (items: Map<string, Map<string, Etebase.CollectionItem>>) {
|
||||
const entries: Map<string, Map<string, T>> = new Map();
|
||||
if (items) {
|
||||
for (const [colUid, col] of items.entries()) {
|
||||
const cur = new Map();
|
||||
entries.set(colUid, cur);
|
||||
for (const item of col.values()) {
|
||||
const contact = parseFunc(await item.getContent(Etebase.OutputFormat.String));
|
||||
contact.collectionUid = colUid;
|
||||
contact.itemUid = item.uid;
|
||||
cur.set(item.uid, contact);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return entries;
|
||||
},
|
||||
{ max: 1 }
|
||||
);
|
||||
}
|
Loading…
Reference in New Issue