Add support for locale change (#55)
* Add new event type * Add state handling for locale change
This commit is contained in:
parent
aaab88b9ae
commit
ac6900f35f
4 changed files with 47 additions and 1 deletions
|
@ -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");
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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 = {
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue