Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 37e656b

Browse files
refactor: move firestore prepareAttributes (#1254)
1 parent 8875940 commit 37e656b

File tree

3 files changed

+31
-35
lines changed

3 files changed

+31
-35
lines changed

studio/src/app/services/editor/offline/offline.service.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {Slide, SlideAttributes} from '../../../models/data/slide';
1111

1212
import {SlotType} from '../../../types/editor/slot-type';
1313

14-
import {OfflineUtils} from '../../../utils/editor/offline.utils';
1514
import {FirestoreUtils} from '../../../utils/editor/firestore.utils';
1615
import {ServiceWorkerUtils} from '../../../utils/core/service-worker.utils';
1716

@@ -633,7 +632,7 @@ export class OfflineService {
633632
return;
634633
}
635634

636-
slide.data.attributes = (await OfflineUtils.prepareAttributes(slide.data.attributes)) as SlideAttributes;
635+
slide.data.attributes = (await FirestoreUtils.prepareAttributes(slide.data.attributes)) as SlideAttributes;
637636

638637
if (slide.data.content === null) {
639638
// @ts-ignore
@@ -669,7 +668,7 @@ export class OfflineService {
669668
return;
670669
}
671670

672-
deck.data.attributes = (await OfflineUtils.prepareAttributes(deck.data.attributes)) as DeckAttributes;
671+
deck.data.attributes = (await FirestoreUtils.prepareAttributes(deck.data.attributes)) as DeckAttributes;
673672

674673
if (deck.data.background === null) {
675674
// @ts-ignore

studio/src/app/utils/editor/firestore.utils.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import firebase from 'firebase/app';
22
import 'firebase/firestore';
33

4+
import {SlideAttributes} from '../../models/data/slide';
5+
import {DeckAttributes} from '../../models/data/deck';
6+
47
export class FirestoreUtils {
58
static filterDelete<T>(obj: T, replaceWithNull: boolean = false): T {
69
if (typeof obj !== 'object' || Array.isArray(obj)) {
@@ -35,4 +38,30 @@ export class FirestoreUtils {
3538

3639
return JSON.stringify(attr) === JSON.stringify(firebase.firestore.FieldValue.delete());
3740
}
41+
42+
static async prepareAttributes(attributes: SlideAttributes | DeckAttributes): Promise<SlideAttributes | DeckAttributes> {
43+
if (!attributes) {
44+
// @ts-ignore
45+
return firebase.firestore.FieldValue.delete();
46+
}
47+
48+
const keys: string[] = Object.keys(attributes);
49+
50+
if (!keys || keys.length <= 0) {
51+
// @ts-ignore
52+
return firebase.firestore.FieldValue.delete();
53+
}
54+
55+
keys.forEach((key: string) => {
56+
const attr = attributes[key];
57+
58+
// Replace null values with Firestore "to delete fields"
59+
if (attr === null) {
60+
// @ts-ignore
61+
attributes[key] = firebase.firestore.FieldValue.delete();
62+
}
63+
});
64+
65+
return attributes;
66+
}
3867
}
Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import firebase from 'firebase/app';
2-
31
import {SlideAttributes} from '../../models/data/slide';
42
import {DeckAttributes} from '../../models/data/deck';
53

@@ -19,34 +17,4 @@ export class OfflineUtils {
1917

2018
return FirestoreUtils.filterDelete(attributes, true);
2119
}
22-
23-
static prepareAttributes(attributes: SlideAttributes | DeckAttributes): Promise<SlideAttributes | DeckAttributes> {
24-
return new Promise<SlideAttributes | DeckAttributes>((resolve) => {
25-
if (!attributes) {
26-
// @ts-ignore
27-
resolve(firebase.firestore.FieldValue.delete());
28-
return;
29-
}
30-
31-
const keys: string[] = Object.keys(attributes);
32-
33-
if (!keys || keys.length <= 0) {
34-
// @ts-ignore
35-
resolve(firebase.firestore.FieldValue.delete());
36-
return;
37-
}
38-
39-
keys.forEach((key: string) => {
40-
const attr = attributes[key];
41-
42-
// Replace null values with Firestore "to delete fields"
43-
if (attr === null) {
44-
// @ts-ignore
45-
attributes[key] = firebase.firestore.FieldValue.delete();
46-
}
47-
});
48-
49-
resolve(attributes);
50-
});
51-
}
5220
}

0 commit comments

Comments
 (0)