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

Commit 007712a

Browse files
fix: save published data offline (#1442)
Signed-off-by: peterpeterparker <[email protected]>
1 parent 19f9496 commit 007712a

File tree

5 files changed

+56
-16
lines changed

5 files changed

+56
-16
lines changed

providers/ic/src/providers/publish/publish.ic.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import {Deck, DeckPublish, Doc, DocPublish, PublishUrl} from '@deckdeckgo/editor
33
import {_SERVICE as StorageBucketActor} from '../../canisters/storage/storage.did';
44

55
import {BucketActor} from '../../utils/manager.utils';
6-
import {publishDeck} from '../../utils/publish.deck.utils';
6+
import {emitDeckPublished, publishDeck} from '../../utils/publish.deck.utils';
77
import {uploadResources} from '../../utils/publish.resources.utils';
88
import {publishOverview} from '../../utils/publish.overview.utils';
99
import {getStorageActor} from '../../utils/storage.utils';
10-
import {publishDoc} from '../../utils/publish.doc.utils';
10+
import {emitDocPublished, publishDoc} from '../../utils/publish.doc.utils';
1111

1212
export const deckPublish: DeckPublish = async ({deck}: {deck: Deck; config: Record<string, string>}): Promise<Deck> => {
1313
await uploadResources({meta: deck.data.meta});
@@ -16,6 +16,8 @@ export const deckPublish: DeckPublish = async ({deck}: {deck: Deck; config: Reco
1616

1717
await publishOverview({storageUpload, publishData, dataId: updatedDeck.id});
1818

19+
emitDeckPublished(deck);
20+
1921
return updatedDeck;
2022
};
2123

@@ -26,6 +28,8 @@ export const docPublish: DocPublish = async ({doc}: {doc: Doc}): Promise<Doc> =>
2628

2729
await publishOverview({storageUpload, publishData, dataId: updatedDoc.id});
2830

31+
emitDocPublished(doc);
32+
2933
return updatedDoc;
3034
};
3135

providers/ic/src/utils/publish.deck.utils.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ export const publishDeck = async ({
2828
// 5. Upload
2929
await uploadSocialImage({storageUpload, publishData});
3030

31-
// 6. Tells the snapshot the process is over
32-
emitDeckPublished(deck);
33-
3431
return {
3532
storageUpload,
3633
publishData,
@@ -54,7 +51,7 @@ const initDeckIndexHTML = async ({deck}: {deck: Deck}): Promise<{html: string; p
5451
};
5552
};
5653

57-
const emitDeckPublished = (deck: Deck) => {
54+
export const emitDeckPublished = (deck: Deck) => {
5855
const {id, data} = deck;
5956

6057
const deployedDeck: Deck = {

providers/ic/src/utils/publish.doc.utils.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ export const publishDoc = async ({
2828
// 5. Upload
2929
await uploadSocialImage({storageUpload, publishData});
3030

31-
// 6. Tells the snapshot the process is over
32-
emitDocPublished(doc);
33-
3431
return {
3532
storageUpload,
3633
publishData,
@@ -54,7 +51,7 @@ const initDocIndexHTML = async ({doc}: {doc: Doc}): Promise<{html: string; publi
5451
};
5552
};
5653

57-
const emitDocPublished = (doc: Doc) => {
54+
export const emitDocPublished = (doc: Doc) => {
5855
const {id, data} = doc;
5956

6057
const deployedDoc: Doc = {

studio/src/app/modals/editor/app-publish/app-publish.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {snapshotDeck} from '../../../providers/data/deck/deck.provider';
1111

1212
import {updateSlidesQRCode} from '../../../utils/editor/qrcode.utils';
1313
import {snapshotDoc} from '../../../providers/data/doc/doc.provider';
14+
import {updatePublishedDeckOffline, updatePublishedDocOffline} from '../../../providers/publish/publish.provider';
1415

1516
@Component({
1617
tag: 'app-publish',
@@ -24,7 +25,12 @@ export class AppPublish {
2425

2526
private unsubscribeSnapshot: () => void | undefined;
2627

28+
private docListener;
29+
private deckListener;
30+
2731
async componentWillLoad() {
32+
this.initOfflineUpdate();
33+
2834
this.unsubscribeSnapshot = await this.initSnapshot();
2935
}
3036

@@ -42,10 +48,23 @@ export class AppPublish {
4248
});
4349
}
4450

51+
/**
52+
* We snapshot the doc/deck changes and have to replicate the value to the offline data to replicate the new "meta" information.
53+
*/
54+
private initOfflineUpdate() {
55+
this.docListener = editorStore.onChange('doc', updatePublishedDocOffline);
56+
this.deckListener = editorStore.onChange('deck', updatePublishedDeckOffline);
57+
}
58+
4559
async componentDidLoad() {
4660
history.pushState({modal: true}, null);
4761
}
4862

63+
disconnectedCallback() {
64+
this.docListener?.();
65+
this.deckListener?.();
66+
}
67+
4968
@Listen('popstate', {target: 'window'})
5069
async handleHardwareBackButton(_e: PopStateEvent) {
5170
await this.closeModal();

studio/src/app/providers/publish/publish.provider.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import {Deck, Author, UserSocial, DeckPublish, PublishUrl, Meta, Doc, DocPublish} from '@deckdeckgo/editor';
22

3+
import {set} from 'idb-keyval';
4+
35
import editorStore from '../../stores/editor.store';
46
import userStore from '../../stores/user.store';
57
import authStore from '../../stores/auth.store';
8+
import errorStore from '../../stores/error.store';
69

710
import {cloud} from '../../utils/core/environment.utils';
811
import {cloudProvider} from '../../utils/core/providers.utils';
@@ -67,9 +70,7 @@ const publishDoc = async (inputs: PublishInputs): Promise<void> => {
6770

6871
const {docPublish}: {docPublish: DocPublish} = await cloudProvider<{docPublish: DocPublish}>();
6972

70-
const publishedDoc: Doc = await docPublish({doc});
71-
72-
editorStore.state.doc = {...publishedDoc};
73+
await docPublish({doc});
7374
};
7475

7576
const publishDeck = async (inputs: PublishInputs): Promise<void> => {
@@ -79,9 +80,7 @@ const publishDeck = async (inputs: PublishInputs): Promise<void> => {
7980

8081
const firebaseConfig: Record<string, string> = EnvironmentConfigService.getInstance().get('firebase');
8182

82-
const publishedDeck: Deck = await deckPublish({deck, config: firebaseConfig});
83-
84-
editorStore.state.deck = {...publishedDeck};
83+
await deckPublish({deck, config: firebaseConfig});
8584
};
8685

8786
const updateDeckMeta = (inputs: PublishInputs): Deck => {
@@ -190,3 +189,27 @@ const updateMeta = ({inputs, meta}: {inputs: PublishInputs; meta: Meta | undefin
190189

191190
return updateMeta;
192191
};
192+
193+
export const updatePublishedDocOffline = async (doc: Doc | undefined) => {
194+
if (!doc) {
195+
return;
196+
}
197+
198+
try {
199+
await set(`/docs/${doc.id}`, doc);
200+
} catch (err) {
201+
errorStore.state.error = err;
202+
}
203+
};
204+
205+
export const updatePublishedDeckOffline = async (deck: Deck | undefined) => {
206+
if (!deck) {
207+
return;
208+
}
209+
210+
try {
211+
await set(`/decks/${deck.id}`, deck);
212+
} catch (err) {
213+
errorStore.state.error = err;
214+
}
215+
};

0 commit comments

Comments
 (0)