-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(admin-ui): Move data product detail data fetching into service
- Loading branch information
1 parent
601c766
commit 894ea7b
Showing
5 changed files
with
202 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
admin-ui/src/app/catalog/providers/product-detail.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { BehaviorSubject, forkJoin, Observable, of } from 'rxjs'; | ||
import { map, mergeMap, shareReplay, skip } from 'rxjs/operators'; | ||
import { normalizeString } from 'shared/normalize-string'; | ||
|
||
import { | ||
CreateProductInput, | ||
CreateProductVariantInput, | ||
FacetWithValues, | ||
LanguageCode, | ||
UpdateProductInput, | ||
UpdateProductMutation, | ||
UpdateProductVariantInput, | ||
UpdateProductVariantsMutation, | ||
} from '../../common/generated-types'; | ||
import { DataService } from '../../data/providers/data.service'; | ||
import { CreateProductVariantsConfig } from '../components/generate-product-variants/generate-product-variants.component'; | ||
|
||
/** | ||
* Handles the logic for making the API calls to perform CRUD operations on a Product and its related | ||
* entities. This logic was extracted out of the component because it became too large and hard to follow. | ||
*/ | ||
@Injectable() | ||
export class ProductDetailService { | ||
private facetsSubject = new BehaviorSubject<FacetWithValues.Fragment[]>([]); | ||
|
||
constructor(private dataService: DataService) {} | ||
|
||
getFacets(): Observable<FacetWithValues.Fragment[]> { | ||
let skipValue = 0; | ||
if (this.facetsSubject.value.length === 0) { | ||
this.dataService.facet | ||
.getFacets(9999999, 0) | ||
.mapSingle(data => data.facets.items) | ||
.subscribe(items => this.facetsSubject.next(items)); | ||
skipValue = 1; | ||
} | ||
|
||
return this.facetsSubject.pipe(skip(skipValue)); | ||
} | ||
|
||
getTaxCategories() { | ||
return this.dataService.settings | ||
.getTaxCategories() | ||
.mapSingle(data => data.taxCategories) | ||
.pipe(shareReplay(1)); | ||
} | ||
|
||
createProduct( | ||
input: CreateProductInput, | ||
createVariantsConfig: CreateProductVariantsConfig, | ||
languageCode: LanguageCode, | ||
) { | ||
const createProduct$ = this.dataService.product.createProduct(input); | ||
|
||
const createOptionGroups$ = createVariantsConfig.groups.length | ||
? forkJoin( | ||
createVariantsConfig.groups.map(c => { | ||
return this.dataService.product.createProductOptionGroups({ | ||
code: normalizeString(c.name, '-'), | ||
translations: [{ languageCode, name: c.name }], | ||
options: c.values.map(v => ({ | ||
code: normalizeString(v, '-'), | ||
translations: [{ languageCode, name: v }], | ||
})), | ||
}); | ||
}), | ||
) | ||
: of([]); | ||
|
||
return forkJoin(createProduct$, createOptionGroups$).pipe( | ||
mergeMap(([{ createProduct }, createOptionGroups]) => { | ||
const optionGroups = createOptionGroups.map(g => g.createProductOptionGroup); | ||
const addOptionsToProduct$ = optionGroups.length | ||
? forkJoin( | ||
optionGroups.map(optionGroup => { | ||
return this.dataService.product.addOptionGroupToProduct({ | ||
productId: createProduct.id, | ||
optionGroupId: optionGroup.id, | ||
}); | ||
}), | ||
) | ||
: of([]); | ||
return addOptionsToProduct$.pipe( | ||
map(() => { | ||
return { createProduct, optionGroups }; | ||
}), | ||
); | ||
}), | ||
mergeMap(({ createProduct, optionGroups }) => { | ||
const variants: CreateProductVariantInput[] = createVariantsConfig.variants.map(v => { | ||
const optionIds = optionGroups.length | ||
? v.optionValues.map((optionName, index) => { | ||
const option = optionGroups[index].options.find(o => o.name === optionName); | ||
if (!option) { | ||
throw new Error( | ||
`Could not find a matching ProductOption "${optionName}" when creating variant`, | ||
); | ||
} | ||
return option.id; | ||
}) | ||
: []; | ||
const name = optionGroups.length | ||
? `${createProduct.name} ${v.optionValues.join(' ')}` | ||
: createProduct.name; | ||
return { | ||
productId: createProduct.id, | ||
price: v.price, | ||
sku: v.sku, | ||
stockOnHand: v.stock, | ||
translations: [ | ||
{ | ||
languageCode, | ||
name, | ||
}, | ||
], | ||
optionIds, | ||
}; | ||
}); | ||
return this.dataService.product.createProductVariants(variants).pipe( | ||
map(({ createProductVariants }) => ({ | ||
createProductVariants, | ||
productId: createProduct.id, | ||
})), | ||
); | ||
}), | ||
); | ||
} | ||
|
||
updateProduct( | ||
languageCode: LanguageCode, | ||
productInput?: UpdateProductInput, | ||
variantInput?: UpdateProductVariantInput[], | ||
) { | ||
const updateOperations: Array<Observable<UpdateProductMutation | UpdateProductVariantsMutation>> = []; | ||
if (productInput) { | ||
updateOperations.push(this.dataService.product.updateProduct(productInput)); | ||
} | ||
if (variantInput) { | ||
updateOperations.push(this.dataService.product.updateProductVariants(variantInput)); | ||
} | ||
return forkJoin(updateOperations); | ||
} | ||
} |
Oops, something went wrong.