diff --git a/src/Collections/CollectionChangeEntries.tsx b/src/Collections/CollectionChangeEntries.tsx index 75c9c86..10dd51f 100644 --- a/src/Collections/CollectionChangeEntries.tsx +++ b/src/Collections/CollectionChangeEntries.tsx @@ -33,17 +33,15 @@ export interface CachedItem { async function decryptItems(items: Map>) { const entries: Map> = 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()) { - cur.set(item.uid, { - item, - metadata: await item.getMeta(), - content: await item.getContent(Etebase.OutputFormat.String), - }); - } + for (const [colUid, col] of items.entries()) { + const cur = new Map(); + entries.set(colUid, cur); + for (const item of col.values()) { + cur.set(item.uid, { + item, + metadata: await item.getMeta(), + content: await item.getContent(Etebase.OutputFormat.String), + }); } } diff --git a/src/Collections/Main.tsx b/src/Collections/Main.tsx index f9a1011..c5b7cb4 100644 --- a/src/Collections/Main.tsx +++ b/src/Collections/Main.tsx @@ -18,6 +18,8 @@ import PageNotFound from "../PageNotFound"; import CollectionEdit from "./CollectionEdit"; import CollectionMembers from "./CollectionMembers"; import Collection from "./Collection"; +import { useAsyncDispatch } from "../store"; +import { collectionUpload } from "../store/actions"; const decryptCollections = getDecryptCollectionsFunction(); @@ -26,6 +28,7 @@ export default function CollectionsMain() { const history = useHistory(); const etebase = useCredentials()!; const collections = useCollections(etebase); + const dispatch = useAsyncDispatch(); React.useEffect(() => { if (collections) { @@ -43,7 +46,7 @@ export default function CollectionsMain() { async function onSave(collection: Etebase.Collection): Promise { const colMgr = getCollectionManager(etebase); - await colMgr.upload(collection); + await dispatch(collectionUpload(colMgr, collection)); history.push(routeResolver.getRoute("collections")); } @@ -51,7 +54,7 @@ export default function CollectionsMain() { async function onDelete(collection: Etebase.Collection) { const colMgr = getCollectionManager(etebase); await collection.delete(); - await colMgr.upload(collection); + await dispatch(collectionUpload(colMgr, collection)); history.push(routeResolver.getRoute("collections")); } diff --git a/src/store/actions.ts b/src/store/actions.ts index 7a7d5c5..62a5a9b 100644 --- a/src/store/actions.ts +++ b/src/store/actions.ts @@ -37,15 +37,34 @@ export const setCacheCollection = createAction( (_colMgr: Etebase.CollectionManager, col: Etebase.Collection) => { return { colUid: col.uid, + deleted: col.isDeleted, }; } ); export const unsetCacheCollection = createAction( "UNSET_CACHE_COLLECTION", + (_colMgr: Etebase.CollectionManager, _colUid: string) => { + return undefined; + }, (_colMgr: Etebase.CollectionManager, colUid: string) => { return { colUid, + deleted: true, + }; + } +); + +export const collectionUpload = createAction( + "COLLECTION_UPLOAD", + async (colMgr: Etebase.CollectionManager, col: Etebase.Collection) => { + await colMgr.upload(col); + return Etebase.toBase64(await colMgr.cacheSave(col)); + }, + (_colMgr: Etebase.CollectionManager, col: Etebase.Collection) => { + return { + colUid: col.uid, + deleted: col.isDeleted, }; } ); diff --git a/src/store/reducers.ts b/src/store/reducers.ts index 0eca33a..0db8f25 100644 --- a/src/store/reducers.ts +++ b/src/store/reducers.ts @@ -94,15 +94,17 @@ export const syncGeneral = handleActions( export const collections = handleActions( { - [actions.setCacheCollection.toString()]: (state: CacheCollectionsData, action: ActionMeta) => { - if (action.payload !== undefined) { - return state.set(action.meta.colUid, action.payload); - } - return state; - }, - [actions.unsetCacheCollection.toString()]: (state: CacheCollectionsData, action: ActionMeta) => { + [combineActions( + actions.setCacheCollection, + actions.collectionUpload, + actions.unsetCacheCollection + ).toString()]: (state: CacheCollectionsData, action: ActionMeta) => { if (action.payload !== undefined) { - return state.remove(action.meta.colUid); + if (action.meta.deleted) { + return state.remove(action.meta.colUid); + } else { + return state.set(action.meta.colUid, action.payload); + } } return state; }, @@ -124,6 +126,12 @@ export const items = handleActions( } return state; }, + [actions.setCacheCollection.toString()]: (state: CacheItemsData, action: ActionMeta) => { + if (action.payload !== undefined) { + return state.set(action.meta.colUid, ImmutableMap()); + } + return state; + }, [actions.unsetCacheCollection.toString()]: (state: CacheItemsData, action: ActionMeta) => { if (action.payload !== undefined) { return state.remove(action.meta.colUid);