|
1 |
| -import { autorun, makeAutoObservable, runInAction } from "mobx"; |
| 1 | +import { makeAutoObservable, runInAction, when } from "mobx"; |
2 | 2 | import { types, Instance, SnapshotIn, applySnapshot, typecheck, unprotect } from "mobx-state-tree";
|
3 | 3 | import { union } from "lodash";
|
4 | 4 | import firebase from "firebase";
|
@@ -80,6 +80,8 @@ export class SortedDocuments {
|
80 | 80 | metadataDocsFiltered = MetadataDocMapModel.create();
|
81 | 81 | metadataDocsWithoutUnit = MetadataDocMapModel.create();
|
82 | 82 | docsReceived = false;
|
| 83 | + // Maps from document ID to the history entry ID that the user requested to view. |
| 84 | + documentHistoryViewRequests: Record<string,string> = {}; |
83 | 85 |
|
84 | 86 | constructor(stores: ISortedDocumentsStores) {
|
85 | 87 | makeAutoObservable(this);
|
@@ -117,6 +119,20 @@ export class SortedDocuments {
|
117 | 119 | return this.stores.user;
|
118 | 120 | }
|
119 | 121 |
|
| 122 | + setDocumentHistoryViewRequest(docKey: string, historyId: string) { |
| 123 | + this.documentHistoryViewRequests[docKey] = historyId; |
| 124 | + console.log("setDocumentHistoryViewRequest", docKey, historyId); |
| 125 | + } |
| 126 | + getDocumentHistoryViewRequest(docKey: string) { |
| 127 | + // We only want to move to this history entry once, |
| 128 | + // so we delete the request after it has been fulfilled. |
| 129 | + const historyId = this.documentHistoryViewRequests[docKey]; |
| 130 | + if (historyId) { |
| 131 | + delete this.documentHistoryViewRequests[docKey]; |
| 132 | + } |
| 133 | + return historyId; |
| 134 | + } |
| 135 | + |
120 | 136 | sortBy(sortType: PrimarySortType): DocumentGroup[] {
|
121 | 137 | switch (sortType) {
|
122 | 138 | case "Group":
|
@@ -356,15 +372,8 @@ export class SortedDocuments {
|
356 | 372 |
|
357 | 373 | async fetchFullDocument(docKey: string) {
|
358 | 374 | if (!this.docsReceived) {
|
359 |
| - // Wait until the initial documents have been received before trying to open a document. |
360 |
| - await new Promise<void>(resolve => { |
361 |
| - const disposer = autorun(() => { |
362 |
| - if (this.docsReceived) { |
363 |
| - disposer(); |
364 |
| - resolve(); |
365 |
| - } |
366 |
| - }); |
367 |
| - }); |
| 375 | + // Wait until the initial batch of documents has been received from Firestore. |
| 376 | + await when(() => this.docsReceived); |
368 | 377 | }
|
369 | 378 | const metadataDoc = this.firestoreMetadataDocs.find(doc => doc.key === docKey);
|
370 | 379 | if (!metadataDoc) {
|
|
0 commit comments