diff --git a/src/Calendars/Main.tsx b/src/Calendars/Main.tsx index 19b9b55..6e38ebe 100644 --- a/src/Calendars/Main.tsx +++ b/src/Calendars/Main.tsx @@ -13,7 +13,7 @@ import IconChangeHistory from "@material-ui/icons/ChangeHistory"; import { EventType, PimType } from "../pim-types"; import { useCredentials } from "../credentials"; -import { useItems, useCollections, getCollectionManager } from "../etebase-helpers"; +import { useItems, useCollections } from "../etebase-helpers"; import { routeResolver } from "../App"; import Calendar from "./Calendar"; import Event from "./Event"; @@ -21,7 +21,7 @@ import LoadingIndicator from "../widgets/LoadingIndicator"; import EventEdit from "./EventEdit"; import PageNotFound from "../PageNotFound"; -import { CachedCollection, getItemNavigationUid, getDecryptCollectionsFunction, getDecryptItemsFunction, PimFab } from "../Pim/helpers"; +import { CachedCollection, getItemNavigationUid, getDecryptCollectionsFunction, getDecryptItemsFunction, PimFab, itemDelete, itemSave } from "../Pim/helpers"; import { historyPersistor } from "../persist-state-history"; const PersistCalendar = historyPersistor("Calendar")(Calendar); @@ -60,47 +60,13 @@ export default function CalendarsMain() { } async function onItemSave(item: PimType, collectionUid: string, originalItem?: PimType): Promise { - 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()).getTime(); - 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]); + await itemSave(etebase, collection, items!, item, collectionUid, originalItem); } 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()).getTime(); - const meta = await eteItem.getMeta(); - meta.mtime = mtime; - await eteItem.setMeta(meta); - await eteItem.delete(); - await itemMgr.batch([eteItem]); + await itemDelete(etebase, collection, items!, item, collectionUid); history.push(routeResolver.getRoute("pim.events")); } diff --git a/src/Contacts/Main.tsx b/src/Contacts/Main.tsx index cd98871..d56825d 100644 --- a/src/Contacts/Main.tsx +++ b/src/Contacts/Main.tsx @@ -12,7 +12,7 @@ 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 { useItems, useCollections } from "../etebase-helpers"; import { routeResolver } from "../App"; import SearchableAddressBook from "./SearchableAddressBook"; import Contact from "./Contact"; @@ -20,7 +20,7 @@ import LoadingIndicator from "../widgets/LoadingIndicator"; import ContactEdit from "./ContactEdit"; import PageNotFound from "../PageNotFound"; -import { CachedCollection, getItemNavigationUid, getDecryptCollectionsFunction, getDecryptItemsFunction, PimFab } from "../Pim/helpers"; +import { CachedCollection, getItemNavigationUid, getDecryptCollectionsFunction, getDecryptItemsFunction, PimFab, itemSave, itemDelete } from "../Pim/helpers"; const colType = "etebase.vcard"; @@ -56,47 +56,13 @@ export default function ContactsMain() { } async function onItemSave(item: PimType, collectionUid: string, originalItem?: PimType): Promise { - 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()).getTime(); - 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]); + await itemSave(etebase, collection, items!, item, collectionUid, originalItem); } 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()).getTime(); - const meta = await eteItem.getMeta(); - meta.mtime = mtime; - await eteItem.setMeta(meta); - await eteItem.delete(); - await itemMgr.batch([eteItem]); + await itemDelete(etebase, collection, items!, item, collectionUid); history.push(routeResolver.getRoute("pim.contacts")); } diff --git a/src/Pim/helpers.tsx b/src/Pim/helpers.tsx index c6e9556..d4b6fbc 100644 --- a/src/Pim/helpers.tsx +++ b/src/Pim/helpers.tsx @@ -10,6 +10,7 @@ import memoize from "memoizee"; import * as Etebase from "etebase"; import { PimType } from "../pim-types"; +import { getCollectionManager } from "../etebase-helpers"; export const defaultColor = "#8BC34A"; @@ -65,6 +66,48 @@ export function getDecryptItemsFunction(_colType: string, par ); } +export async function itemSave(etebase: Etebase.Account, collection: Etebase.Collection, items: Map>, item: PimType, collectionUid: string, originalItem?: PimType): Promise { + const itemUid = originalItem?.itemUid; + const colMgr = getCollectionManager(etebase); + const itemMgr = colMgr.getItemManager(collection); + + const mtime = (new Date()).getTime(); + 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]); +} + +export async function itemDelete(etebase: Etebase.Account, collection: Etebase.Collection, items: Map>, item: PimType, collectionUid: string) { + const itemUid = item.itemUid!; + const colMgr = getCollectionManager(etebase); + const itemMgr = colMgr.getItemManager(collection); + + const eteItem = items!.get(collectionUid)?.get(itemUid)!; + const mtime = (new Date()).getTime(); + const meta = await eteItem.getMeta(); + meta.mtime = mtime; + await eteItem.setMeta(meta); + await eteItem.delete(); + await itemMgr.batch([eteItem]); +} + interface PimFabPropsType { onClick: () => void; } diff --git a/src/Tasks/Main.tsx b/src/Tasks/Main.tsx index 6f94292..7319baa 100644 --- a/src/Tasks/Main.tsx +++ b/src/Tasks/Main.tsx @@ -12,7 +12,7 @@ import IconChangeHistory from "@material-ui/icons/ChangeHistory"; import { TaskType, PimType } from "../pim-types"; import { useCredentials } from "../credentials"; -import { useItems, useCollections, getCollectionManager } from "../etebase-helpers"; +import { useItems, useCollections } from "../etebase-helpers"; import { routeResolver } from "../App"; import TaskList from "./TaskList"; import Task from "./Task"; @@ -20,7 +20,7 @@ import LoadingIndicator from "../widgets/LoadingIndicator"; import TaskEdit from "./TaskEdit"; import PageNotFound from "../PageNotFound"; -import { CachedCollection, getItemNavigationUid, getDecryptCollectionsFunction, getDecryptItemsFunction, PimFab } from "../Pim/helpers"; +import { CachedCollection, getItemNavigationUid, getDecryptCollectionsFunction, getDecryptItemsFunction, PimFab, itemSave, itemDelete } from "../Pim/helpers"; const colType = "etebase.vtodo"; @@ -56,47 +56,13 @@ export default function TasksMain() { } async function onItemSave(item: PimType, collectionUid: string, originalItem?: PimType): Promise { - 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()).getTime(); - 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]); + await itemSave(etebase, collection, items!, item, collectionUid, originalItem); } 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()).getTime(); - const meta = await eteItem.getMeta(); - meta.mtime = mtime; - await eteItem.setMeta(meta); - await eteItem.delete(); - await itemMgr.batch([eteItem]); + await itemDelete(etebase, collection, items!, item, collectionUid); history.push(routeResolver.getRoute("pim.tasks")); }