Skip to content

Commit 90a2c3c

Browse files
bartoszherbaMarcin Kwiatkowski
andcommitted
fix(composables): revert getters (#739)
- copy getters back to the legacy composables for a backward compatibility package and mark all as deprecated Co-authored-by: Marcin Kwiatkowski <[email protected]>
1 parent 3e7397b commit 90a2c3c

19 files changed

+1088
-4
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @deprecated since version 1.0.0
3+
*/
4+
import { AgnosticAttribute } from '@vue-storefront/core';
5+
import { Product } from '@vue-storefront/magento-api';
6+
7+
export const getAttributeValue = (attribute) => attribute.values;
8+
9+
export const formatAttributeList = (attributes): AgnosticAttribute[] => attributes.map((attr) => {
10+
const attrValue = getAttributeValue(attr);
11+
return {
12+
name: attr.attribute_code,
13+
value: attrValue,
14+
label: attr.label,
15+
};
16+
});
17+
18+
export const getVariantByAttributes = (products: Product[], attributes: any): Product => {
19+
if (!products || products.length === 0) {
20+
return null;
21+
}
22+
23+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
24+
const configurationKeys = Object.keys(attributes);
25+
26+
// @ts-ignore
27+
return products[0].configurable_children.find((product) => configurationKeys
28+
// @ts-ignore
29+
.every((attrName) => product[attrName] && product[attrName] === attributes[attrName])) || products[0].configurable_children[0];
30+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @deprecated since version 1.0.0
3+
*/
4+
import { Countries, Country } from '@vue-storefront/magento-api';
5+
import { AddressGetter } from '../types';
6+
7+
const countriesList: AddressGetter['countriesList'] = (countries: Countries[]) => countries
8+
.filter((c) => c.id && c.full_name_english && c.full_name_locale)
9+
.map((c) => ({
10+
id: c.id,
11+
label: c.full_name_locale,
12+
englishLabel: c.full_name_english,
13+
abbreviation: c.two_letter_abbreviation,
14+
}))
15+
.sort((a, b) => a.label.localeCompare(b.label));
16+
17+
const regionList: AddressGetter['regionList'] = (country: Country) => (country?.available_regions ? country.available_regions
18+
.filter((c) => c.id && c.code && c.name)
19+
.map((c) => ({
20+
id: c.id,
21+
label: c.name,
22+
abbreviation: c.code,
23+
} as {
24+
id: number;
25+
label: string;
26+
abbreviation: string;
27+
}))
28+
.sort((a, b) => a.label.localeCompare(b.label)) : []);
29+
30+
export default {
31+
countriesList,
32+
regionList,
33+
} as AddressGetter;
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
/**
2+
* @deprecated since version 1.0.0
3+
*/
4+
import {
5+
CartGetters as CartGettersBase,
6+
AgnosticPrice,
7+
AgnosticTotals,
8+
AgnosticAttribute,
9+
AgnosticCoupon,
10+
AgnosticDiscount,
11+
} from '@vue-storefront/core';
12+
import {
13+
Discount,
14+
Cart,
15+
CartItem,
16+
Product,
17+
SelectedShippingMethod, ConfigurableCartItem, ProductInterface,
18+
} from '@vue-storefront/magento-api';
19+
20+
import productGetters from './productGetters';
21+
import { AgnosticPaymentMethod } from '../types';
22+
23+
export const getItems = (cart: Cart): CartItem[] => {
24+
if (!cart || !cart.items) {
25+
return [];
26+
}
27+
28+
return cart.items;
29+
};
30+
31+
export const getItemName = (product: CartItem): string => productGetters.getName(product.product as Product);
32+
33+
export const getSlug = (product: CartItem): string => productGetters.getSlug(product.product as Product);
34+
35+
export const getItemImage = (product: CartItem): string => productGetters.getProductThumbnailImage(product.product as Product);
36+
37+
export const getItemPrice = (product: CartItem): AgnosticPrice => {
38+
if (!product || !product.prices) {
39+
return {
40+
regular: 0,
41+
special: 0,
42+
};
43+
}
44+
if (product.prices) {
45+
return {
46+
regular: product.prices.row_total.value || 0,
47+
special: product.prices.total_item_discount.value || 0,
48+
};
49+
}
50+
const regularPrice = product.product?.price_range?.minimum_price?.regular_price?.value;
51+
const specialPrice = product.product?.price_range?.minimum_price?.final_price?.value;
52+
53+
return {
54+
regular: regularPrice || 0,
55+
special: specialPrice || 0,
56+
// @ts-ignore
57+
credit: Math.round(specialPrice / 10),
58+
// @TODO: Who set this installment value?
59+
installment: Math.round((specialPrice * 1.1046) / 10),
60+
discountPercentage: 100 - Math.round((specialPrice / regularPrice) * 100),
61+
total: product.prices?.row_total?.value,
62+
};
63+
};
64+
65+
export const productHasSpecialPrice = (product: CartItem): boolean => getItemPrice(product).regular < getItemPrice(product).special;
66+
67+
export const getItemQty = (product: CartItem): number => product.quantity;
68+
69+
export const getItemAttributes = (
70+
{ product }: CartItem & { product: Product },
71+
_filterByAttributeName?: Array<string>,
72+
): Record<string, AgnosticAttribute | string> => {
73+
const attributes = {};
74+
75+
if (!product || !product.configurable_options) {
76+
return attributes;
77+
}
78+
79+
const configurableOptions = product.configurable_options;
80+
81+
// eslint-disable-next-line no-restricted-syntax
82+
for (const option of configurableOptions) {
83+
attributes[option.attribute_code] = {
84+
name: option.attribute_code,
85+
label: option.label,
86+
value: option.values.map((value) => {
87+
const obj = {};
88+
obj[value.value_index] = value.label;
89+
return obj;
90+
}),
91+
} as AgnosticAttribute;
92+
}
93+
return attributes;
94+
};
95+
96+
export const getItemSku = (product: CartItem): string => product?.product?.sku || '';
97+
98+
const calculateDiscounts = (discounts: Discount[]): number => discounts.reduce((a, b) => Number.parseFloat(`${a}`) + Number.parseFloat(`${b.amount.value}`), 0);
99+
100+
export const getTotals = (cart: Cart): AgnosticTotals => {
101+
if (!cart || !cart.prices) return {} as AgnosticTotals;
102+
103+
const subtotal = cart.prices.subtotal_including_tax.value;
104+
return {
105+
total: cart.prices.grand_total.value,
106+
subtotal: cart.prices.subtotal_including_tax.value,
107+
special: (cart.prices.discounts) ? subtotal - calculateDiscounts(cart.prices.discounts) : subtotal,
108+
} as AgnosticTotals;
109+
};
110+
111+
export const getShippingPrice = (cart: Cart): number => {
112+
if (!cart.shipping_addresses) {
113+
return 0;
114+
}
115+
116+
return cart.shipping_addresses
117+
.reduce((
118+
acc,
119+
shippingAddress,
120+
) => {
121+
// eslint-disable-next-line @typescript-eslint/naming-convention
122+
const { selected_shipping_method } = shippingAddress;
123+
124+
if (selected_shipping_method) {
125+
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
126+
return acc + selected_shipping_method.amount.value;
127+
}
128+
129+
return acc;
130+
}, 0);
131+
};
132+
133+
export const getTotalItems = (cart: Cart): number => {
134+
if (!cart) {
135+
return 0;
136+
}
137+
return cart.total_quantity;
138+
};
139+
140+
export const getConfiguredVariant = (product: ConfigurableCartItem): ProductInterface | null => product?.configured_variant || null;
141+
142+
// eslint-disable-next-line import/no-named-as-default-member
143+
export const getFormattedPrice = (price: number) => productGetters.getFormattedPrice(price);
144+
145+
export const getCoupons = (cart: Cart): AgnosticCoupon[] => (Array.isArray(cart?.applied_coupons) ? cart.applied_coupons.map((c) => ({
146+
id: c.code,
147+
name: c.code,
148+
value: 0,
149+
code: c.code,
150+
} as AgnosticCoupon)) : []);
151+
152+
export const getDiscounts = (cart: Cart): AgnosticDiscount[] => (Array.isArray(cart?.prices?.discounts) ? cart.prices.discounts.map((d) => ({
153+
id: d.label,
154+
name: d.label,
155+
description: '',
156+
value: d.amount.value,
157+
code: d.label,
158+
} as AgnosticDiscount)) : []);
159+
160+
export const getAppliedCoupon = (cart: Cart): AgnosticCoupon | null => (Array.isArray(cart?.applied_coupons) && cart?.applied_coupons.length > 0 ? {
161+
id: cart.applied_coupons[0].code,
162+
name: cart.applied_coupons[0].code,
163+
value: 0,
164+
code: cart.applied_coupons[0].code,
165+
} : null);
166+
167+
export const getSelectedShippingMethod = (cart: Cart): SelectedShippingMethod | null => (cart?.shipping_addresses?.length > 0
168+
? cart?.shipping_addresses[0]?.selected_shipping_method
169+
: null);
170+
171+
export const getAvailablePaymentMethods = (cart: Cart): AgnosticPaymentMethod[] => cart?.available_payment_methods.map((p) => ({
172+
label: p.title,
173+
value: p.code,
174+
}));
175+
176+
export const getStockStatus = (product: CartItem): string => product.product.stock_status;
177+
export interface CartGetters extends CartGettersBase<Cart, CartItem> {
178+
getAppliedCoupon(cart: Cart): AgnosticCoupon | null;
179+
getAvailablePaymentMethods(cart: Cart): AgnosticPaymentMethod[];
180+
getSelectedShippingMethod(cart: Cart): SelectedShippingMethod | null;
181+
productHasSpecialPrice(product: CartItem): boolean;
182+
getStockStatus(product: CartItem): string;
183+
getConfiguredVariant(product: ConfigurableCartItem): ProductInterface | null;
184+
}
185+
186+
const cartGetters: CartGetters = {
187+
getAppliedCoupon,
188+
getAvailablePaymentMethods,
189+
getCoupons,
190+
getDiscounts,
191+
getFormattedPrice,
192+
getItemAttributes,
193+
getItemImage,
194+
getItemName,
195+
getSlug,
196+
getItemPrice,
197+
getItemQty,
198+
getItems,
199+
getItemSku,
200+
getSelectedShippingMethod,
201+
getShippingPrice,
202+
getTotalItems,
203+
getTotals,
204+
productHasSpecialPrice,
205+
getStockStatus,
206+
getConfiguredVariant,
207+
};
208+
209+
export default cartGetters;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @deprecated since version 1.0.0
3+
*/
4+
import { CategoryGetters, AgnosticCategoryTree, AgnosticBreadcrumb } from '@vue-storefront/core';
5+
import { Category } from '@vue-storefront/magento-api';
6+
import { buildCategoryTree } from '../helpers/buildCategoryTree';
7+
8+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
9+
export const getTree = (category: Category): AgnosticCategoryTree | null => {
10+
if (!category) {
11+
return null;
12+
}
13+
return buildCategoryTree(category, '');
14+
};
15+
16+
export const getCategoryTree = (
17+
category: Category,
18+
currentCategory: string = '',
19+
withProducts = false,
20+
): AgnosticCategoryTree | null => (
21+
category
22+
? buildCategoryTree(category, currentCategory, withProducts)
23+
: null
24+
);
25+
26+
export const getCategoryBreadcrumbs = (category: any): AgnosticBreadcrumb[] => {
27+
let breadcrumbs = [];
28+
29+
if (!category) {
30+
return [];
31+
}
32+
33+
if (Array.isArray(category?.breadcrumbs)) {
34+
breadcrumbs = category.breadcrumbs.map((breadcrumb) => ({
35+
text: breadcrumb.category_name,
36+
link: `/c/${breadcrumb.category_url_path}${category.url_suffix || ''}`,
37+
} as AgnosticBreadcrumb));
38+
}
39+
40+
breadcrumbs.push({
41+
text: category.name,
42+
link: `/c/${category.url_path}${category.url_suffix || ''}`,
43+
} as AgnosticBreadcrumb);
44+
45+
return breadcrumbs;
46+
};
47+
48+
const categoryGetters: CategoryGetters<Category> = {
49+
getTree,
50+
getBreadcrumbs: getCategoryBreadcrumbs,
51+
getCategoryTree,
52+
};
53+
54+
export default categoryGetters;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @deprecated since version 1.0.0
3+
*/
4+
import { ShippingMethod } from '@vue-storefront/magento-api';
5+
import productGetters from './productGetters';
6+
7+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
8+
export const getShippingMethodId = (shippingMethod: ShippingMethod): string => '';
9+
10+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
11+
export const getShippingMethodName = (shippingMethod: ShippingMethod): string => '';
12+
13+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
14+
export const getShippingMethodDescription = (shippingMethod: ShippingMethod): string => '';
15+
16+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
17+
export const getShippingMethodPrice = (shippingMethod: ShippingMethod): number => 0;
18+
19+
// eslint-disable-next-line import/no-named-as-default-member
20+
export const getFormattedPrice = (price: number) => productGetters.getFormattedPrice(price);
21+
22+
const checkoutGetters = {
23+
getShippingMethodId,
24+
getShippingMethodName,
25+
getShippingMethodDescription,
26+
getFormattedPrice,
27+
getShippingMethodPrice,
28+
};
29+
30+
export default checkoutGetters;

0 commit comments

Comments
 (0)