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,
|
ready: false,
|
||||||
path: "/",
|
path: "/",
|
||||||
theme: "light",
|
theme: "light",
|
||||||
|
locale: "en",
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -23,10 +24,23 @@ describe("app-bridge-state.ts", () => {
|
||||||
id: "foo-bar",
|
id: "foo-bar",
|
||||||
path: "/",
|
path: "/",
|
||||||
theme: "light",
|
theme: "light",
|
||||||
|
locale: "pl",
|
||||||
};
|
};
|
||||||
|
|
||||||
instance.setState(newState);
|
instance.setState(newState);
|
||||||
|
|
||||||
expect(instance.getState()).toEqual(expect.objectContaining(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;
|
domain: string;
|
||||||
path: string;
|
path: string;
|
||||||
theme: ThemeType;
|
theme: ThemeType;
|
||||||
|
locale: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type Options = {
|
||||||
|
initialLocale?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export class AppBridgeStateContainer {
|
export class AppBridgeStateContainer {
|
||||||
|
@ -16,8 +21,15 @@ export class AppBridgeStateContainer {
|
||||||
ready: false,
|
ready: false,
|
||||||
path: "/",
|
path: "/",
|
||||||
theme: "light",
|
theme: "light",
|
||||||
|
locale: "en",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
constructor(options: Options = {}) {
|
||||||
|
if (options.initialLocale) {
|
||||||
|
this.state.locale = options.initialLocale;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
getState() {
|
getState() {
|
||||||
return this.state;
|
return this.state;
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,6 +37,12 @@ function eventStateReducer(state: AppBridgeState, event: Events) {
|
||||||
theme: event.payload.theme,
|
theme: event.payload.theme,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
case EventType.localeChanged: {
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
locale: event.payload.locale,
|
||||||
|
};
|
||||||
|
}
|
||||||
case EventType.response: {
|
case EventType.response: {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
@ -56,6 +62,7 @@ const createEmptySubscribeMap = (): SubscribeMap => ({
|
||||||
response: {},
|
response: {},
|
||||||
redirect: {},
|
redirect: {},
|
||||||
theme: {},
|
theme: {},
|
||||||
|
localeChanged: {},
|
||||||
});
|
});
|
||||||
|
|
||||||
export type AppBridgeOptions = {
|
export type AppBridgeOptions = {
|
||||||
|
|
|
@ -7,6 +7,7 @@ export const EventType = {
|
||||||
response: "response",
|
response: "response",
|
||||||
redirect: "redirect",
|
redirect: "redirect",
|
||||||
theme: "theme",
|
theme: "theme",
|
||||||
|
localeChanged: "localeChanged",
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export type EventType = Values<typeof EventType>;
|
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<
|
export type PayloadOfEvent<
|
||||||
TEventType extends EventType,
|
TEventType extends EventType,
|
||||||
|
|
Loading…
Reference in a new issue