-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdocument.ts
More file actions
263 lines (227 loc) · 7.49 KB
/
document.ts
File metadata and controls
263 lines (227 loc) · 7.49 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import type ScriptVersion from '@tdev-models/documents/ScriptVersion';
import type TaskState from '@tdev-models/documents/TaskState';
import type String from '@tdev-models/documents/String';
import api from './base';
import { AxiosPromise } from 'axios';
import QuillV2 from '@tdev-models/documents/QuillV2';
import { Delta } from 'quill/core';
import Solution from '@tdev-models/documents/Solution';
import Directory from '@tdev-models/documents/FileSystem/Directory';
import File from '@tdev-models/documents/FileSystem/File';
import Restricted from '@tdev-models/documents/Restricted';
import MdxComment from '@tdev-models/documents/MdxComment';
import { Color } from '@tdev-components/shared/Colors';
import CmsText from '@tdev-models/documents/CmsText';
import DynamicDocumentRoots from '@tdev-models/documents/DynamicDocumentRoots';
import ProgressState from '@tdev-models/documents/ProgressState';
import type DocumentStore from '@tdev-stores/DocumentStore';
import iDocumentContainer from '@tdev-models/iDocumentContainer';
import iViewStore from '@tdev-stores/ViewStores/iViewStore';
import Code from '@tdev-models/documents/Code';
import ChoiceAnswer, {
ChoiceAnswerChoices,
ChoiceAnswerOptionOrders,
ChoiceAnswerQuestionOrder
} from '@tdev-models/documents/ChoiceAnswer';
export enum Access {
RO_DocumentRoot = 'RO_DocumentRoot',
RW_DocumentRoot = 'RW_DocumentRoot',
None_DocumentRoot = 'None_DocumentRoot',
RO_StudentGroup = 'RO_StudentGroup',
RW_StudentGroup = 'RW_StudentGroup',
None_StudentGroup = 'None_StudentGroup',
RO_User = 'RO_User',
RW_User = 'RW_User',
None_User = 'None_User'
}
export interface ScriptVersionData {
code: string;
pasted?: boolean;
}
export interface StringData {
text: string;
}
export interface ChoiceAnswerData {
choices: ChoiceAnswerChoices;
optionOrders: ChoiceAnswerOptionOrders;
questionOrder: ChoiceAnswerQuestionOrder | null;
}
export interface QuillV2Data {
delta: Delta;
}
export interface CodeData {
code: string;
}
export interface SolutionData {
/** no content needed */
}
export interface RestrictedData {
/** no content needed */
}
export interface CmsTextData {
text: string;
}
export interface DirData {
name: string;
isOpen: boolean;
}
export interface FileData {
name: string;
isOpen: boolean;
}
export type StateType =
| 'checked'
| 'question'
| 'unset'
| 'star'
| 'star-half'
| 'star-empty'
| 'clock-check'
| 'progress-check';
export interface TaskStateData {
state: StateType;
}
export interface ProgressStateData {
progress: number;
}
export interface MdxCommentData {
type: string;
nr: number;
commentNr: number;
isOpen: boolean;
color: Color;
}
export interface DynamicDocumentRootsData<T extends ContainerType> {
// document type of the container document
containerType: T;
documentRootIds: string[];
}
export interface ViewStoreTypeMapping {
['_view_store>']: typeof iViewStore; // placeholder to avoid empty interface error
}
export type ViewStoreType = keyof ViewStoreTypeMapping;
export type ViewStore = ViewStoreTypeMapping[ViewStoreType];
export interface ContainerTypeDataMapping {
['_container_placeholder_']: { name: string }; // placeholder to avoid empty interface error
}
export interface TypeDataMapping extends ContainerTypeDataMapping {
['task_state']: TaskStateData;
['progress_state']: ProgressStateData;
['code']: CodeData;
// TODO: rename to `code_version`?
['script_version']: ScriptVersionData;
['string']: StringData;
['choice_answer']: ChoiceAnswerData;
['quill_v2']: QuillV2Data;
['solution']: SolutionData;
['dir']: DirData;
['file']: FileData;
['mdx_comment']: MdxCommentData;
['restricted']: RestrictedData;
['cms_text']: CmsTextData;
['dynamic_document_roots']: DynamicDocumentRootsData<any>;
// Add more mappings as needed
}
export type ContainerType = keyof ContainerTypeDataMapping;
type KeysWithCode<T> = {
[K in keyof T]: 'code' extends keyof T[K] ? K : never;
}[keyof Omit<T, 'script_version'>];
export type CodeType = KeysWithCode<TypeDataMapping>;
export interface ContainerTypeModelMapping {
['_container_placeholder_']: iDocumentContainer<ContainerType>; // placeholder to avoid empty interface error
}
export interface TypeModelMapping extends ContainerTypeModelMapping {
['task_state']: TaskState;
['progress_state']: ProgressState;
['code']: Code;
// TODO: rename to `code_version`?
['script_version']: ScriptVersion;
['string']: String;
['choice_answer']: ChoiceAnswer;
['quill_v2']: QuillV2;
['solution']: Solution;
['dir']: Directory;
['file']: File;
['mdx_comment']: MdxComment;
['restricted']: Restricted;
['cms_text']: CmsText;
['dynamic_document_roots']: DynamicDocumentRoots<any>;
/**
* Add more mappings as needed
* TODO: implement the mapping in DocumentRoot.ts
* @see DocumentRoot
* @link file://../../src/stores/DocumentStore.ts#CreateDocumentModel
*/
}
export type ContainerModelType = ContainerTypeModelMapping[ContainerType];
export type DocumentType = keyof TypeModelMapping;
export type DocumentModelType = TypeModelMapping[DocumentType];
/**
* Document types that can be edited by admins ON BEHALF OF other users.
* This should not be the default case for most documents - but things like CMS texts
* should be editeable by admins only.
*/
export const ADMIN_EDITABLE_DOCUMENTS: DocumentType[] = ['cms_text'] as const;
export interface Document<Type extends DocumentType> {
id: string;
type: Type;
authorId: string;
parentId: string | null | undefined;
documentRootId: string;
data: TypeDataMapping[Type];
createdAt: string;
updatedAt: string;
}
export type Factory<Type extends DocumentType = DocumentType> = (
data: Document<Type>,
store: DocumentStore
) => TypeModelMapping[Type];
export function find<Type extends DocumentType>(
id: string,
signal: AbortSignal
): AxiosPromise<Document<Type>> {
return api.get(`/documents/${id}`, { signal });
}
export function create<Type extends DocumentType>(
data: Partial<Document<Type>>,
onBehalfOf: boolean,
isMain: boolean,
signal: AbortSignal
): AxiosPromise<Document<Type>> {
const queryParams: string[] = [];
if (onBehalfOf) {
queryParams.push('onBehalfOf=true');
}
if (isMain) {
queryParams.push('uniqueMain=true');
}
return api.post(`/documents${queryParams.length > 0 ? `?${queryParams.join('&')}` : ''}`, data, {
signal
});
}
export function remove(id: string, signal: AbortSignal): AxiosPromise<void> {
return api.delete(`/documents/${id}`, { signal });
}
export function update<Type extends DocumentType>(
id: string,
data: TypeDataMapping[Type],
onBehalfOf: boolean,
signal: AbortSignal
): AxiosPromise<Document<Type>> {
return api.put(`/documents/${id}${onBehalfOf ? '?onBehalfOf=true' : ''}`, { data }, { signal });
}
/**
* TODO: would it be better to only grab documents from a specific student group?
*/
export function allDocuments(documentRootIds: string[], signal: AbortSignal): AxiosPromise<Document<any>[]> {
return api.get(`/documents?${documentRootIds.map((id) => `rids=${id}`).join('&')}`, {
signal
});
}
export function linkTo<Type extends DocumentType>(
id: string,
linkToId: string,
signal: AbortSignal
): AxiosPromise<Document<Type>> {
return api.put(`/documents/${id}/linkTo/${linkToId}`, { signal });
}