Skip to content

Commit

Permalink
feat(composables): add setCurrentCountry (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdanilowicz authored Aug 7, 2023
1 parent 133faf4 commit 3683116
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/nine-ladybugs-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopware-pwa/composables-next": minor
---

Add `setCountry` method for `useSessionContext` composable
5 changes: 5 additions & 0 deletions .changeset/smooth-tips-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopware/api-client": minor
---

Add `setCurrentCountry` for changing context countryId
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { defaultInstance } from "../../apiService";
import { setCurrentCountry } from "../contextService";
import { update } from "../..";
import { describe, expect, it, beforeEach, vi } from "vitest";

vi.mock("../../../src/apiService");
const mockedApiInstance = defaultInstance;

describe("ContextService - setCurrentCountry", () => {
const newCountryId = "28bbf3f6e79145ba8ffc91c409690ab8";
const contextToken = "NWDdcRTTWoPk4Ngv13z5NDMMsDFRb9W6";

const mockedPatch = vi.fn();
beforeEach(() => {
vi.resetAllMocks();
mockedApiInstance.invoke = {
patch: mockedPatch,
} as any;
});

describe("with contextToken given", () => {
beforeEach(() => {
update({ contextToken });
});

it("should return context token", async () => {
mockedPatch.mockResolvedValueOnce({
data: { "sw-context-token": contextToken },
});

const result = await setCurrentCountry(newCountryId);

expect(mockedPatch).toBeCalledTimes(1);
expect(mockedPatch).toBeCalledWith("/store-api/context", {
countryId: newCountryId,
});

expect(result.contextToken).toEqual(contextToken);
});
});

describe("without contextToken given", () => {
beforeEach(() => {
update({ contextToken: undefined });
});

it("should return context token", async () => {
mockedPatch.mockResolvedValueOnce({
data: { "sw-context-token": contextToken },
});

const result = await setCurrentCountry(newCountryId);

expect(mockedPatch).toBeCalledTimes(1);
expect(mockedPatch).toBeCalledWith("/store-api/context", {
countryId: "28bbf3f6e79145ba8ffc91c409690ab8",
});

expect(result.contextToken).toEqual(contextToken);
});
});
});
23 changes: 22 additions & 1 deletion packages/api-client/src/services/contextService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export async function getAvailableLanguages(
}

/**
* Set the current session's language to correspoding to id
* Set the current session's language to corresponding to id
*
* @param {string} newLanguageId id of the language
* @param {ShopwareApiInstance} contextInstance instance of the api client (by default it's an Axios instance)
Expand All @@ -170,6 +170,27 @@ export async function setCurrentLanguage(
return resp;
}

/**
* Set the current session's country
*
* @param {string} newCountryId id of the country
* @param {ShopwareApiInstance} contextInstance instance of the api client (by default it's an Axios instance)
*
* @throws ClientApiError
* @category Context
* @public
*/

export async function setCurrentCountry(
newCountryId: string,
contextInstance: ShopwareApiInstance = defaultInstance,
): Promise<ContextTokenResponse> {
const params = { countryId: newCountryId };
const resp = await updateContext(params, contextInstance);

return resp;
}

/**
* Get all available countries
*
Expand Down
13 changes: 13 additions & 0 deletions packages/composables/src/useSessionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
setCurrentShippingAddress,
setCurrentBillingAddress,
setCurrentLanguage,
setCurrentCountry,
} from "@shopware-pwa/api-client";
import { useShopwareContext } from "./useShopwareContext";
import { _useContext } from "./internal/_useContext";
Expand All @@ -27,6 +28,12 @@ export type UseSessionContextReturn = {
* Patches the context in order to use new language
*/
setLanguage(language: Partial<Language>): Promise<void>;
/**
* Patches the context in order to use new countryId
*
* @param {string} countryId
*/
setCountry(countryId: string): Promise<void>;
/**
* current context's language
*/
Expand Down Expand Up @@ -173,6 +180,11 @@ export function useSessionContext(
await refreshSessionContext();
};

const setCountry = async (countryId: string) => {
await setCurrentCountry(countryId, apiInstance);
await refreshSessionContext();
};

const activeShippingAddress = computed(
() => sessionContext.value?.shippingLocation?.address || null,
);
Expand Down Expand Up @@ -234,5 +246,6 @@ export function useSessionContext(
setLanguage,
languageId,
languageIdChain,
setCountry,
};
}

2 comments on commit 3683116

@vercel
Copy link

@vercel vercel bot commented on 3683116 Aug 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 3683116 Aug 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

frontends-demo – ./templates/vue-demo-store

frontends-demo-git-main-shopware-frontends.vercel.app
frontends-demo-shopware-frontends.vercel.app
frontends-demo.vercel.app

Please sign in to comment.