-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjournalRepository.ts
84 lines (71 loc) · 3.14 KB
/
journalRepository.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import Observable from "../../shared/observable/observable.ts";
import { Journal, JournalDTO } from "./journal.ts";
import { ClientStorageRepository } from "./clientStorageRepository.ts";
export interface JournalState {
journals: JournalDTO[];
pendingDeletion: JournalDTO | null;
showConfirmationModal: boolean;
}
export class JournalRepository {
private readonly journalState: Observable<JournalState>;
constructor(private readonly clientRepository: ClientStorageRepository) {
this.journalState = new Observable<JournalState>({journals: [], pendingDeletion: null, showConfirmationModal: false});
this.hydrateFromClientStorage();
}
async hydrateFromClientStorage() {
// Hydrate journals from client storage on load
const favorites = await this.clientRepository.getAll();
if (favorites.length) {
this.journalState.setValue({journals: favorites, pendingDeletion: null, showConfirmationModal: false});
}
}
async add(journal: Journal) {
this.journalState.setValue({
journals: [...this.journalState.getValue().journals, journal.toPersistence()],
pendingDeletion: null,
showConfirmationModal: false,
});
}
async loadJournals(presenterCb: (journals: JournalState) => void) {
this.journalState.subscribe(presenterCb);
}
async delete(journal: Journal) {
// If journal is not a favorite, just delete it
if (!journal.getIsFavorite()) {
const journalState = this.journalState.getValue();
const newJournals = journalState.journals.filter(v => v.id !== journal.getId());
this.journalState.setValue({...journalState, journals: newJournals});
return;
}
const { pendingDeletion, journals } = this.journalState.getValue();
if (pendingDeletion && pendingDeletion.id === journal.getId()) {
const newJournals = journals.filter(v => v.id !== journal.getId());
this.journalState.setValue({journals: newJournals, pendingDeletion: null, showConfirmationModal: false});
await this.clientRepository.delete(journal.getId());
this.journalState.setValue({...this.journalState.getValue(), pendingDeletion: null});
return;
}
this.journalState.setValue({...this.journalState.getValue(), pendingDeletion: journal.toPersistence(), showConfirmationModal: true});
}
async setFavorite(journal: Journal) {
const { journals } = this.journalState.getValue();
const newJournals: JournalDTO[] = journals.map(f => {
if (f.id === journal.getId()) {
return {...journal.toPersistence(), isFavorite: !journal.getIsFavorite()};
}
return f;
});
await this.clientRepository.add(journal);
this.journalState.setValue({journals: newJournals, pendingDeletion: null, showConfirmationModal: false});
}
resetPendingDeletion() {
this.journalState.setValue({...this.journalState.getValue(), pendingDeletion: null, showConfirmationModal: false});
}
setConfirmationModal() {
const state = this.journalState.getValue();
this.journalState.setValue({...this.journalState.getValue(), showConfirmationModal: !state.showConfirmationModal});
}
hasJournals() {
return this.journalState.getValue().journals.length > 0;
}
}