Skip to content

Commit

Permalink
feat: edit product variant for physical goods
Browse files Browse the repository at this point in the history
  • Loading branch information
belsman committed Aug 21, 2024
1 parent 90735d1 commit 1f4a89f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 32 deletions.
4 changes: 2 additions & 2 deletions packages/core/graphql/src/lib/documents/fragments/product.gql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fragment ProductFragment on IoRestorecommerceProductProduct {
id
tags
# tags
product {
name
description
Expand Down Expand Up @@ -28,7 +28,7 @@ fragment ProductFragment on IoRestorecommerceProductProduct {
# }
stockLevel
stockKeepingUnit
parentVariantId
# parentVariantId
taxIds
price {
currencyId
Expand Down
14 changes: 0 additions & 14 deletions packages/core/graphql/src/lib/generated/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5411,7 +5411,6 @@ export type CatalogProductMutateMutation = {
payload?: {
__typename?: 'IoRestorecommerceProductProduct';
id?: string | null;
tags?: Array<string> | null;
product?: {
__typename?: 'IoRestorecommerceProductIndividualProduct';
name?: string | null;
Expand All @@ -5432,7 +5431,6 @@ export type CatalogProductMutateMutation = {
description?: string | null;
stockLevel?: number | null;
stockKeepingUnit?: string | null;
parentVariantId?: string | null;
taxIds?: Array<string> | null;
price?: {
__typename?: 'IoRestorecommercePricePrice';
Expand Down Expand Up @@ -5508,7 +5506,6 @@ export type CatalogProductReadQuery = {
payload?: {
__typename?: 'IoRestorecommerceProductProduct';
id?: string | null;
tags?: Array<string> | null;
product?: {
__typename?: 'IoRestorecommerceProductIndividualProduct';
name?: string | null;
Expand All @@ -5529,7 +5526,6 @@ export type CatalogProductReadQuery = {
description?: string | null;
stockLevel?: number | null;
stockKeepingUnit?: string | null;
parentVariantId?: string | null;
taxIds?: Array<string> | null;
price?: {
__typename?: 'IoRestorecommercePricePrice';
Expand Down Expand Up @@ -5981,7 +5977,6 @@ export type OrderFragmentFragment = {
product?: {
__typename?: 'IoRestorecommerceProductProduct';
id?: string | null;
tags?: Array<string> | null;
product?: {
__typename?: 'IoRestorecommerceProductIndividualProduct';
name?: string | null;
Expand All @@ -6002,7 +5997,6 @@ export type OrderFragmentFragment = {
description?: string | null;
stockLevel?: number | null;
stockKeepingUnit?: string | null;
parentVariantId?: string | null;
taxIds?: Array<string> | null;
price?: {
__typename?: 'IoRestorecommercePricePrice';
Expand Down Expand Up @@ -6519,7 +6513,6 @@ export type OrganizationFragmentFragment = {
export type ProductFragmentFragment = {
__typename?: 'IoRestorecommerceProductProduct';
id?: string | null;
tags?: Array<string> | null;
product?: {
__typename?: 'IoRestorecommerceProductIndividualProduct';
name?: string | null;
Expand All @@ -6540,7 +6533,6 @@ export type ProductFragmentFragment = {
description?: string | null;
stockLevel?: number | null;
stockKeepingUnit?: string | null;
parentVariantId?: string | null;
taxIds?: Array<string> | null;
price?: {
__typename?: 'IoRestorecommercePricePrice';
Expand Down Expand Up @@ -8244,7 +8236,6 @@ export type OrderingOrderMutateMutation = {
product?: {
__typename?: 'IoRestorecommerceProductProduct';
id?: string | null;
tags?: Array<string> | null;
product?: {
__typename?: 'IoRestorecommerceProductIndividualProduct';
name?: string | null;
Expand All @@ -8265,7 +8256,6 @@ export type OrderingOrderMutateMutation = {
description?: string | null;
stockLevel?: number | null;
stockKeepingUnit?: string | null;
parentVariantId?: string | null;
taxIds?: Array<string> | null;
price?: {
__typename?: 'IoRestorecommercePricePrice';
Expand Down Expand Up @@ -8782,7 +8772,6 @@ export type OrderingOrderReadQuery = {
product?: {
__typename?: 'IoRestorecommerceProductProduct';
id?: string | null;
tags?: Array<string> | null;
product?: {
__typename?: 'IoRestorecommerceProductIndividualProduct';
name?: string | null;
Expand All @@ -8803,7 +8792,6 @@ export type OrderingOrderReadQuery = {
description?: string | null;
stockLevel?: number | null;
stockKeepingUnit?: string | null;
parentVariantId?: string | null;
taxIds?: Array<string> | null;
price?: {
__typename?: 'IoRestorecommercePricePrice';
Expand Down Expand Up @@ -9393,7 +9381,6 @@ export const LocationFragmentFragmentDoc = gql`
export const ProductFragmentFragmentDoc = gql`
fragment ProductFragment on IoRestorecommerceProductProduct {
id
tags
product {
name
description
Expand All @@ -9411,7 +9398,6 @@ export const ProductFragmentFragmentDoc = gql`
description
stockLevel
stockKeepingUnit
parentVariantId
taxIds
price {
currencyId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,57 @@ export class ProductVariantEditComponent {
}

onSubmit() {
// ASSUME A PRODUCT IS PHYSICAL!
const value = this.variantModalForm.form.value as IProductVariantFormValue;
value.id = uuidv4();

if (value.offerings === 'physical') {
// REMOVE the field 'offerings'
delete value.offerings;
// Fix the backend price issues!
const regularPrice = +(value.price?.regularPrice || 0);
const salePrice = +(value.price?.salePrice || 0);

// TODO Convert price!
value.stockLevel = +(value.stockLevel || 0);
value.price = {
...value.price,
regularPrice,
salePrice,
};

// REMOVE the field 'offerings'
delete value.offerings;

if (this.variant.id) {
// EDIT MODE
const updatedVariant: IIoRestorecommerceProductPhysicalVariant = {
...this.variant,
...value,
};

const productWithUpdatedVariant =
this.product.product.physical?.variants?.map((variant) => {
if (variant.id === updatedVariant.id) {
return updatedVariant;
} else {
return variant;
}
});

const product: IProduct = {
...this.product,
product: {
...this.product.product,
physical: {
variants: productWithUpdatedVariant,
},
},
};

this.productFacade.update({
items: [product],
mode: ModeType.Update,
});
} else {
// CREATE MODE
value.id = uuidv4();
let product: IProduct;

if (this.product.product.physical?.variants) {
Expand Down Expand Up @@ -116,10 +160,9 @@ export class ProductVariantEditComponent {
}

this.close();
// Massage the value to the parent product
}

onAction(_: string) {
// TODO
this.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { ProductVariantEditComponent } from './product-variant-modal.component';
<ng-container *ngIf="vm$ | async as vm">
<rc-product-view
(addVariant)="onAddVariant(vm.product)"
(editVariant)="onEditVariant($event)"
(editVariant)="onEditVariant($event, vm.product)"
[product]="vm.product"
/>
</ng-container>
Expand Down Expand Up @@ -93,12 +93,15 @@ export class ProductViewComponent implements OnInit, OnDestroy {
.subscribe();
}

onEditVariant(variant: IIoRestorecommerceProductPhysicalVariant) {
console.log('variant:', variant);
onEditVariant(
variant: IIoRestorecommerceProductPhysicalVariant,
product: IProduct
) {
this.subscriptions.sink = this.addVariantLayer
.open({
data: {
title: `Edit product variant`,
product,
variant,
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ export const buildProductVariantSchema = (
return {
type: 'form',
fields: [
// {
// name: 'price',
// type: 'object',
// fields: [

// ],
// },
{
name: 'offerings',
label: 'Offerings',
Expand Down

0 comments on commit 1f4a89f

Please sign in to comment.