-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathuseNoteSettings.ts
More file actions
226 lines (197 loc) · 5.77 KB
/
useNoteSettings.ts
File metadata and controls
226 lines (197 loc) · 5.77 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { ref, type Ref } from 'vue';
import type NoteSettings from '@/domain/entities/NoteSettings';
import type { Note, NoteId } from '@/domain/entities/Note';
import { noteSettingsService, noteService } from '@/domain';
import type { UserId } from '@/domain/entities/User';
import type { MemberRole } from '@/domain/entities/Team';
import { useRouter } from 'vue-router';
/**
* Note settings hook state
*/
interface UseNoteSettingsComposableState {
/**
* NoteSettings ref
*/
noteSettings: Ref<NoteSettings | null>;
/**
* Parent note, undefined if it's a root note
*/
parentNote: Ref<Note | undefined>;
/**
* Load note settings
* @param id - note id
*/
load: (id: NoteId) => Promise<void>;
/**
* Update field isPublic in note settings
* @param id - note id
* @param newIsPublicValue - new value for isPublic field
*/
updateIsPublic: (id: NoteId, newIsPublicValue: boolean) => Promise<void>;
/**
* Update field showNoteHierarchy in note settings
* @param id - Note id
* @param newShowNoteHierarchyValue - new showNoteHierarchy
*/
updateShowNoteHierarchy: (id: NoteId, newShowNoteHierarchyValue: boolean) => Promise<void>;
/**
* Revoke invitation hash
* @param id - note id
*/
revokeHash: (id: NoteId) => Promise<string>;
/**
* Patch team member role by user and note id
* @param id - Note id
* @param userId - id of the user whose role is to be changed
* @param newRole - new role
*/
changeRole: (id: NoteId, userId: UserId, newRole: MemberRole) => Promise<void>;
/**
* Delete note by it's id
* @param id - Note id
*/
deleteNoteById: (id: NoteId) => Promise<void>;
/**
* Update note cover
* @param id - note id
* @param data - picture binary data
*/
updateCover: (id: NoteId, data: Blob) => Promise<void>;
/**
* Set parent for the note
* @param id - Child note id
* @param newParentURL - New parent note URL
*/
setParent: (id: NoteId, newParentURL: string) => Promise<void>;
}
/**
* Application service for working with the Note settings
*/
export default function (): UseNoteSettingsComposableState {
/**
* NoteSettings ref
*/
const noteSettings = ref<NoteSettings | null>(null);
/**
* Parent note
*
* undefined by default
*/
const parentNote = ref<Note | undefined>();
/**
* Router instance used to replace the current route with note id
*/
const router = useRouter();
/**
* Get note settings
* @param id - Note id
*/
const load = async (id: NoteId): Promise<void> => {
noteSettings.value = await noteSettingsService.getNoteSettingsById(id);
const response = await noteService.getNoteById(id);
parentNote.value = response.parentNote;
};
/**
* Update field isPublic in note settings
* @param id - Note id
* @param newIsPublicValue - new isPublic
*/
async function updateIsPublic(id: NoteId, newIsPublicValue: boolean): Promise<void> {
const { isPublic } = await noteSettingsService.patchNoteSettingsByNoteId(id, { isPublic: newIsPublicValue });
/**
* If note settings were not loaded till this moment for some reason, do nothing
*/
if (noteSettings.value) {
noteSettings.value.isPublic = isPublic;
}
}
/**
* Update field showNoteHierarchy in note settings
* @param id - Note id
* @param newShowNoteHierarchyValue - new showNoteHierarchy
*/
async function updateShowNoteHierarchy(id: NoteId, newShowNoteHierarchyValue: boolean): Promise<void> {
const { showNoteHierarchy } = await noteSettingsService.patchNoteSettingsByNoteId(id, { showNoteHierarchy: newShowNoteHierarchyValue });
/**
* If note settings were not loaded till this moment for some reason, do nothing
*/
if (noteSettings.value) {
noteSettings.value.showNoteHierarchy = showNoteHierarchy;
}
}
/**
* Revoke invitation hash
* @param id - Note id
*/
const revokeHash = async (id: NoteId): Promise<string> => {
const { invitationHash } = await noteSettingsService.regenerateInvitationHash(id);
/**
* Check if note setting is not empty
*/
if (noteSettings.value) {
noteSettings.value = { ...noteSettings.value,
invitationHash };
}
return invitationHash;
};
/**
* Patch team member role by user and note id
* @param id - Note id
* @param userId - id of the user whose role is to be changed
* @param newRole - new role
* @returns updated note settings
*/
const changeRole = async (id: NoteId, userId: UserId, newRole: MemberRole): Promise<void> => {
await noteSettingsService.patchMemberRoleByUserId(id, userId, newRole);
};
/**
* Delete note by it's id
* @param id - Note id
*/
const deleteNoteById = async (id: NoteId): Promise<void> => {
await noteSettingsService.deleteNote(id);
void router.push({
name: 'home',
});
};
/**
* Set parent for the note
* @param id - Child note id
* @param newParentURL - New parent note URL
*/
async function setParent(id: NoteId, newParentURL: string): Promise<void> {
try {
parentNote.value = await noteService.setParentByUrl(id, newParentURL);
} catch (error) {
if (error instanceof Error) {
window.alert(error.message);
}
}
};
/**
* Update note cover picture
* @param id - note id
* @param data - picture binary data
*/
const updateCover = async (id: NoteId, data: Blob): Promise<void> => {
const { cover } = await noteSettingsService.updateCover(id, data);
if (noteSettings.value) {
noteSettings.value = {
...noteSettings.value,
cover,
};
}
};
return {
updateCover,
setParent,
parentNote,
noteSettings,
load,
updateIsPublic,
revokeHash,
changeRole,
deleteNoteById,
updateShowNoteHierarchy,
};
}