-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathadmin-lecture-list.ts
More file actions
225 lines (194 loc) · 8.12 KB
/
Copy pathadmin-lecture-list.ts
File metadata and controls
225 lines (194 loc) · 8.12 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
import { StreamableMapProvider } from "./provider";
import {
AdminLectureList,
Lecture,
LectureFile,
UpdateLectureMetaRequest,
VideoSection,
videoSectionSort,
} from "../api/admin-lecture-list";
import { FileType } from "../edit-course";
import { PostFormDataListener } from "../utilities/fetch-wrappers";
const dateFormatOptions: Intl.DateTimeFormatOptions = {
weekday: "long",
year: "numeric",
month: "short",
day: "2-digit",
};
const timeFormatOptions: Intl.DateTimeFormatOptions = {
hour: "2-digit",
minute: "2-digit",
};
export interface UpdateMetaProps {
payload: UpdateLectureMetaRequest;
options?: {
saveSeries?: boolean;
};
}
export class AdminLectureListProvider extends StreamableMapProvider<number, Lecture[]> {
protected async fetcher(courseId: number): Promise<Lecture[]> {
const result = await AdminLectureList.get(courseId);
return result.map((s) => {
s.hasAttachments = (s.files || []).some((f) => f.fileType === FileType.attachment);
s.videoSections = (s.videoSections ?? []).sort(videoSectionSort);
s.startDate = new Date(s.start);
s.startDateFormatted = s.startDate.toLocaleDateString("en-US", dateFormatOptions);
s.startTimeFormatted = s.startDate.toLocaleTimeString("en-US", timeFormatOptions);
s.endDate = new Date(s.end);
s.endDateFormatted = s.endDate.toLocaleDateString("en-US", dateFormatOptions);
s.endTimeFormatted = s.endDate.toLocaleTimeString("en-US", timeFormatOptions);
s.newCombinedVideo = null;
s.newPresentationVideo = null;
s.newCameraVideo = null;
return s;
});
}
async add(courseId: number, lecture: Lecture): Promise<void> {
await AdminLectureList.add(courseId, lecture);
await this.fetch(courseId, true);
await this.triggerUpdate(courseId);
}
async setPrivate(courseId: number, lectureId: number, isPrivate: boolean) {
await AdminLectureList.setPrivate(lectureId, isPrivate);
this.data[courseId] = (await this.getData(courseId)).map((s) => {
if (s.lectureId !== lectureId) {
return s;
}
return {
...s,
private: !s.private,
};
});
await this.triggerUpdate(courseId);
}
async delete(courseId: number, lectureIds: number[]) {
await AdminLectureList.delete(courseId, lectureIds);
this.data[courseId] = (await this.getData(courseId)).filter((s) => !lectureIds.includes(s.lectureId));
await this.triggerUpdate(courseId);
}
async deleteSeries(courseId: number, lectureId: number) {
await AdminLectureList.deleteSeries(courseId, lectureId);
const lectures = await this.getData(courseId);
const seriesIdentifier = lectures.find((l) => l.lectureId === lectureId)?.seriesIdentifier ?? null;
const lectureIds = lectures.filter((l) => l.seriesIdentifier === seriesIdentifier).map((l) => l.lectureId);
this.data[courseId] = lectures.filter((s) => !lectureIds.includes(s.lectureId));
await this.triggerUpdate(courseId);
}
async updateMeta(courseId: number, lectureId: number, props: UpdateMetaProps) {
const updateSeries = props?.options?.saveSeries === true;
const seriesIdentifier =
(await this.getData(courseId)).find((l) => l.lectureId === lectureId)?.seriesIdentifier ?? null;
await AdminLectureList.updateMetadata(courseId, lectureId, props.payload);
if (updateSeries) {
await AdminLectureList.saveSeriesMetadata(courseId, lectureId);
}
this.data[courseId] = (await this.getData(courseId)).map((s) => {
const isLecture = s.lectureId === lectureId;
const isInLectureSeries = s.seriesIdentifier === seriesIdentifier;
if (isLecture || (updateSeries && isInLectureSeries)) {
s = {
...s,
};
// Patch updated keys in local data
for (const requestKey in props.payload) {
const val = props.payload[requestKey];
if (val !== undefined) {
s[requestKey] = val;
}
}
}
return s;
});
await this.triggerUpdate(courseId);
}
async uploadAttachmentFile(courseId: number, lectureId: number, file: File) {
const res = await AdminLectureList.uploadAttachmentFile(courseId, lectureId, file);
const newFile = new LectureFile({
id: JSON.parse(res.responseText),
fileType: FileType.attachment,
friendlyName: file.name,
});
this.data[courseId] = (await this.getData(courseId)).map((s) => {
if (s.lectureId === lectureId) {
const updatedFiles = [...(s.files ?? []), newFile];
return {
...s,
files: updatedFiles,
hasAttachments: updatedFiles.some((f) => f.fileType === FileType.attachment),
};
}
return s;
});
await this.triggerUpdate(courseId);
}
async deleteAttachment(courseId: number, lectureId: number, attachmentId: number) {
await AdminLectureList.deleteAttachment(courseId, lectureId, attachmentId);
this.data[courseId] = (await this.getData(courseId)).map((s) => {
if (s.lectureId === lectureId) {
const updatedFiles = (s.files ?? []).filter((a) => a.id !== attachmentId);
return {
...s,
files: updatedFiles,
hasAttachments: updatedFiles.some((f) => f.fileType === FileType.attachment),
};
}
return s;
});
await this.triggerUpdate(courseId);
}
async addSections(courseId: number, lectureId: number, videoSections: VideoSection[]) {
const newSections = await AdminLectureList.addSections(lectureId, videoSections);
this.data[courseId] = (await this.getData(courseId)).map((s) => {
if (s.lectureId === lectureId) {
return {
...s,
videoSections: [...s.videoSections, ...newSections],
};
}
return s;
});
await this.triggerUpdate(courseId);
}
async updateSection(courseId: number, lectureId: number, videoSection: VideoSection) {
await AdminLectureList.updateSection(lectureId, videoSection);
this.data[courseId] = (await this.getData(courseId)).map((s) => {
if (s.lectureId === lectureId) {
return {
...s,
videoSections: [...s.videoSections.filter((a) => a.id !== videoSection.id), videoSection].sort(
videoSectionSort,
),
};
}
return s;
});
await this.triggerUpdate(courseId);
}
async deleteSection(courseId: number, lectureId: number, videoSectionId: number) {
await AdminLectureList.deleteSection(lectureId, videoSectionId);
this.data[courseId] = (await this.getData(courseId)).map((s) => {
if (s.lectureId === lectureId) {
return {
...s,
videoSections: [...s.videoSections.filter((a) => a.id !== videoSectionId)].sort(videoSectionSort),
};
}
return s;
});
await this.triggerUpdate(courseId);
}
async uploadVideo(
courseId: number,
lectureId: number,
videoType: string,
file: File,
listener: PostFormDataListener = {},
) {
await AdminLectureList.uploadVideo(courseId, lectureId, videoType, file, listener);
}
async uploadThumbnail(courseId: number, lectureId: number, file: File) {
await AdminLectureList.uploadThumbnailFile(courseId, lectureId, file);
await this.getData(courseId, true);
await this.triggerUpdate(courseId);
}
}