Skip to content

Commit

Permalink
feat(admin-ui): Update slug in product detail form after save
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed May 31, 2019
1 parent d8d5fcc commit 2cecb39
Showing 1 changed file with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import {
ProductWithVariants,
TaxCategory,
UpdateProductInput,
UpdateProductMutation,
UpdateProductVariantInput,
UpdateProductVariantsMutation,
} from '../../../common/generated-types';
import { createUpdatedTranslatable } from '../../../common/utilities/create-updated-translatable';
import { flattenFacetValues } from '../../../common/utilities/flatten-facet-values';
Expand Down Expand Up @@ -290,7 +292,9 @@ export class ProductDetailComponent extends BaseDetailComponent<ProductWithVaria
take(1),
mergeMap(([product, languageCode]) => {
const productGroup = this.getProductFormGroup();
const updateOperations: Array<Observable<any>> = [];
const updateOperations: Array<
Observable<UpdateProductMutation | UpdateProductVariantsMutation>
> = [];

if (productGroup.dirty || this.assetsChanged()) {
const newProduct = this.getUpdatedProduct(
Expand All @@ -316,7 +320,8 @@ export class ProductDetailComponent extends BaseDetailComponent<ProductWithVaria
}),
)
.subscribe(
() => {
result => {
this.updateSlugAfterSave(result);
this.detailForm.markAsPristine();
this.assetChanges = {};
this.variantAssetChanges = {};
Expand Down Expand Up @@ -470,4 +475,20 @@ export class ProductDetailComponent extends BaseDetailComponent<ProductWithVaria
private getProductFormGroup(): FormGroup {
return this.detailForm.get('product') as FormGroup;
}

/**
* The server may alter the slug value in order to normalize and ensure uniqueness upon saving.
*/
private updateSlugAfterSave(results: Array<UpdateProductMutation | UpdateProductVariantsMutation>) {
const firstResult = results[0];
const slugControl = this.detailForm.get(['product', 'slug']);

function isUpdateMutation(input: any): input is UpdateProductMutation {
return input.hasOwnProperty('updateProduct');
}

if (slugControl && isUpdateMutation(firstResult)) {
slugControl.setValue(firstResult.updateProduct.slug, { emitEvent: false });
}
}
}

0 comments on commit 2cecb39

Please sign in to comment.