diff --git a/src/app-bridge/app-bridge-provider.test.tsx b/src/app-bridge/app-bridge-provider.test.tsx index fcb6470..fec6093 100644 --- a/src/app-bridge/app-bridge-provider.test.tsx +++ b/src/app-bridge/app-bridge-provider.test.tsx @@ -133,6 +133,7 @@ describe("useAppBridge hook", () => { path: "", ready: false, theme: "light", + locale: "en", }); }); }); diff --git a/src/app-bridge/app-bridge-state.ts b/src/app-bridge/app-bridge-state.ts index 687a60d..25de828 100644 --- a/src/app-bridge/app-bridge-state.ts +++ b/src/app-bridge/app-bridge-state.ts @@ -1,3 +1,4 @@ +import { LocaleCode } from "../locales"; import { ThemeType } from "./events"; export type AppBridgeState = { @@ -7,11 +8,11 @@ export type AppBridgeState = { domain: string; path: string; theme: ThemeType; - locale: string; + locale: LocaleCode; }; type Options = { - initialLocale?: string; + initialLocale?: LocaleCode; }; export class AppBridgeStateContainer { diff --git a/src/app-bridge/app-bridge.test.ts b/src/app-bridge/app-bridge.test.ts index 845fa46..1553386 100644 --- a/src/app-bridge/app-bridge.test.ts +++ b/src/app-bridge/app-bridge.test.ts @@ -1,6 +1,10 @@ import { fireEvent } from "@testing-library/dom"; import { beforeEach, describe, expect, it, vi } from "vitest"; +import { LocaleCode } from "../locales"; +// eslint-disable-next-line +import { actions, AppBridge, DispatchResponseEvent, HandshakeEvent, ThemeEvent } from "."; + // mock document.referrer const origin = "http://example.com"; const domain = "saleor.domain.host"; @@ -17,9 +21,6 @@ Object.defineProperty(window, "location", { writable: true, }); -// eslint-disable-next-line -import { actions, DispatchResponseEvent, HandshakeEvent, AppBridge, ThemeEvent } from "."; - const handshakeEvent: HandshakeEvent = { payload: { token: "mock-token", @@ -211,4 +212,12 @@ describe("AppBridge", () => { expect(appBridge.getState().domain).toEqual("https://foo.bar"); }); + + it.each(["pl", "en", "it"])("sets initial locale \"%s\" from constructor", (locale) => { + const instance = new AppBridge({ + initialLocale: locale, + }); + + expect(instance.getState().locale).toBe(locale); + }); }); diff --git a/src/app-bridge/app-bridge.ts b/src/app-bridge/app-bridge.ts index 11a7faf..1a6d797 100644 --- a/src/app-bridge/app-bridge.ts +++ b/src/app-bridge/app-bridge.ts @@ -1,5 +1,6 @@ import debugPkg from "debug"; +import { LocaleCode } from "../locales"; import { Actions } from "./actions"; import { AppBridgeState, AppBridgeStateContainer } from "./app-bridge-state"; import { SSR } from "./constants"; @@ -67,6 +68,7 @@ const createEmptySubscribeMap = (): SubscribeMap => ({ export type AppBridgeOptions = { targetDomain?: string; + initialLocale?: LocaleCode; }; const getDefaultOptions = (): AppBridgeOptions => ({ @@ -74,7 +76,7 @@ const getDefaultOptions = (): AppBridgeOptions => ({ }); export class AppBridge { - private state = new AppBridgeStateContainer(); + private state: AppBridgeStateContainer; private refererOrigin = document.referrer ? new URL(document.referrer).origin : undefined; @@ -91,6 +93,10 @@ export class AppBridge { ); } + this.state = new AppBridgeStateContainer({ + initialLocale: options.initialLocale, + }); + this.combinedOptions = { ...this.combinedOptions, ...options, diff --git a/src/app-bridge/events.ts b/src/app-bridge/events.ts index 889f1e6..da566b6 100644 --- a/src/app-bridge/events.ts +++ b/src/app-bridge/events.ts @@ -1,3 +1,4 @@ +import { LocaleCode } from "../locales"; import { Values } from "./helpers"; export type Version = 1; @@ -51,7 +52,7 @@ export type ThemeEvent = Event< export type LocaleChangedEvent = Event< "localeChanged", { - locale: string; + locale: LocaleCode; } >; diff --git a/src/const.ts b/src/const.ts index 35c25bc..a908391 100644 --- a/src/const.ts +++ b/src/const.ts @@ -2,3 +2,5 @@ export const SALEOR_DOMAIN_HEADER = "saleor-domain"; export const SALEOR_EVENT_HEADER = "saleor-event"; export const SALEOR_SIGNATURE_HEADER = "saleor-signature"; export const SALEOR_AUTHORIZATION_BEARER_HEADER = "authorization-bearer"; + +export * from "./locales"; diff --git a/src/index.ts b/src/index.ts index 2708a4f..27de774 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,9 @@ const capitalize = (value: string) => value.charAt(0).toUpperCase() + value.slic const dropFileExtension = (filename: string) => path.parse(filename).name; +/** + * @deprecated remove magic + */ export const inferWebhooks = async ( baseURL: string, webhooksPath: string, diff --git a/src/locales.ts b/src/locales.ts new file mode 100644 index 0000000..6af0f08 --- /dev/null +++ b/src/locales.ts @@ -0,0 +1,47 @@ +/** + * Available locales in Saleor Dashboard. + * TODO: Extract to shared package between Dashboard and sdk, so this stay in sync? + */ +export type LocaleCode = + | "ar" + | "az" + | "bg" + | "bn" + | "ca" + | "cs" + | "da" + | "de" + | "el" + | "en" + | "es" + | "es-CO" + | "et" + | "fa" + | "fr" + | "hi" + | "hu" + | "hy" + | "id" + | "is" + | "it" + | "ja" + | "ko" + | "mn" + | "nb" + | "nl" + | "pl" + | "pt" + | "pt-BR" + | "ro" + | "ru" + | "sk" + | "sl" + | "sq" + | "sr" + | "sv" + | "th" + | "tr" + | "uk" + | "vi" + | "zh-Hans" + | "zh-Hant";