Add post message listeners

This commit is contained in:
Chi Vinh Le
2018-08-10 16:12:08 +02:00
parent 9510fbed57
commit 37f733dfa0
8 changed files with 77 additions and 8 deletions
+11 -3
View File
@@ -10,16 +10,24 @@ import {
} from "talk-framework/lib/bootstrap";
import AppContainer from "./containers/AppContainer";
import {
onPostMessageAuthError,
onPostMessageSetAuthToken,
onPymSetCommentID,
} from "./listeners";
import { initLocalState } from "./local";
import localesData from "./locales";
import { withSetCommentID } from "./pym";
const pymFeatures = [withSetCommentID];
const listeners = [
onPymSetCommentID,
onPostMessageSetAuthToken,
onPostMessageAuthError,
];
// This is called when the context is first initialized.
async function init(context: TalkContext) {
await initLocalState(context.relayEnvironment);
pymFeatures.forEach(f => f(context));
listeners.forEach(f => f(context));
}
async function main() {
@@ -0,0 +1,5 @@
export { default as onPymSetCommentID } from "./onPymSetCommentID";
export {
default as onPostMessageSetAuthToken,
} from "./onPostMessageSetAuthToken";
export { default as onPostMessageAuthError } from "./onPostMessageAuthError";
@@ -0,0 +1,11 @@
import { TalkContext } from "talk-framework/lib/bootstrap";
export default function onPostMessageSetAuthToken({
postMessage,
}: TalkContext) {
// Auth popup will use this to send back errors during login.
postMessage!.on("authError", error => {
// tslint:disable-next-line:no-console
console.error(error);
});
}
@@ -0,0 +1,30 @@
import { Environment, RecordSource } from "relay-runtime";
import { LOCAL_ID } from "talk-framework/lib/relay";
import { createRelayEnvironment } from "talk-framework/testHelpers";
import onPostMessageSetAuthToken from "./onPostMessageSetAuthToken";
let relayEnvironment: Environment;
const source: RecordSource = new RecordSource();
beforeAll(() => {
relayEnvironment = createRelayEnvironment({
source,
});
});
it("Sets auth token", () => {
const token = "auth-token";
const context = {
postMessage: {
on: (name: string, cb: (token: string) => void) => {
expect(name).toBe("setAuthToken");
cb(token);
},
},
relayEnvironment,
};
onPostMessageSetAuthToken(context as any);
expect(source.get(LOCAL_ID)!.authToken).toEqual(token);
});
@@ -0,0 +1,16 @@
import { commitLocalUpdate } from "react-relay";
import { TalkContext } from "talk-framework/lib/bootstrap";
import { LOCAL_ID } from "talk-framework/lib/relay";
export default function onPostMessageSetAuthToken({
relayEnvironment,
postMessage,
}: TalkContext) {
// Auth popup will use this to handle a successful login.
postMessage!.on("setAuthToken", token => {
commitLocalUpdate(relayEnvironment, s => {
s.get(LOCAL_ID)!.setValue(token, "authToken");
});
});
}
@@ -3,7 +3,7 @@ import { Environment, RecordSource } from "relay-runtime";
import { LOCAL_ID } from "talk-framework/lib/relay";
import { createRelayEnvironment } from "talk-framework/testHelpers";
import withSetCommentID from "./withSetCommentID";
import onPymSetCommentID from "./onPymSetCommentID";
let relayEnvironment: Environment;
const source: RecordSource = new RecordSource();
@@ -25,7 +25,7 @@ it("Sets comment id", () => {
},
relayEnvironment,
};
withSetCommentID(context as any);
onPymSetCommentID(context as any);
expect(source.get(LOCAL_ID)!.commentID).toEqual(id);
});
@@ -40,6 +40,6 @@ it("Sets comment id to null when empty", () => {
},
relayEnvironment,
};
withSetCommentID(context as any);
onPymSetCommentID(context as any);
expect(source.get(LOCAL_ID)!.commentID).toEqual(null);
});
@@ -3,7 +3,7 @@ import { commitLocalUpdate } from "react-relay";
import { TalkContext } from "talk-framework/lib/bootstrap";
import { LOCAL_ID } from "talk-framework/lib/relay";
export default function withSetCommentID({
export default function onPymSetCommentID({
relayEnvironment,
pym,
}: TalkContext) {
-1
View File
@@ -1 +0,0 @@
export { default as withSetCommentID } from "./withSetCommentID";