From f4ac017f07d38413b25dc007262cce35a47c180b Mon Sep 17 00:00:00 2001 From: peterpeterparker Date: Sun, 22 Nov 2020 13:39:55 +0100 Subject: [PATCH] fix: clean offline slides --- .../src/app/utils/editor/firestore.utils.tsx | 4 ++- studio/src/app/utils/editor/offline.utils.tsx | 31 ++++++------------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/studio/src/app/utils/editor/firestore.utils.tsx b/studio/src/app/utils/editor/firestore.utils.tsx index d2b287018..cb96fa023 100644 --- a/studio/src/app/utils/editor/firestore.utils.tsx +++ b/studio/src/app/utils/editor/firestore.utils.tsx @@ -1,5 +1,5 @@ export class FirestoreUtils { - static filterDelete(obj: T): T { + static filterDelete(obj: T, replaceWithNull: boolean = false): T { if (typeof obj !== 'object' || Array.isArray(obj)) { return obj; } @@ -13,6 +13,8 @@ export class FirestoreUtils { // We don't want to keep empty leaf {} if (Object.keys(value).length > 0) { res[key] = value; + } else if (replaceWithNull) { + res[key] = null; } } else { res[key] = value; diff --git a/studio/src/app/utils/editor/offline.utils.tsx b/studio/src/app/utils/editor/offline.utils.tsx index 0da3ec0ff..d3ff264f0 100644 --- a/studio/src/app/utils/editor/offline.utils.tsx +++ b/studio/src/app/utils/editor/offline.utils.tsx @@ -6,31 +6,18 @@ import {DeckAttributes} from '../../models/data/deck'; import {FirestoreUtils} from './firestore.utils'; export class OfflineUtils { - static cleanAttributes(attributes: SlideAttributes | DeckAttributes): Promise { - return new Promise((resolve) => { - if (!attributes || attributes === undefined) { - resolve(null); - return; - } + static async cleanAttributes(attributes: SlideAttributes | DeckAttributes): Promise { + if (!attributes || attributes === undefined) { + return null; + } - const keys: string[] = Object.keys(attributes); + const keys: string[] = Object.keys(attributes); - if (!keys || keys.length <= 0) { - resolve(null); - return; - } + if (!keys || keys.length <= 0) { + return null; + } - keys.forEach((key: string) => { - const attr = attributes[key]; - - // Replace Firestore "to delete fields" with null values - if (FirestoreUtils.shouldAttributeBeCleaned(attr)) { - attributes[key] = null; - } - }); - - resolve(attributes); - }); + return FirestoreUtils.filterDelete(attributes, true); } static prepareAttributes(attributes: SlideAttributes | DeckAttributes): Promise {