Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: keep cart items on currency change #1505

Open
wants to merge 12 commits into
base: dev
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions client-app/core/api/graphql/cart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from "./mutations/addOrUpdateCartPayment";
export * from "./mutations/addOrUpdateCartShipment";
export * from "./mutations/changeCartComment";
export * from "./mutations/changeCartConfiguredItem";
export * from "./mutations/changeCartCurrency";
export * from "./mutations/changeCartItemQuantity";
export * from "./mutations/changeFullCartItemQuantity";
export * from "./mutations/changeFullCartItemsQuantity";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#import "../../fragments/fullCart.graphql"

mutation ChangeCartCurrency($command: InputChangeCartCurrencyType!) {
changeCartCurrency(command: $command) {
...fullCart
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useMutation } from "@/core/api/graphql/composables";
import { ChangeCartCurrencyDocument } from "@/core/api/graphql/types";
import { globals } from "@/core/globals";

export function useChangeCartCurrencyMutation() {
const { storeId, currencyCode, cultureName } = globals;
return useMutation(ChangeCartCurrencyDocument, {
variables: {
command: {
storeId,
currencyCode,
cultureName,
},
},
});
}
32 changes: 32 additions & 0 deletions client-app/core/api/graphql/types.ts

Large diffs are not rendered by default.

28 changes: 19 additions & 9 deletions client-app/shared/account/composables/useSignMeIn.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { syncRefs, useAsyncState } from "@vueuse/core";
import { ref } from "vue";
import { useGetMeQuery, useMergeCartMutation } from "@/core/api/graphql";
import { useChangeCartCurrencyMutation, useGetMeQuery, useMergeCartMutation } from "@/core/api/graphql";
import { useAuth } from "@/core/composables/useAuth";
import { useCurrency } from "@/core/composables/useCurrency";
import { useLanguages } from "@/core/composables/useLanguages";
import { USER_ID_LOCAL_STORAGE } from "@/core/constants";
import { globals } from "@/core/globals";
import { TabsType, openReturnUrl, useBroadcast } from "@/shared/broadcast";
import { useShortCart } from "@/shared/cart/composables";
import type { IdentityErrorType } from "@/core/api/graphql/types";
Expand All @@ -17,6 +18,8 @@ export function useSignMeIn() {
const { mutate: mergeCart } = useMergeCartMutation();
const { unpinLocale, removeLocaleFromUrl } = useLanguages();
const { supportedCurrencies, saveCurrencyCode } = useCurrency();
const { mutate: changeCartCurrency } = useChangeCartCurrencyMutation();
const { currencyCode: currentCurencyCode } = globals;

const { isLoading: loading, execute: signIn } = useAsyncState(
async () => {
Expand All @@ -28,19 +31,26 @@ export function useSignMeIn() {
// get user that will be applied after reload.
await getMe();

if (me.value?.me) {
const currencyCode = me.value?.me?.contact?.currencyCode;

if (me.value?.me && currencyCode) {
if (cart.value?.id) {
await mergeCart({ command: { userId: me.value.me.id, secondCartId: cart.value.id } });
}

const currencyCode = me.value.me.contact?.currencyCode;

if (currencyCode) {
const contactCurrency = supportedCurrencies.value.find((item) => item.code === currencyCode);
if (contactCurrency) {
saveCurrencyCode(contactCurrency.code, false);
if (currencyCode !== currentCurencyCode) {
await changeCartCurrency({
command: {
userId: me.value.me.id,
newCurrencyCode: currencyCode,
},
});
}
}

const contactCurrency = supportedCurrencies.value.find((item) => item.code === currencyCode);
if (contactCurrency) {
saveCurrencyCode(contactCurrency.code, false);
}
}

void broadcast.emit(openReturnUrl, undefined, TabsType.ALL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,30 @@
</template>

<script setup lang="ts">
import { useChangeCartCurrencyMutation } from "@/core/api/graphql";
import { useCurrency } from "@/core/composables";
import { globals } from "@/core/globals";
import { useFullCart } from "@/shared/cart";

const { currentCurrency, supportedCurrencies, saveCurrencyCode } = useCurrency();
const { cart } = useFullCart();
const { mutate: changeCartCurrency } = useChangeCartCurrencyMutation();
const { userId } = globals;

function select(code: string) {
async function select(code: string): Promise<void> {
if (currentCurrency.value?.code !== code) {
if (cart.value) {
await changeCartCurrency({
command: {
userId,
cartId: cart.value.id,
cartName: cart.value.name,
cartType: cart.value.type,
newCurrencyCode: code,
},
});
}

saveCurrencyCode(code);
}
}
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5273,7 +5273,7 @@ __metadata:
languageName: node
linkType: hard

"@virto-commerce/front-modules-google-ecommerce-analytics@npm:^1.0.10":
"@virto-commerce/front-modules-google-ecommerce-analytics@npm:1.0.10":
version: 1.0.10
resolution: "@virto-commerce/front-modules-google-ecommerce-analytics@npm:1.0.10"
dependencies:
Expand Down Expand Up @@ -14892,7 +14892,7 @@ __metadata:
"@unhead/vue": "npm:^1.9.14"
"@vee-validate/rules": "npm:~4.13.2"
"@vee-validate/yup": "npm:~4.13.2"
"@virto-commerce/front-modules-google-ecommerce-analytics": "npm:^1.0.10"
"@virto-commerce/front-modules-google-ecommerce-analytics": "npm:1.0.10"
"@virto-commerce/front-modules-hotjar": "npm:^1.0.4"
"@vitejs/plugin-vue": "npm:^5.0.5"
"@vue/apollo-composable": "npm:^4.0.2"
Expand Down
Loading