-
-
Notifications
You must be signed in to change notification settings - Fork 932
/
collection-detail.component.ts
354 lines (332 loc) · 12.6 KB
/
collection-detail.component.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
OnDestroy,
OnInit,
ViewChild,
} from '@angular/core';
import { FormBuilder, UntypedFormArray, UntypedFormControl, Validators } from '@angular/forms';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import {
Asset,
COLLECTION_FRAGMENT,
CollectionDetailQueryDocument,
CollectionFragment,
ConfigurableOperation,
ConfigurableOperationDefinition,
ConfigurableOperationInput,
CreateCollectionInput,
createUpdatedTranslatable,
DataService,
encodeConfigArgValue,
findTranslation,
getConfigArgValue,
getCustomFieldsDefaults,
LanguageCode,
LocalStorageService,
ModalService,
NotificationService,
Permission,
TypedBaseDetailComponent,
unicodePatternValidator,
UpdateCollectionInput,
} from '@vendure/admin-ui/core';
import { normalizeString } from '@vendure/common/lib/normalize-string';
import { gql } from 'apollo-angular';
import { combineLatest, merge, Observable, of, Subject } from 'rxjs';
import { debounceTime, distinctUntilChanged, filter, map, mergeMap, switchMap, take } from 'rxjs/operators';
import { CollectionContentsComponent } from '../collection-contents/collection-contents.component';
export const COLLECTION_DETAIL_QUERY = gql`
query CollectionDetailQuery($id: ID!) {
collection(id: $id) {
...Collection
}
}
${COLLECTION_FRAGMENT}
`;
@Component({
selector: 'vdr-collection-detail',
templateUrl: './collection-detail.component.html',
styleUrls: ['./collection-detail.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CollectionDetailComponent
extends TypedBaseDetailComponent<typeof CollectionDetailQueryDocument, 'collection'>
implements OnInit, OnDestroy
{
customFields = this.getCustomFieldConfig('Collection');
detailForm = this.formBuilder.group({
name: ['', Validators.required],
slug: ['', unicodePatternValidator(/^[\p{Letter}0-9._-]+$/)],
description: '',
visible: false,
inheritFilters: true,
filters: this.formBuilder.array([]),
customFields: this.formBuilder.group(getCustomFieldsDefaults(this.customFields)),
});
assetChanges: { assets?: Asset[]; featuredAsset?: Asset } = {};
filters: ConfigurableOperation[] = [];
allFilters: ConfigurableOperationDefinition[] = [];
updatedFilters$: Observable<ConfigurableOperationInput[]>;
inheritFilters$: Observable<boolean>;
livePreview = false;
parentId$: Observable<string | undefined>;
readonly updatePermission = [Permission.UpdateCatalog, Permission.UpdateCollection];
private filterRemoved$ = new Subject<void>();
@ViewChild('collectionContents') contentsComponent: CollectionContentsComponent;
constructor(
private changeDetector: ChangeDetectorRef,
protected dataService: DataService,
private formBuilder: FormBuilder,
private notificationService: NotificationService,
private modalService: ModalService,
private localStorageService: LocalStorageService,
) {
super();
this.livePreview = this.localStorageService.get('livePreviewCollectionContents') ?? false;
}
ngOnInit() {
this.init();
this.dataService.collection.getCollectionFilters().single$.subscribe(res => {
this.allFilters = res.collectionFilters;
});
const filtersFormArray = this.detailForm.get('filters') as UntypedFormArray;
const inheritFiltersControl = this.detailForm.get('inheritFilters') as UntypedFormControl;
this.inheritFilters$ = inheritFiltersControl.valueChanges.pipe(distinctUntilChanged());
this.updatedFilters$ = merge(filtersFormArray.statusChanges, this.filterRemoved$).pipe(
debounceTime(200),
filter(() => filtersFormArray.touched),
map(() =>
this.mapOperationsToInputs(this.filters, filtersFormArray.value).filter(_filter => {
// ensure all the arguments have valid values. E.g. a newly-added
// filter will not yet have valid values
for (const arg of _filter.arguments) {
if (arg.value === '') {
return false;
}
}
return true;
}),
),
);
this.parentId$ = this.route.paramMap.pipe(
map(pm => pm.get('parentId') || undefined),
switchMap(parentId => {
if (parentId) {
return of(parentId);
} else {
return this.entity$.pipe(map(collection => collection.parent?.id));
}
}),
);
}
ngOnDestroy() {
this.destroy();
}
getFilterDefinition(_filter: ConfigurableOperation): ConfigurableOperationDefinition | undefined {
return this.allFilters.find(f => f.code === _filter.code);
}
assetsChanged(): boolean {
return !!Object.values(this.assetChanges).length;
}
/**
* If creating a new Collection, automatically generate the slug based on the collection name.
*/
updateSlug(nameValue: string) {
const slugControl = this.detailForm.get(['slug']);
const currentTranslation = this.entity ? findTranslation(this.entity, this.languageCode) : undefined;
const currentSlugIsEmpty = !currentTranslation || !currentTranslation.slug;
if (slugControl && slugControl.pristine && currentSlugIsEmpty) {
slugControl.setValue(normalizeString(`${nameValue}`, '-'));
}
}
addFilter(collectionFilter: ConfigurableOperation) {
const filtersArray = this.detailForm.get('filters') as UntypedFormArray;
const argsHash = collectionFilter.args.reduce(
(output, arg) => ({
...output,
[arg.name]: getConfigArgValue(arg.value),
}),
{},
);
filtersArray.push(
this.formBuilder.control({
code: collectionFilter.code,
args: argsHash,
}),
);
this.filters.push({
code: collectionFilter.code,
args: collectionFilter.args.map(a => ({ name: a.name, value: getConfigArgValue(a.value) })),
});
}
removeFilter(index: number) {
const filtersArray = this.detailForm.get('filters') as UntypedFormArray;
if (index !== -1) {
filtersArray.removeAt(index);
filtersArray.markAsDirty();
filtersArray.markAsTouched();
this.filters.splice(index, 1);
this.filterRemoved$.next();
}
}
create() {
if (!this.detailForm.dirty) {
return;
}
const input = this.getUpdatedCollection(
{
id: '',
createdAt: '',
updatedAt: '',
languageCode: this.languageCode,
name: '',
slug: '',
isPrivate: false,
breadcrumbs: [],
description: '',
featuredAsset: null,
assets: [],
translations: [],
inheritFilters: true,
filters: [],
parent: {} as any,
children: null,
},
this.detailForm,
this.languageCode,
) as CreateCollectionInput;
const parentId = this.route.snapshot.paramMap.get('parentId');
if (parentId) {
input.parentId = parentId;
}
this.dataService.collection.createCollection(input).subscribe(
data => {
this.notificationService.success(_('common.notify-create-success'), {
entity: 'Collection',
});
this.assetChanges = {};
this.detailForm.markAsPristine();
this.changeDetector.markForCheck();
this.router.navigate(['../', data.createCollection.id], { relativeTo: this.route });
},
err => {
this.notificationService.error(_('common.notify-create-error'), {
entity: 'Collection',
});
},
);
}
save() {
combineLatest(this.entity$, this.languageCode$)
.pipe(
take(1),
mergeMap(([category, languageCode]) => {
const input = this.getUpdatedCollection(
category,
this.detailForm,
languageCode,
) as UpdateCollectionInput;
return this.dataService.collection.updateCollection(input);
}),
)
.subscribe(
() => {
this.assetChanges = {};
this.detailForm.markAsPristine();
this.changeDetector.markForCheck();
this.notificationService.success(_('common.notify-update-success'), {
entity: 'Collection',
});
this.contentsComponent.refresh();
},
err => {
this.notificationService.error(_('common.notify-update-error'), {
entity: 'Collection',
});
},
);
}
canDeactivate(): boolean {
return super.canDeactivate() && !this.assetChanges.assets && !this.assetChanges.featuredAsset;
}
toggleLivePreview() {
this.livePreview = !this.livePreview;
this.localStorageService.set('livePreviewCollectionContents', this.livePreview);
}
trackByFn(index: number, item: ConfigurableOperation) {
return JSON.stringify(item);
}
/**
* Sets the values of the form on changes to the category or current language.
*/
protected setFormValues(entity: CollectionFragment, languageCode: LanguageCode) {
const currentTranslation = findTranslation(entity, languageCode);
this.detailForm.patchValue({
name: currentTranslation ? currentTranslation.name : '',
slug: currentTranslation ? currentTranslation.slug : '',
description: currentTranslation ? currentTranslation.description : '',
visible: !entity.isPrivate,
inheritFilters: entity.inheritFilters,
});
const formArray = this.detailForm.get('filters') as UntypedFormArray;
if (formArray.length !== entity.filters.length) {
formArray.clear();
this.filters = [];
entity.filters.forEach(f => this.addFilter(f));
}
if (this.customFields.length) {
this.setCustomFieldFormValues(
this.customFields,
this.detailForm.get(['customFields']),
entity,
currentTranslation,
);
}
}
/**
* Given a category and the value of the form, this method creates an updated copy of the category which
* can then be persisted to the API.
*/
private getUpdatedCollection(
category: CollectionFragment,
form: typeof this.detailForm,
languageCode: LanguageCode,
): CreateCollectionInput | UpdateCollectionInput {
const updatedCategory = createUpdatedTranslatable({
translatable: category,
updatedFields: form.value,
customFieldConfig: this.customFields,
languageCode,
defaultTranslation: {
languageCode,
name: category.name || '',
slug: category.slug || '',
description: category.description || '',
},
});
return {
...updatedCategory,
assetIds: this.assetChanges.assets?.map(a => a.id),
featuredAssetId: this.assetChanges.featuredAsset?.id,
isPrivate: !form.value.visible,
filters: this.mapOperationsToInputs(this.filters, this.detailForm.value.filters),
};
}
/**
* Maps an array of conditions or actions to the input format expected by the GraphQL API.
*/
private mapOperationsToInputs(
operations: ConfigurableOperation[],
formValueOperations: any,
): ConfigurableOperationInput[] {
return operations.map((o, i) => ({
code: o.code,
arguments: Object.entries(formValueOperations[i].args).map(([name, value], j) => ({
name,
value: encodeConfigArgValue(value),
})),
}));
}
}