Skip to content

Commit

Permalink
feat(admin-ui): Support for editing Asset custom fields
Browse files Browse the repository at this point in the history
Relates to #684
  • Loading branch information
michaelbromley committed Feb 18, 2021
1 parent 60b6171 commit f109436
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@
<vdr-asset-preview
[asset]="entity$ | async"
[editable]="true"
[customFields]="customFields"
[customFieldsForm]="detailForm.get('customFields')"
(assetChange)="onAssetChange($event)"
></vdr-asset-preview>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { ChangeDetectionStrategy, Component, OnDestroy, OnInit } from '@angular/
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { Asset, BaseDetailComponent, GetAsset, LanguageCode } from '@vendure/admin-ui/core';
import {
Asset,
BaseDetailComponent,
CustomFieldConfig,
GetAsset,
LanguageCode,
} from '@vendure/admin-ui/core';
import { DataService, NotificationService, ServerConfigService } from '@vendure/admin-ui/core';

@Component({
Expand All @@ -13,6 +19,7 @@ import { DataService, NotificationService, ServerConfigService } from '@vendure/
})
export class AssetDetailComponent extends BaseDetailComponent<GetAsset.Asset> implements OnInit, OnDestroy {
detailForm = new FormGroup({});
customFields: CustomFieldConfig[];

constructor(
router: Router,
Expand All @@ -23,12 +30,16 @@ export class AssetDetailComponent extends BaseDetailComponent<GetAsset.Asset> im
private formBuilder: FormBuilder,
) {
super(route, router, serverConfigService, dataService);
this.customFields = this.getCustomFieldConfig('Asset');
}

ngOnInit() {
this.detailForm = new FormGroup({
name: new FormControl(''),
tags: new FormControl([]),
customFields: this.formBuilder.group(
this.customFields.reduce((hash, field) => ({ ...hash, [field.name]: '' }), {}),
),
});
this.init();
}
Expand All @@ -49,6 +60,7 @@ export class AssetDetailComponent extends BaseDetailComponent<GetAsset.Asset> im
id: this.id,
name: this.detailForm.value.name,
tags: this.detailForm.value.tags,
customFields: this.detailForm.value.customFields,
})
.subscribe(
() => {
Expand All @@ -65,5 +77,17 @@ export class AssetDetailComponent extends BaseDetailComponent<GetAsset.Asset> im
protected setFormValues(entity: GetAsset.Asset, languageCode: LanguageCode): void {
this.detailForm.get('name')?.setValue(entity.name);
this.detailForm.get('tags')?.setValue(entity.tags);
if (this.customFields.length) {
const customFieldsGroup = this.detailForm.get('customFields') as FormGroup;

for (const fieldDef of this.customFields) {
const key = fieldDef.name;
const value = (entity as any).customFields[key];
const control = customFieldsGroup.get(key);
if (control) {
control.patchValue(value);
}
}
}
}
}
22 changes: 22 additions & 0 deletions packages/admin-ui/src/lib/core/src/common/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7513,6 +7513,27 @@ export type GetServerConfigQuery = { globalSettings: (
) | (
{ __typename?: 'RelationCustomFieldConfig' }
& CustomFields_RelationCustomFieldConfig_Fragment
)>, Asset: Array<(
{ __typename?: 'StringCustomFieldConfig' }
& CustomFields_StringCustomFieldConfig_Fragment
) | (
{ __typename?: 'LocaleStringCustomFieldConfig' }
& CustomFields_LocaleStringCustomFieldConfig_Fragment
) | (
{ __typename?: 'IntCustomFieldConfig' }
& CustomFields_IntCustomFieldConfig_Fragment
) | (
{ __typename?: 'FloatCustomFieldConfig' }
& CustomFields_FloatCustomFieldConfig_Fragment
) | (
{ __typename?: 'BooleanCustomFieldConfig' }
& CustomFields_BooleanCustomFieldConfig_Fragment
) | (
{ __typename?: 'DateTimeCustomFieldConfig' }
& CustomFields_DateTimeCustomFieldConfig_Fragment
) | (
{ __typename?: 'RelationCustomFieldConfig' }
& CustomFields_RelationCustomFieldConfig_Fragment
)>, Channel: Array<(
{ __typename?: 'StringCustomFieldConfig' }
& CustomFields_StringCustomFieldConfig_Fragment
Expand Down Expand Up @@ -9487,6 +9508,7 @@ export namespace GetServerConfig {
export type CustomFieldConfig = (NonNullable<(NonNullable<(NonNullable<GetServerConfigQuery['globalSettings']>)['serverConfig']>)['customFieldConfig']>);
export type Address = NonNullable<(NonNullable<(NonNullable<(NonNullable<(NonNullable<GetServerConfigQuery['globalSettings']>)['serverConfig']>)['customFieldConfig']>)['Address']>)[number]>;
export type Administrator = NonNullable<(NonNullable<(NonNullable<(NonNullable<(NonNullable<GetServerConfigQuery['globalSettings']>)['serverConfig']>)['customFieldConfig']>)['Administrator']>)[number]>;
export type Asset = NonNullable<(NonNullable<(NonNullable<(NonNullable<(NonNullable<GetServerConfigQuery['globalSettings']>)['serverConfig']>)['customFieldConfig']>)['Asset']>)[number]>;
export type Channel = NonNullable<(NonNullable<(NonNullable<(NonNullable<(NonNullable<GetServerConfigQuery['globalSettings']>)['serverConfig']>)['customFieldConfig']>)['Channel']>)[number]>;
export type Collection = NonNullable<(NonNullable<(NonNullable<(NonNullable<(NonNullable<GetServerConfigQuery['globalSettings']>)['serverConfig']>)['customFieldConfig']>)['Collection']>)[number]>;
export type Customer = NonNullable<(NonNullable<(NonNullable<(NonNullable<(NonNullable<GetServerConfigQuery['globalSettings']>)['serverConfig']>)['customFieldConfig']>)['Customer']>)[number]>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,9 @@ export const GET_SERVER_CONFIG = gql`
Administrator {
...CustomFields
}
Asset {
...CustomFields
}
Channel {
...CustomFields
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<label>{{ 'common.custom-fields' | translate }}</label>
<ng-container *ngFor="let customField of customFields">
<vdr-custom-field-control
entityName="Facet"
entityName="Address"
[customFieldsFormGroup]="customFieldsGroup"
[customField]="customField"
></vdr-custom-field-control>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,30 @@
<ng-container *ngIf="editable">
<vdr-tag-selector formControlName="tags"></vdr-tag-selector>
<button class="btn btn-link btn-sm" (click)="manageTags()">
<clr-icon shape="tags"></clr-icon> {{ 'common.manage-tags' | translate }}
<clr-icon shape="tags"></clr-icon>
{{ 'common.manage-tags' | translate }}
</button>
</ng-container>
<div *ngIf="!editable">
<vdr-chip *ngFor="let tag of asset.tags" [colorFrom]="tag.value"
><clr-icon shape="tag" class="mr2"></clr-icon> {{ tag.value }}</vdr-chip
<vdr-chip *ngFor="let tag of asset.tags" [colorFrom]="tag.value">
<clr-icon shape="tag" class="mr2"></clr-icon>
{{ tag.value }}</vdr-chip
>
</div>
</vdr-labeled-data>
</form>
<section *ngIf="customFields.length">
<label>{{ 'common.custom-fields' | translate }}</label>
<ng-container *ngFor="let customField of customFields">
<vdr-custom-field-control
*ngIf="customFieldIsSet(customField.name)"
entityName="Asset"
[compact]="true"
[customFieldsFormGroup]="customFieldsForm"
[customField]="customField"
></vdr-custom-field-control>
</ng-container>
</section>
<div class="flex-spacer"></div>
<div class="preview-select">
<clr-select-container>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { fromEvent, Subscription } from 'rxjs';
import { debounceTime } from 'rxjs/operators';

import { GetAsset, GetAssetList, UpdateAssetInput } from '../../../common/generated-types';
import { CustomFieldConfig, GetAsset, GetAssetList, UpdateAssetInput } from '../../../common/generated-types';
import { DataService } from '../../../data/providers/data.service';
import { ModalService } from '../../../providers/modal/modal.service';
import { NotificationService } from '../../../providers/notification/notification.service';
Expand All @@ -34,6 +34,8 @@ type AssetLike = GetAssetList.Items | GetAsset.Asset;
export class AssetPreviewComponent implements OnInit, OnDestroy {
@Input() asset: AssetLike;
@Input() editable = false;
@Input() customFields: CustomFieldConfig[] = [];
@Input() customFieldsForm: FormGroup | undefined;
@Output() assetChange = new EventEmitter<Omit<UpdateAssetInput, 'focalPoint'>>();
@Output() editClick = new EventEmitter();

Expand Down Expand Up @@ -96,6 +98,10 @@ export class AssetPreviewComponent implements OnInit, OnDestroy {
}
}

customFieldIsSet(name: string): boolean {
return !!this.customFieldsForm?.get([name]);
}

getSourceFileName(): string {
const parts = this.asset.source.split('/');
return parts[parts.length - 1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<ng-container *ngFor="let customField of customFields">
<vdr-custom-field-control
*ngIf="customFieldIsSet(customField.name)"
entityName="GlobalSettings"
entityName="Administrator"
[customFieldsFormGroup]="detailForm.get('customFields')"
[customField]="customField"
></vdr-custom-field-control>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
<ng-container *ngFor="let customField of customFields">
<vdr-custom-field-control
*ngIf="customFieldIsSet(customField.name)"
entityName="GlobalSettings"
entityName="Channel"
[customFieldsFormGroup]="detailForm.get('customFields')"
[customField]="customField"
></vdr-custom-field-control>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<ng-container *ngFor="let customField of customFields">
<vdr-custom-field-control
*ngIf="customFieldIsSet(customField.name)"
entityName="GlobalSettings"
entityName="Administrator"
[customFieldsFormGroup]="detailForm.get('customFields')"
[customField]="customField"
></vdr-custom-field-control>
Expand Down

0 comments on commit f109436

Please sign in to comment.