Skip to content

Commit d9d455b

Browse files
committed
Add event for form communication
1 parent 27d2df7 commit d9d455b

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

src/app-bridge/app-bridge-state.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@ import { LocaleCode } from "../locales";
22
import { AppPermission, Permission } from "../types";
33
import { ThemeType } from "./events";
44

5+
export type FormContextTranslateProduct = {
6+
formId: "translate-product";
7+
translationLanguage: string;
8+
productId: string;
9+
fields: Array<{
10+
fieldName: string;
11+
originalValue: string;
12+
translatedValue: string;
13+
type: "short" | "rich";
14+
}>;
15+
};
16+
17+
/**
18+
* Strong-type all forms as union
19+
*/
20+
export type FormContextPayload = FormContextTranslateProduct;
21+
522
export type AppBridgeState = {
623
token?: string;
724
id: string;
@@ -24,6 +41,7 @@ export type AppBridgeState = {
2441
email: string;
2542
};
2643
appPermissions?: AppPermission[];
44+
formContext?: FormContextPayload;
2745
};
2846

2947
type Options = {

src/app-bridge/app-bridge.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ function eventStateReducer(state: AppBridgeState, event: Events) {
6161
token: event.payload.token,
6262
};
6363
}
64+
case EventType.formData: {
65+
return {
66+
...state,
67+
formContext: event.payload,
68+
};
69+
}
6470
case EventType.response: {
6571
return state;
6672
}
@@ -82,6 +88,7 @@ const createEmptySubscribeMap = (): SubscribeMap => ({
8288
theme: {},
8389
localeChanged: {},
8490
tokenRefresh: {},
91+
formData: {},
8592
});
8693

8794
export type AppBridgeOptions = {

src/app-bridge/events.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { FormContextPayload } from "@/app-bridge/app-bridge-state";
2+
13
import { LocaleCode } from "../locales";
24
import { Values } from "./helpers";
35

@@ -10,6 +12,7 @@ export const EventType = {
1012
theme: "theme",
1113
localeChanged: "localeChanged",
1214
tokenRefresh: "tokenRefresh",
15+
formData: "formData",
1316
} as const;
1417

1518
export type EventType = Values<typeof EventType>;
@@ -66,17 +69,20 @@ export type TokenRefreshEvent = Event<
6669
}
6770
>;
6871

72+
export type FormDataEvent = Event<"formData", FormContextPayload>;
73+
6974
export type Events =
7075
| HandshakeEvent
7176
| DispatchResponseEvent
7277
| RedirectEvent
7378
| ThemeEvent
7479
| LocaleChangedEvent
75-
| TokenRefreshEvent;
80+
| TokenRefreshEvent
81+
| FormDataEvent;
7682

7783
export type PayloadOfEvent<
7884
TEventType extends EventType,
79-
TEvent extends Events = Events
85+
TEvent extends Events = Events,
8086
// @ts-ignore TODO - why this is not working with this tsconfig? Fixme
8187
> = TEvent extends Event<TEventType, unknown> ? TEvent["payload"] : never;
8288

@@ -113,7 +119,7 @@ export const DashboardEventFactory = {
113119
saleorVersions?: {
114120
dashboard: string;
115121
core: string;
116-
}
122+
},
117123
): HandshakeEvent {
118124
return {
119125
type: "handshake",
@@ -141,4 +147,10 @@ export const DashboardEventFactory = {
141147
},
142148
};
143149
},
150+
createFormDataEvent(formId: string, formPayload: FormContextPayload): FormDataEvent {
151+
return {
152+
type: "formData",
153+
payload: formPayload,
154+
};
155+
},
144156
};

0 commit comments

Comments
 (0)