-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3317 from andrzejewsky/feature/3249-refactoring-v…
…uex-cart Refactoring vuex cart module
- Loading branch information
Showing
61 changed files
with
1,578 additions
and
1,006 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import { DataResolver } from './types/DataResolver' | ||
import Task from '@vue-storefront/core/lib/sync/types/Task' | ||
import CartItem from '@vue-storefront/core/modules/cart/types/CartItem' | ||
import { TaskQueue } from '@vue-storefront/core/lib/sync' | ||
import { processLocalizedURLAddress } from '@vue-storefront/core/helpers' | ||
import config from 'config'; | ||
|
||
const setShippingInfo = async (addressInformation: any): Promise<Task> => | ||
TaskQueue.execute({ | ||
url: processLocalizedURLAddress(config.cart.shippinginfo_endpoint), | ||
payload: { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
mode: 'cors', | ||
body: JSON.stringify({ addressInformation }) | ||
}, | ||
silent: true | ||
}); | ||
|
||
const getTotals = async (): Promise<Task> => | ||
TaskQueue.execute({ | ||
url: processLocalizedURLAddress(config.cart.totals_endpoint), | ||
payload: { | ||
method: 'GET', | ||
headers: { 'Content-Type': 'application/json' }, | ||
mode: 'cors' | ||
}, | ||
silent: true | ||
}); | ||
|
||
const getCartToken = async (guestCart: boolean = false, forceClientState: boolean = false): Promise<Task> => { | ||
const url = processLocalizedURLAddress(guestCart | ||
? config.cart.create_endpoint.replace('{{token}}', '') | ||
: config.cart.create_endpoint) | ||
|
||
return TaskQueue.execute({ | ||
url, | ||
payload: { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
mode: 'cors' | ||
}, | ||
force_client_state: forceClientState, | ||
silent: true | ||
}); | ||
} | ||
|
||
const updateItem = async (cartServerToken: string, cartItem: CartItem): Promise<Task> => | ||
TaskQueue.execute({ | ||
url: processLocalizedURLAddress(config.cart.updateitem_endpoint), | ||
payload: { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
mode: 'cors', | ||
body: JSON.stringify({ | ||
cartItem: { | ||
...cartItem, | ||
quoteId: cartItem.quoteId || cartServerToken | ||
} | ||
}) | ||
} | ||
}); | ||
|
||
const deleteItem = async (cartServerToken: string, cartItem: CartItem): Promise<Task> => | ||
TaskQueue.execute({ | ||
url: processLocalizedURLAddress(config.cart.deleteitem_endpoint), | ||
payload: { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
mode: 'cors', | ||
body: JSON.stringify({ | ||
cartItem: { | ||
...cartItem, | ||
quoteId: cartServerToken | ||
} | ||
}) | ||
}, | ||
silent: true | ||
}); | ||
|
||
const getPaymentMethods = async (): Promise<Task> => | ||
TaskQueue.execute({ | ||
url: processLocalizedURLAddress(config.cart.paymentmethods_endpoint), | ||
payload: { | ||
method: 'GET', | ||
headers: { 'Content-Type': 'application/json' }, | ||
mode: 'cors' | ||
}, | ||
silent: true | ||
}); | ||
|
||
const getShippingMethods = async (address: any): Promise<Task> => | ||
TaskQueue.execute({ | ||
url: processLocalizedURLAddress(config.cart.shippingmethods_endpoint), | ||
payload: { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
mode: 'cors', | ||
body: JSON.stringify({ | ||
address | ||
}) | ||
}, | ||
silent: true | ||
}); | ||
|
||
const getItems = async (): Promise<Task> => | ||
TaskQueue.execute({ | ||
url: processLocalizedURLAddress(config.cart.pull_endpoint), | ||
payload: { | ||
method: 'GET', | ||
headers: { 'Content-Type': 'application/json' }, | ||
mode: 'cors' | ||
}, | ||
silent: true | ||
}); | ||
|
||
const applyCoupon = async (couponCode: string): Promise<Task> => { | ||
const url = processLocalizedURLAddress(config.cart.applycoupon_endpoint.replace('{{coupon}}', couponCode)) | ||
|
||
return TaskQueue.execute({ | ||
url, | ||
payload: { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
mode: 'cors' | ||
}, | ||
silent: false | ||
}); | ||
} | ||
|
||
const removeCoupon = async (): Promise<Task> => | ||
TaskQueue.execute({ | ||
url: processLocalizedURLAddress(config.cart.deletecoupon_endpoint), | ||
payload: { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
mode: 'cors' | ||
}, | ||
silent: false | ||
}); | ||
|
||
export const CartService: DataResolver.CartService = { | ||
setShippingInfo, | ||
getTotals, | ||
getCartToken, | ||
updateItem, | ||
deleteItem, | ||
getPaymentMethods, | ||
getShippingMethods, | ||
getItems, | ||
applyCoupon, | ||
removeCoupon | ||
} |
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { CategoryService } from './CategoryService'; | ||
import { UserService } from './UserService'; | ||
import { CategoryService } from './CategoryService' | ||
import { UserService } from './UserService' | ||
import { CartService } from './CartService' | ||
|
||
export { | ||
CategoryService, | ||
UserService | ||
UserService, | ||
CartService | ||
} |
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
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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
|
||
// @deprecated moved to theme | ||
export const MicrocartButton = { | ||
name: 'MicrocartButton', | ||
mounted () { | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import i18n from '@vue-storefront/i18n' | ||
import sumBy from 'lodash-es/sumBy' | ||
import ShippingMethod from '@vue-storefront/core/modules/cart/types/ShippingMethod' | ||
import PaymentMethod from '@vue-storefront/core/modules/cart/types/PaymentMethod' | ||
import CartItem from '@vue-storefront/core/modules/cart/types/CartItem' | ||
|
||
const calculateTotals = (shippingMethod: ShippingMethod, paymentMethod: PaymentMethod, cartItems: CartItem[]) => { | ||
const shippingTax = shippingMethod ? shippingMethod.price_incl_tax : 0 | ||
|
||
const totalsArray = [ | ||
{ | ||
code: 'subtotal_incl_tax', | ||
title: i18n.t('Subtotal incl. tax'), | ||
value: sumBy(cartItems, (p) => p.qty * p.price_incl_tax) | ||
}, | ||
{ | ||
code: 'grand_total', | ||
title: i18n.t('Grand total'), | ||
value: sumBy(cartItems, (p) => p.qty * p.price_incl_tax + shippingTax) | ||
} | ||
] | ||
|
||
if (paymentMethod) { | ||
totalsArray.push({ | ||
code: 'payment', | ||
title: i18n.t(paymentMethod.title), | ||
value: paymentMethod.cost_incl_tax | ||
}) | ||
} | ||
if (shippingMethod) { | ||
totalsArray.push({ | ||
code: 'shipping', | ||
title: i18n.t(shippingMethod.method_title), | ||
value: shippingMethod.price_incl_tax | ||
}) | ||
} | ||
|
||
return totalsArray | ||
} | ||
|
||
export default calculateTotals |
Oops, something went wrong.