-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
SongListStore.ts
340 lines (304 loc) · 11 KB
/
SongListStore.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import CommentContract from '@DataContracts/CommentContract';
import PagingProperties from '@DataContracts/PagingPropertiesContract';
import PartialFindResultContract from '@DataContracts/PartialFindResultContract';
import SongInListContract from '@DataContracts/Song/SongInListContract';
import TagSelectionContract from '@DataContracts/Tag/TagSelectionContract';
import TagUsageForApiContract from '@DataContracts/Tag/TagUsageForApiContract';
import {
SongOptionalField,
SongOptionalFields,
} from '@Models/EntryOptionalFields';
import EntryType from '@Models/EntryType';
import LoginManager from '@Models/LoginManager';
import PVServiceIcons from '@Models/PVServiceIcons';
import SongType from '@Models/Songs/SongType';
import ArtistRepository from '@Repositories/ArtistRepository';
import SongListRepository from '@Repositories/SongListRepository';
import SongRepository from '@Repositories/SongRepository';
import TagRepository from '@Repositories/TagRepository';
import UserRepository from '@Repositories/UserRepository';
import EntryUrlMapper from '@Shared/EntryUrlMapper';
import GlobalValues from '@Shared/GlobalValues';
import UrlMapper from '@Shared/UrlMapper';
import EditableCommentsStore from '@Stores/EditableCommentsStore';
import PVPlayerStore from '@Stores/PVs/PVPlayerStore';
import PVPlayersFactory from '@Stores/PVs/PVPlayersFactory';
import AdvancedSearchFilter from '@Stores/Search/AdvancedSearchFilter';
import AdvancedSearchFilters from '@Stores/Search/AdvancedSearchFilters';
import ArtistFilters from '@Stores/Search/ArtistFilters';
import { ISongSearchItem } from '@Stores/Search/SongSearchStore';
import TagFilter from '@Stores/Search/TagFilter';
import TagFilters from '@Stores/Search/TagFilters';
import ServerSidePagingStore from '@Stores/ServerSidePagingStore';
import PlayListRepositoryForSongListAdapter, {
ISongListStore,
} from '@Stores/Song/PlayList/PlayListRepositoryForSongListAdapter';
import PlayListStore from '@Stores/Song/PlayList/PlayListStore';
import SongWithPreviewStore from '@Stores/Song/SongWithPreviewStore';
import TagListStore from '@Stores/Tag/TagListStore';
import TagsEditStore from '@Stores/Tag/TagsEditStore';
import { StoreWithPagination } from '@vocadb/route-sphere';
import Ajv, { JSONSchemaType } from 'ajv';
import _ from 'lodash';
import {
action,
computed,
makeObservable,
observable,
reaction,
runInAction,
} from 'mobx';
const loginManager = new LoginManager(vdb.values);
interface SongListRouteParams {
advancedFilters?: AdvancedSearchFilter[];
artistId?: number[];
artistParticipationStatus?: string /* TODO: enum */;
childTags?: boolean;
childVoicebanks?: boolean;
page?: number;
pageSize?: number;
playlistMode?: boolean;
query?: string;
sort?: string /* TODO: enum */;
songType?: SongType;
tagId?: number[];
}
// TODO: Use single Ajv instance. See https://ajv.js.org/guide/managing-schemas.html.
const ajv = new Ajv({ coerceTypes: true });
// TODO: Make sure that we compile schemas only once and re-use compiled validation functions. See https://ajv.js.org/guide/getting-started.html.
const schema: JSONSchemaType<SongListRouteParams> = require('./SongListRouteParams.schema');
const validate = ajv.compile(schema);
export default class SongListStore
implements ISongListStore, StoreWithPagination<SongListRouteParams> {
public readonly advancedFilters = new AdvancedSearchFilters();
public readonly artistFilters: ArtistFilters;
public readonly comments: EditableCommentsStore;
@observable public loading = true; // Currently loading for data
@observable public page: (SongInListContract & {
song: ISongSearchItem;
})[] = []; // Current page of items
public readonly paging = new ServerSidePagingStore(20); // Paging view model
public pauseNotifications = false;
@observable public playlistMode = false;
public readonly playlistStore: PlayListStore;
public readonly pvPlayerStore: PVPlayerStore;
public readonly pvServiceIcons: PVServiceIcons;
@observable public query = '';
@observable public showAdvancedFilters = false;
@observable public showTags = false;
@observable public sort = '' /* TODO: enum */;
@observable public songType = SongType.Unspecified;
public readonly tagsEditStore: TagsEditStore;
public readonly tagFilters: TagFilters;
public readonly tagUsages: TagListStore;
public constructor(
private readonly values: GlobalValues,
urlMapper: UrlMapper,
private readonly songListRepo: SongListRepository,
private readonly songRepo: SongRepository,
tagRepo: TagRepository,
private readonly userRepo: UserRepository,
artistRepo: ArtistRepository,
latestComments: CommentContract[],
private readonly listId: number,
tagUsages: TagUsageForApiContract[],
pvPlayersFactory: PVPlayersFactory,
canDeleteAllComments: boolean,
) {
makeObservable(this);
this.artistFilters = new ArtistFilters(values, artistRepo);
this.comments = new EditableCommentsStore(
loginManager,
songListRepo.getComments({}),
listId,
canDeleteAllComments,
canDeleteAllComments,
false,
latestComments,
true,
);
// TODO
this.pvPlayerStore = new PVPlayerStore(
values,
songRepo,
userRepo,
pvPlayersFactory,
);
const playListRepoAdapter = new PlayListRepositoryForSongListAdapter(
songListRepo,
listId,
this,
);
this.playlistStore = new PlayListStore(
values,
urlMapper,
playListRepoAdapter,
this.pvPlayerStore,
);
this.pvServiceIcons = new PVServiceIcons(urlMapper);
this.tagsEditStore = new TagsEditStore(
{
getTagSelections: (): Promise<TagSelectionContract[]> =>
userRepo.getSongListTagSelections({ songListId: this.listId }),
saveTagSelections: (tags): Promise<void> =>
userRepo
.updateSongListTags({ songListId: this.listId, tags: tags })
.then(this.tagUsages.updateTagUsages),
},
EntryType.SongList,
);
this.tagFilters = new TagFilters(values, tagRepo);
this.tagUsages = new TagListStore(tagUsages);
reaction(() => this.showTags, this.updateResultsWithTotalCount);
}
@computed public get childTags(): boolean {
return this.tagFilters.childTags;
}
public set childTags(value: boolean) {
this.tagFilters.childTags = value;
}
@computed public get tags(): TagFilter[] {
return this.tagFilters.tags;
}
public set tags(value: TagFilter[]) {
this.tagFilters.tags = value;
}
@computed public get tagIds(): number[] {
return _.map(this.tags, (t) => t.id);
}
public set tagIds(value: number[]) {
// OPTIMIZE
this.tagFilters.tags = [];
this.tagFilters.addTags(value);
}
public mapTagUrl = (tagUsage: TagUsageForApiContract): string => {
return EntryUrlMapper.details_tag(tagUsage.tag.id, tagUsage.tag.urlSlug);
};
public popState = false;
public clearResultsByQueryKeys: (keyof SongListRouteParams)[] = [
'advancedFilters',
'artistId',
'artistParticipationStatus',
'childTags',
'childVoicebanks',
'pageSize',
'songType',
'tagId',
'sort',
'playlistMode',
'query',
];
@computed.struct public get routeParams(): SongListRouteParams {
return {
advancedFilters: this.advancedFilters.filters.map((filter) => ({
description: filter.description,
filterType: filter.filterType,
negate: filter.negate,
param: filter.param,
})),
artistId: this.artistFilters.artistIds,
artistParticipationStatus: this.artistFilters.artistParticipationStatus,
childTags: this.childTags,
childVoicebanks: this.artistFilters.childVoicebanks,
page: this.paging.page,
pageSize: this.paging.pageSize,
playlistMode: this.playlistMode,
query: this.query,
sort: this.sort,
songType: this.songType,
tagId: this.tagIds,
};
}
public set routeParams(value: SongListRouteParams) {
this.advancedFilters.filters = value.advancedFilters ?? [];
this.artistFilters.artistIds = ([] as number[]).concat(
value.artistId ?? [],
);
this.artistFilters.artistParticipationStatus =
value.artistParticipationStatus ?? 'Everything';
this.childTags = value.childTags ?? false;
this.artistFilters.childVoicebanks = value.childVoicebanks ?? false;
this.paging.page = value.page ?? 1;
this.paging.pageSize = value.pageSize ?? 20;
this.playlistMode = value.playlistMode ?? false;
this.query = value.query ?? '';
this.sort = value.sort ?? '';
this.songType = value.songType ?? SongType.Unspecified;
this.tagIds = ([] as number[]).concat(value.tagId ?? []);
}
public validateRouteParams = (data: any): data is SongListRouteParams => {
return validate(data);
};
private loadResults = (
pagingProperties: PagingProperties,
): Promise<PartialFindResultContract<SongInListContract>> => {
if (this.playlistMode) {
this.playlistStore.updateResultsWithTotalCount();
return Promise.resolve({ items: [], totalCount: 0 });
} else {
const fields = [
SongOptionalField.AdditionalNames,
SongOptionalField.MainPicture,
];
if (this.showTags) fields.push(SongOptionalField.Tags);
return this.songListRepo.getSongs({
listId: this.listId,
query: this.query,
songTypes:
this.songType !== SongType.Unspecified ? [this.songType] : undefined,
tagIds: this.tagIds,
childTags: this.childTags,
artistIds: this.artistFilters.artistIds,
artistParticipationStatus: this.artistFilters.artistParticipationStatus,
childVoicebanks: this.artistFilters.childVoicebanks,
advancedFilters: this.advancedFilters.filters,
pvServices: undefined,
paging: pagingProperties,
fields: new SongOptionalFields(fields),
sort: this.sort,
lang: this.values.languagePreference,
});
}
};
@action public updateResults = async (
clearResults: boolean,
): Promise<void> => {
// Disable duplicate updates
if (this.pauseNotifications) return;
this.pauseNotifications = true;
this.loading = true;
const pagingProperties = this.paging.getPagingProperties(clearResults);
const result = await this.loadResults(pagingProperties);
_.each(result.items, (item) => {
const song = item.song;
const songAny: any = song;
if (song.pvServices && song.pvServices !== 'Nothing') {
songAny.previewStore = new SongWithPreviewStore(
this.songRepo,
this.userRepo,
song.id,
song.pvServices,
);
// TODO: songAny.previewViewModel.ratingComplete = ui.showThankYouForRatingMessage;
} else {
songAny.previewStore = undefined;
}
});
this.pauseNotifications = false;
runInAction(() => {
if (pagingProperties.getTotalCount)
this.paging.totalItems = result.totalCount;
this.page = result.items;
this.loading = false;
});
};
public updateResultsWithTotalCount = (): Promise<void> => {
return this.updateResults(true);
};
public updateResultsWithoutTotalCount = (): Promise<void> => {
return this.updateResults(false);
};
public onClearResults = (): void => {
this.paging.goToFirstPage();
};
}