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
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,
cultureName,
currencyCode,
},
},
});
}
13 changes: 13 additions & 0 deletions client-app/core/api/graphql/cart/mutations/clearCart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useApolloClient } from "@vue/apollo-composable";
import { useCartMutationVariables } from "@/core/api/graphql/cart/composables";
import { useMutation } from "@/core/api/graphql/composables";
import { ClearCartDocument } from "@/core/api/graphql/types";
import { globals } from "@/core/globals";
import type { CartIdFragment, CartType } from "@/core/api/graphql/types";
import type { MaybeRef } from "vue";

Expand All @@ -12,6 +13,18 @@ export function useClearCartMutation(cart?: MaybeRef<CartIdFragment | undefined>
return result;
}

export function useClearCurrencyCartMutation() {
const { storeId, cultureName } = globals;
return useMutation(ClearCartDocument, {
variables: {
command: {
storeId,
cultureName,
},
},
});
}

/** @deprecated Use {@link useClearCartMutation} instead. */
export async function clearCart(cartId: string): Promise<CartType> {
const { mutate } = useClearCartMutation({ id: cartId });
Expand Down
32 changes: 32 additions & 0 deletions client-app/core/api/graphql/types.ts

Large diffs are not rendered by default.

19 changes: 16 additions & 3 deletions client-app/pages/account/profile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ import { useForm } from "vee-validate";
import { computed } from "vue";
import { useI18n } from "vue-i18n";
import * as yup from "yup";
import { useChangeCartCurrencyMutation } from "@/core/api/graphql";
import { useCurrency, usePageHead, useThemeContext } from "@/core/composables";
import { useLanguages } from "@/core/composables/useLanguages";
import { ProfileUpdateSuccessModal, useUser } from "@/shared/account";
import { dataChangedEvent, useBroadcast } from "@/shared/broadcast";
import { useModal } from "@/shared/modal";

const MAX_NAME_LENGTH = 64;
Expand All @@ -99,6 +101,8 @@ const { themeContext } = useThemeContext();
const { openModal } = useModal();
const { removeLocaleFromUrl, unpinLocale } = useLanguages();
const { supportedCurrencies, saveCurrencyCode } = useCurrency();
const { mutate: changeCartCurrency } = useChangeCartCurrencyMutation();
const broadcast = useBroadcast();

usePageHead({
title: computed(() => t("pages.account.profile.meta.title")),
Expand Down Expand Up @@ -137,8 +141,15 @@ function applyLanguage(): void {
removeLocaleFromUrl();
}

function applyCurrency(): void {
async function applyCurrency(): Promise<void> {
if (user.value?.contact?.currencyCode) {
await changeCartCurrency({
command: {
userId: user.value.id,
newCurrencyCode: user.value.contact.currencyCode,
},
});

const contactCurrency = supportedCurrencies.value.find((item) => item.code === user.value!.contact!.currencyCode);

if (contactCurrency) {
Expand All @@ -154,9 +165,11 @@ const onSubmit = handleSubmit(async (data) => {
openModal({
component: ProfileUpdateSuccessModal,
props: {
onClose() {
async onClose() {
applyLanguage();
applyCurrency();
await applyCurrency();

void broadcast.emit(dataChangedEvent);

location.reload();
},
Expand Down
39 changes: 30 additions & 9 deletions client-app/shared/account/composables/useSignMeIn.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { syncRefs, useAsyncState } from "@vueuse/core";
import { ref } from "vue";
import { useGetMeQuery, useMergeCartMutation } from "@/core/api/graphql";
import {
useChangeCartCurrencyMutation,
useClearCurrencyCartMutation,
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 +23,9 @@ export function useSignMeIn() {
const { mutate: mergeCart } = useMergeCartMutation();
const { unpinLocale, removeLocaleFromUrl } = useLanguages();
const { supportedCurrencies, saveCurrencyCode } = useCurrency();
const { mutate: changeCartCurrency } = useChangeCartCurrencyMutation();
const { currencyCode: currentCurencyCode } = globals;
const { mutate: clearCurrencyCart } = useClearCurrencyCartMutation();

const { isLoading: loading, execute: signIn } = useAsyncState(
async () => {
Expand All @@ -28,19 +37,31 @@ 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 } });
}
await clearCurrencyCart({
command: { userId: me.value.me.id, currencyCode: currentCurencyCode },
skipQuery: false,
});

const currencyCode = me.value.me.contact?.currencyCode;
await mergeCart({ command: { userId: me.value.me.id, secondCartId: cart.value.id } });

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,34 @@
</template>

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

const { currentCurrency, supportedCurrencies, saveCurrencyCode } = useCurrency();
const { cart } = useFullCart();
const { mutate: changeCartCurrency } = useChangeCartCurrencyMutation();
const broadcast = useBroadcast();
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,
},
});
}

void broadcast.emit(dataChangedEvent);

saveCurrencyCode(code);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,20 @@
<script setup lang="ts">
import { useLanguages } from "@/core/composables/useLanguages";
import { languageToCountryMap } from "@/core/constants";
import { dataChangedEvent, useBroadcast } from "@/shared/broadcast";
import type { ILanguage } from "@/core/types";

const { pinedLocale, supportedLanguages, pinLocale, removeLocaleFromUrl, currentLanguage } = useLanguages();
const broadcast = useBroadcast();

function select(locale: string) {
if (locale !== pinedLocale.value) {
pinLocale(locale);
removeLocaleFromUrl();
location.reload();

void broadcast.emit(dataChangedEvent);

void location.reload();
}
}

Expand Down
Loading