diff --git a/.changeset/orange-buses-bake.md b/.changeset/orange-buses-bake.md new file mode 100644 index 000000000..b7a68a0d4 --- /dev/null +++ b/.changeset/orange-buses-bake.md @@ -0,0 +1,5 @@ +--- +"vue-demo-store": minor +--- + +Add state to the address forms diff --git a/.changeset/yellow-shrimps-explain.md b/.changeset/yellow-shrimps-explain.md new file mode 100644 index 000000000..318100c9b --- /dev/null +++ b/.changeset/yellow-shrimps-explain.md @@ -0,0 +1,5 @@ +--- +"@shopware-pwa/api-client": patch +--- + +Change getContextCountryEndpoint request type to POST diff --git a/apps/e2e-tests/page-objects/CheckoutPage.ts b/apps/e2e-tests/page-objects/CheckoutPage.ts index da2aeb5e1..a33cb691a 100644 --- a/apps/e2e-tests/page-objects/CheckoutPage.ts +++ b/apps/e2e-tests/page-objects/CheckoutPage.ts @@ -33,7 +33,7 @@ export class CheckoutPage { this.street = page.getByTestId("checkout-pi-street-address-input"); this.zipcode = page.getByTestId("checkout-pi-zip-code-input"); this.city = page.getByTestId("checkout-pi-city-input"); - this.country = page.getByTestId("checkout-pi-country-input"); + this.country = page.getByTestId("country-select"); this.submitButton = page.getByTestId("checkout-pi-submit-button"); this.termsBox = page.getByTestId("checkout-terms-box"); this.termCheckbox = page.getByTestId("checkout-t&c-checkbox-tos"); diff --git a/apps/e2e-tests/page-objects/RegisterPage.ts b/apps/e2e-tests/page-objects/RegisterPage.ts index e74fe3289..832c2b863 100644 --- a/apps/e2e-tests/page-objects/RegisterPage.ts +++ b/apps/e2e-tests/page-objects/RegisterPage.ts @@ -26,7 +26,7 @@ export class RegisterForm { this.street = page.getByTestId("registration-street-input"); this.zipcode = page.getByTestId("registration-zipcode-input"); this.city = page.getByTestId("registration-city-input"); - this.country = page.getByTestId("registration-country-select"); + this.country = page.getByTestId("country-select"); this.submitButton = page.getByTestId("registration-submit-button"); } diff --git a/packages/api-client/src/services/ContextServiceTests/getAvailableCountries.spec.ts b/packages/api-client/src/services/ContextServiceTests/getAvailableCountries.spec.ts index f94c28cf0..328f34eef 100644 --- a/packages/api-client/src/services/ContextServiceTests/getAvailableCountries.spec.ts +++ b/packages/api-client/src/services/ContextServiceTests/getAvailableCountries.spec.ts @@ -6,19 +6,23 @@ vi.mock("../../../src/apiService"); const mockedApiInstance = defaultInstance; describe("ContextService - getAvailableCountries", () => { - const mockedGet = vi.fn(); + const mockedPost = vi.fn(); beforeEach(() => { vi.resetAllMocks(); mockedApiInstance.invoke = { - get: mockedGet, + post: mockedPost, } as any; }); it("should return array with countries", async () => { - mockedGet.mockResolvedValueOnce({ data: { total: 2 } }); + mockedPost.mockResolvedValueOnce({ data: { total: 2 } }); const result = await getAvailableCountries(); - expect(mockedGet).toBeCalledTimes(1); - expect(mockedGet).toBeCalledWith("/store-api/country"); + expect(mockedPost).toBeCalledTimes(1); + expect(mockedPost).toBeCalledWith("/store-api/country", { + associations: { + states: {}, + }, + }); expect(result.total).toEqual(2); }); }); diff --git a/packages/api-client/src/services/contextService.ts b/packages/api-client/src/services/contextService.ts index 2a092515a..5a3ec5f43 100644 --- a/packages/api-client/src/services/contextService.ts +++ b/packages/api-client/src/services/contextService.ts @@ -182,9 +182,13 @@ export async function setCurrentLanguage( export async function getAvailableCountries( contextInstance: ShopwareApiInstance = defaultInstance ): Promise> { - const { data } = await contextInstance.invoke.get< + const { data } = await contextInstance.invoke.post< EntityResult<"country", Country> - >(getContextCountryEndpoint()); + >(getContextCountryEndpoint(), { + associations: { + states: {}, + }, + }); return data; } diff --git a/packages/composables/src/useCountries.ts b/packages/composables/src/useCountries.ts index dfb5e981e..79bfb9082 100644 --- a/packages/composables/src/useCountries.ts +++ b/packages/composables/src/useCountries.ts @@ -8,13 +8,14 @@ import { provide, } from "vue"; import { getAvailableCountries } from "@shopware-pwa/api-client"; -import { Country } from "@shopware-pwa/types"; +import { Country, CountryState } from "@shopware-pwa/types"; import { useShopwareContext } from "./useShopwareContext"; export type UseCountriesReturn = { mountedCallback(): Promise; getCountries: ComputedRef; fetchCountries(): Promise; + getStatesForCountry(countryId: string): CountryState[] | null; }; /** @@ -43,11 +44,20 @@ export function useCountries(): UseCountriesReturn { } }; + const getStatesForCountry = (countryId: string) => { + return ( + getCountries.value.find((element: Country) => { + return element.id === countryId; + })?.states || null + ); + }; + onMounted(mountedCallback); return { mountedCallback, fetchCountries, + getStatesForCountry, getCountries, }; } diff --git a/packages/composables/src/useState.ts b/packages/composables/src/useState.ts new file mode 100644 index 000000000..e69de29bb diff --git a/templates/vue-demo-store/components/account/AccountAddressForm.vue b/templates/vue-demo-store/components/account/AccountAddressForm.vue index f87b33e71..745eaa673 100644 --- a/templates/vue-demo-store/components/account/AccountAddressForm.vue +++ b/templates/vue-demo-store/components/account/AccountAddressForm.vue @@ -20,13 +20,13 @@ const props = withDefaults( } ); -const { getCountries } = useCountries(); const { getSalutations } = useSalutations(); const { t } = useI18n(); const { pushError } = useNotifications(); const formData = reactive({ countryId: props.address?.countryId ?? "", + countryStateId: props.address?.countryStateId ?? "", salutationId: props.address?.salutationId ?? "", firstName: props.address?.firstName ?? "", lastName: props.address?.lastName ?? "", @@ -130,33 +130,11 @@ useFocus(firstNameInputElement, { initialValue: true }); data-testid="account-address-form-lastname-input" /> -
- - -
- +