Skip to content

Commit

Permalink
fix(admin-ui): Allow new FacetValues to be added
Browse files Browse the repository at this point in the history
Fixes #222
  • Loading branch information
michaelbromley committed Dec 6, 2019
1 parent b476dcb commit 337762e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
<tbody>
<tr
class="facet-value"
*ngFor="let value of values$ | async; let i = index"
*ngFor="let value of values; let i = index"
[formGroupName]="i"
>
<td class="align-middle">
Expand Down Expand Up @@ -141,7 +141,7 @@
<button
type="button"
class="delete-button"
(click)="deleteFacetValue(facet.values[i].id)"
(click)="deleteFacetValue(facet.values[i]?.id, i)"
[disabled]="!('UpdateCatalog' | hasPermission)"
vdrDropdownItem
>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy } from '@angular/core';
import { FormArray, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { combineLatest, EMPTY, forkJoin, Observable } from 'rxjs';
Expand Down Expand Up @@ -30,13 +30,11 @@ import { ModalService } from '../../../shared/providers/modal/modal.service';
styleUrls: ['./facet-detail.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class FacetDetailComponent extends BaseDetailComponent<FacetWithValues.Fragment>
implements OnInit, OnDestroy {
facet$: Observable<FacetWithValues.Fragment>;
values$: Observable<FacetWithValues.Values[]>;
export class FacetDetailComponent extends BaseDetailComponent<FacetWithValues.Fragment> implements OnDestroy {
customFields: CustomFieldConfig[];
customValueFields: CustomFieldConfig[];
detailForm: FormGroup;
values: Array<FacetWithValues.Values | { name: string; code: string }>;

constructor(
router: Router,
Expand Down Expand Up @@ -64,12 +62,6 @@ export class FacetDetailComponent extends BaseDetailComponent<FacetWithValues.Fr
});
}

ngOnInit() {
this.init();
this.facet$ = this.entity$;
this.values$ = this.facet$.pipe(map(facet => facet.values));
}

ngOnDestroy() {
this.destroy();
}
Expand Down Expand Up @@ -107,7 +99,14 @@ export class FacetDetailComponent extends BaseDetailComponent<FacetWithValues.Fr
addFacetValue() {
const valuesFormArray = this.detailForm.get('values') as FormArray | null;
if (valuesFormArray) {
valuesFormArray.insert(valuesFormArray.length, this.formBuilder.group({ name: '', code: '' }));
valuesFormArray.insert(
valuesFormArray.length,
this.formBuilder.group({
name: ['', Validators.required],
code: '',
}),
);
this.values.push({ name: '', code: '' });
}
}

Expand All @@ -116,7 +115,7 @@ export class FacetDetailComponent extends BaseDetailComponent<FacetWithValues.Fr
if (!facetForm || !facetForm.dirty) {
return;
}
combineLatest(this.facet$, this.languageCode$)
combineLatest(this.entity$, this.languageCode$)
.pipe(
take(1),
mergeMap(([facet, languageCode]) => {
Expand Down Expand Up @@ -144,7 +143,7 @@ export class FacetDetailComponent extends BaseDetailComponent<FacetWithValues.Fr
}

save() {
combineLatest(this.facet$, this.languageCode$)
combineLatest(this.entity$, this.languageCode$)
.pipe(
take(1),
mergeMap(([facet, languageCode]) => {
Expand All @@ -171,7 +170,11 @@ export class FacetDetailComponent extends BaseDetailComponent<FacetWithValues.Fr
translations: [{ name: c.value.name, languageCode }],
}));
if (newValues.length) {
updateOperations.push(this.dataService.facet.createFacetValues(newValues));
updateOperations.push(
this.dataService.facet
.createFacetValues(newValues)
.pipe(switchMap(() => this.dataService.facet.getFacet(this.id).single$)),
);
}
const updatedValues = this.getUpdatedFacetValues(
facet,
Expand Down Expand Up @@ -200,7 +203,16 @@ export class FacetDetailComponent extends BaseDetailComponent<FacetWithValues.Fr
);
}

deleteFacetValue(facetValueId: string) {
deleteFacetValue(facetValueId: string | undefined, index: number) {
if (!facetValueId) {
// deleting a newly-added (not persisted) FacetValue
const valuesFormArray = this.detailForm.get('values') as FormArray | null;
if (valuesFormArray) {
valuesFormArray.removeAt(index);
}
this.values.splice(index, 1);
return;
}
this.showModalAndDelete(facetValueId)
.pipe(
switchMap(response => {
Expand Down Expand Up @@ -276,20 +288,18 @@ export class FacetDetailComponent extends BaseDetailComponent<FacetWithValues.Fr
}
}

const valuesFormArray = this.detailForm.get('values') as FormArray;
const currentValuesFormArray = this.detailForm.get('values') as FormArray;
currentValuesFormArray.clear();
this.values = facet.values;
facet.values.forEach((value, i) => {
const valueTranslation = value.translations.find(t => t.languageCode === languageCode);
const valueTranslation =
value.translations && value.translations.find(t => t.languageCode === languageCode);
const group = {
id: value.id,
code: value.code,
name: valueTranslation ? valueTranslation.name : '',
};
const existing = valuesFormArray.at(i);
if (existing) {
existing.patchValue(group);
} else {
valuesFormArray.insert(i, this.formBuilder.group(group));
}
currentValuesFormArray.insert(i, this.formBuilder.group(group));
if (this.customValueFields.length) {
let customValueFieldsGroup = this.detailForm.get(['values', i, 'customFields']) as FormGroup;
if (!customValueFieldsGroup) {
Expand Down

0 comments on commit 337762e

Please sign in to comment.