Collection upload: consolidate reducer and update store on upload.

master
Tom Hacohen 4 years ago
parent 2f8794e8fd
commit 2c2abbb7ae

@ -33,7 +33,6 @@ export interface CachedItem {
async function decryptItems(items: Map<string, Map<string, Etebase.CollectionItem>>) {
const entries: Map<string, Map<string, CachedItem>> = new Map();
if (items) {
for (const [colUid, col] of items.entries()) {
const cur = new Map();
entries.set(colUid, cur);
@ -45,7 +44,6 @@ async function decryptItems(items: Map<string, Map<string, Etebase.CollectionIte
});
}
}
}
return entries;
}

@ -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<void> {
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"));
}

@ -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,
};
}
);

@ -94,15 +94,17 @@ export const syncGeneral = handleActions(
export const collections = handleActions(
{
[actions.setCacheCollection.toString()]: (state: CacheCollectionsData, action: ActionMeta<CacheCollection, { colUid: string }>) => {
[combineActions(
actions.setCacheCollection,
actions.collectionUpload,
actions.unsetCacheCollection
).toString()]: (state: CacheCollectionsData, action: ActionMeta<CacheCollection, { colUid: string, deleted: boolean }>) => {
if (action.payload !== undefined) {
if (action.meta.deleted) {
return state.remove(action.meta.colUid);
} else {
return state.set(action.meta.colUid, action.payload);
}
return state;
},
[actions.unsetCacheCollection.toString()]: (state: CacheCollectionsData, action: ActionMeta<string, { colUid: string }>) => {
if (action.payload !== undefined) {
return state.remove(action.meta.colUid);
}
return state;
},
@ -124,6 +126,12 @@ export const items = handleActions(
}
return state;
},
[actions.setCacheCollection.toString()]: (state: CacheItemsData, action: ActionMeta<CacheCollection, { colUid: string }>) => {
if (action.payload !== undefined) {
return state.set(action.meta.colUid, ImmutableMap());
}
return state;
},
[actions.unsetCacheCollection.toString()]: (state: CacheItemsData, action: ActionMeta<string, { colUid: string }>) => {
if (action.payload !== undefined) {
return state.remove(action.meta.colUid);

Loading…
Cancel
Save