Add support for locale change (#55)

* Add new event type

* Add state handling for locale change
This commit is contained in:
Lukasz Ostrowski 2022-09-13 11:59:30 +02:00 committed by GitHub
parent aaab88b9ae
commit ac6900f35f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 1 deletions

View file

@ -12,6 +12,7 @@ describe("app-bridge-state.ts", () => {
ready: false,
path: "/",
theme: "light",
locale: "en",
});
});
@ -23,10 +24,23 @@ describe("app-bridge-state.ts", () => {
id: "foo-bar",
path: "/",
theme: "light",
locale: "pl",
};
instance.setState(newState);
expect(instance.getState()).toEqual(expect.objectContaining(newState));
});
it("Set \"en\" to be initial locale value", () => {
expect(new AppBridgeStateContainer().getState().locale).toEqual("en");
});
it("Can be constructed with initial locale", () => {
expect(
new AppBridgeStateContainer({
initialLocale: "pl",
}).getState().locale
).toBe("pl");
});
});

View file

@ -7,6 +7,11 @@ export type AppBridgeState = {
domain: string;
path: string;
theme: ThemeType;
locale: string;
};
type Options = {
initialLocale?: string;
};
export class AppBridgeStateContainer {
@ -16,8 +21,15 @@ export class AppBridgeStateContainer {
ready: false,
path: "/",
theme: "light",
locale: "en",
};
constructor(options: Options = {}) {
if (options.initialLocale) {
this.state.locale = options.initialLocale;
}
}
getState() {
return this.state;
}

View file

@ -37,6 +37,12 @@ function eventStateReducer(state: AppBridgeState, event: Events) {
theme: event.payload.theme,
};
}
case EventType.localeChanged: {
return {
...state,
locale: event.payload.locale,
};
}
case EventType.response: {
return state;
}
@ -56,6 +62,7 @@ const createEmptySubscribeMap = (): SubscribeMap => ({
response: {},
redirect: {},
theme: {},
localeChanged: {},
});
export type AppBridgeOptions = {

View file

@ -7,6 +7,7 @@ export const EventType = {
response: "response",
redirect: "redirect",
theme: "theme",
localeChanged: "localeChanged",
} as const;
export type EventType = Values<typeof EventType>;
@ -47,7 +48,19 @@ export type ThemeEvent = Event<
}
>;
export type Events = HandshakeEvent | DispatchResponseEvent | RedirectEvent | ThemeEvent;
export type LocaleChangedEvent = Event<
"localeChanged",
{
locale: string;
}
>;
export type Events =
| HandshakeEvent
| DispatchResponseEvent
| RedirectEvent
| ThemeEvent
| LocaleChangedEvent;
export type PayloadOfEvent<
TEventType extends EventType,