Collection upload: consolidate reducer and update store on upload.

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

@ -33,17 +33,15 @@ export interface CachedItem {
async function decryptItems(items: Map<string, Map<string, Etebase.CollectionItem>>) { async function decryptItems(items: Map<string, Map<string, Etebase.CollectionItem>>) {
const entries: Map<string, Map<string, CachedItem>> = new Map(); const entries: Map<string, Map<string, CachedItem>> = new Map();
if (items) { for (const [colUid, col] of items.entries()) {
for (const [colUid, col] of items.entries()) { const cur = new Map();
const cur = new Map(); entries.set(colUid, cur);
entries.set(colUid, cur); for (const item of col.values()) {
for (const item of col.values()) { cur.set(item.uid, {
cur.set(item.uid, { item,
item, metadata: await item.getMeta(),
metadata: await item.getMeta(), content: await item.getContent(Etebase.OutputFormat.String),
content: await item.getContent(Etebase.OutputFormat.String), });
});
}
} }
} }

@ -18,6 +18,8 @@ import PageNotFound from "../PageNotFound";
import CollectionEdit from "./CollectionEdit"; import CollectionEdit from "./CollectionEdit";
import CollectionMembers from "./CollectionMembers"; import CollectionMembers from "./CollectionMembers";
import Collection from "./Collection"; import Collection from "./Collection";
import { useAsyncDispatch } from "../store";
import { collectionUpload } from "../store/actions";
const decryptCollections = getDecryptCollectionsFunction(); const decryptCollections = getDecryptCollectionsFunction();
@ -26,6 +28,7 @@ export default function CollectionsMain() {
const history = useHistory(); const history = useHistory();
const etebase = useCredentials()!; const etebase = useCredentials()!;
const collections = useCollections(etebase); const collections = useCollections(etebase);
const dispatch = useAsyncDispatch();
React.useEffect(() => { React.useEffect(() => {
if (collections) { if (collections) {
@ -43,7 +46,7 @@ export default function CollectionsMain() {
async function onSave(collection: Etebase.Collection): Promise<void> { async function onSave(collection: Etebase.Collection): Promise<void> {
const colMgr = getCollectionManager(etebase); const colMgr = getCollectionManager(etebase);
await colMgr.upload(collection); await dispatch(collectionUpload(colMgr, collection));
history.push(routeResolver.getRoute("collections")); history.push(routeResolver.getRoute("collections"));
} }
@ -51,7 +54,7 @@ export default function CollectionsMain() {
async function onDelete(collection: Etebase.Collection) { async function onDelete(collection: Etebase.Collection) {
const colMgr = getCollectionManager(etebase); const colMgr = getCollectionManager(etebase);
await collection.delete(); await collection.delete();
await colMgr.upload(collection); await dispatch(collectionUpload(colMgr, collection));
history.push(routeResolver.getRoute("collections")); history.push(routeResolver.getRoute("collections"));
} }

@ -37,15 +37,34 @@ export const setCacheCollection = createAction(
(_colMgr: Etebase.CollectionManager, col: Etebase.Collection) => { (_colMgr: Etebase.CollectionManager, col: Etebase.Collection) => {
return { return {
colUid: col.uid, colUid: col.uid,
deleted: col.isDeleted,
}; };
} }
); );
export const unsetCacheCollection = createAction( export const unsetCacheCollection = createAction(
"UNSET_CACHE_COLLECTION", "UNSET_CACHE_COLLECTION",
(_colMgr: Etebase.CollectionManager, _colUid: string) => {
return undefined;
},
(_colMgr: Etebase.CollectionManager, colUid: string) => { (_colMgr: Etebase.CollectionManager, colUid: string) => {
return { return {
colUid, 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( export const collections = handleActions(
{ {
[actions.setCacheCollection.toString()]: (state: CacheCollectionsData, action: ActionMeta<CacheCollection, { colUid: string }>) => { [combineActions(
if (action.payload !== undefined) { actions.setCacheCollection,
return state.set(action.meta.colUid, action.payload); actions.collectionUpload,
} actions.unsetCacheCollection
return state; ).toString()]: (state: CacheCollectionsData, action: ActionMeta<CacheCollection, { colUid: string, deleted: boolean }>) => {
},
[actions.unsetCacheCollection.toString()]: (state: CacheCollectionsData, action: ActionMeta<string, { colUid: string }>) => {
if (action.payload !== undefined) { 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; return state;
}, },
@ -124,6 +126,12 @@ export const items = handleActions(
} }
return state; 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 }>) => { [actions.unsetCacheCollection.toString()]: (state: CacheItemsData, action: ActionMeta<string, { colUid: string }>) => {
if (action.payload !== undefined) { if (action.payload !== undefined) {
return state.remove(action.meta.colUid); return state.remove(action.meta.colUid);

Loading…
Cancel
Save