From 4b1e2dd2fd3a1ad45ef86e736b1cc5f5da695272 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Thu, 13 Sep 2018 21:49:30 +0200 Subject: [PATCH] Implement SetActiveTabMutation --- .../mutations/SetActiveTabMutation.spec.ts | 21 ++++++++++++++++ .../stream/mutations/SetActiveTabMutation.ts | 24 +++++++++++++++++++ src/core/client/stream/mutations/index.ts | 4 ++++ 3 files changed, 49 insertions(+) create mode 100644 src/core/client/stream/mutations/SetActiveTabMutation.spec.ts create mode 100644 src/core/client/stream/mutations/SetActiveTabMutation.ts diff --git a/src/core/client/stream/mutations/SetActiveTabMutation.spec.ts b/src/core/client/stream/mutations/SetActiveTabMutation.spec.ts new file mode 100644 index 000000000..d91b8aa68 --- /dev/null +++ b/src/core/client/stream/mutations/SetActiveTabMutation.spec.ts @@ -0,0 +1,21 @@ +import { Environment, RecordSource } from "relay-runtime"; + +import { LOCAL_ID } from "talk-framework/lib/relay"; +import { createRelayEnvironment } from "talk-framework/testHelpers"; + +import { commit } from "./SetActiveTabMutation"; + +let environment: Environment; +const source: RecordSource = new RecordSource(); + +beforeAll(() => { + environment = createRelayEnvironment({ + source, + }); +}); + +it("Sets activeTab", () => { + const tab = "COMMENTS"; + commit(environment, { tab }); + expect(source.get(LOCAL_ID)!.activeTab).toEqual(tab); +}); diff --git a/src/core/client/stream/mutations/SetActiveTabMutation.ts b/src/core/client/stream/mutations/SetActiveTabMutation.ts new file mode 100644 index 000000000..8c930dd0c --- /dev/null +++ b/src/core/client/stream/mutations/SetActiveTabMutation.ts @@ -0,0 +1,24 @@ +import { commitLocalUpdate, Environment } from "relay-runtime"; + +import { createMutationContainer, LOCAL_ID } from "talk-framework/lib/relay"; + +export interface SetActiveTabInput { + tab: "COMMENTS"; +} + +export type SetActiveTabMutation = (input: SetActiveTabInput) => Promise; + +export async function commit( + environment: Environment, + input: SetActiveTabInput +) { + return commitLocalUpdate(environment, store => { + const record = store.get(LOCAL_ID)!; + record.setValue(input.tab, "activeTab"); + }); +} + +export const withSetActiveTabMutation = createMutationContainer( + "setActiveTab", + commit +); diff --git a/src/core/client/stream/mutations/index.ts b/src/core/client/stream/mutations/index.ts index 6c920dfea..5b39e4e78 100644 --- a/src/core/client/stream/mutations/index.ts +++ b/src/core/client/stream/mutations/index.ts @@ -26,3 +26,7 @@ export { withSetAuthPopupStateMutation, SetAuthPopupStateMutation, } from "./SetAuthPopupStateMutation"; +export { + withSetActiveTabMutation, + SetActiveTabMutation, +} from "./SetActiveTabMutation";