diff --git a/src/components/FeatureFlag.js b/src/components/FeatureFlag.js index 8854804..b944ddd 100644 --- a/src/components/FeatureFlag.js +++ b/src/components/FeatureFlag.js @@ -2,7 +2,7 @@ import React from "react"; import { Subscriber } from "react-broadcast"; -import type { FeatureFlagType } from "../types"; +import type { FeatureFlagType, ConfigType } from "../types"; import { BROADCAST_CHANNEL } from "../constants/LaunchDarkly"; import FeatureFlagRenderer from "./FeatureFlagRenderer"; @@ -10,7 +10,7 @@ export default function FeatureFlag (props:FeatureFlagType) { return ( { - (config) => { + (config:ConfigType) => { if (config) { return (); } diff --git a/src/components/FeatureFlagRenderer.js b/src/components/FeatureFlagRenderer.js index 6a50f58..5285841 100644 --- a/src/components/FeatureFlagRenderer.js +++ b/src/components/FeatureFlagRenderer.js @@ -1,13 +1,19 @@ // @flow import { Component } from "react"; -import type { FeatureFlagType, ConfigType, LdClientWrapperType, FlagValueType } from "../types"; +import type { + FeatureFlagType, + ConfigType, + LdClientWrapperType, + FlagValueType, + IdentifyType +} from "../types"; import { ldClientWrapper, ldOverrideFlag } from "../lib/utils"; type Props = FeatureFlagType & ConfigType; type State = { checkFeatureFlagComplete: boolean, - flagValue: any + flagValue: FlagValueType }; export default class FeatureFlagRenderer extends Component { @@ -42,6 +48,7 @@ export default class FeatureFlagRenderer extends Component { this.checkFeatureFlag(ldClient); this.listenFlagChangeEvent(ldClient); + this.identify(ldClient); } componentWillUnmount () { @@ -86,6 +93,17 @@ export default class FeatureFlagRenderer extends Component { }); } + identify (ldClient: LdClientWrapperType) { + if (this.props.identify) { + const { user, identify } = this.props; + const mergedUser:IdentifyType = Object.assign(user, identify); + + ldClient.onReady(() => { + ldClient.identify(mergedUser, identify.hash || null); + }); + } + } + setStateFlagValue (flagValue:FlagValueType) { const { flagKey } = this.props; const typeFlagValue = typeof flagValue; diff --git a/src/types/index.js b/src/types/index.js index 504ff7c..08f3460 100644 --- a/src/types/index.js +++ b/src/types/index.js @@ -2,14 +2,16 @@ export type LdClientWrapperType = { on: (string, (FlagValueType) => (void)) => void, onReady: (() => void) => void, - variation: (string, boolean) => FlagValueType + variation: (string, boolean) => FlagValueType, + identify: (Object, Object | null) => void }; export type FeatureFlagType = { flagKey: string, renderFeatureCallback: (FlagValueType) => ?React$Element, renderDefaultCallback?: () => ?React$Element, - initialRenderCallback?: () => ?React$Element + initialRenderCallback?: () => ?React$Element, + identify?: IdentifyType }; export type ConfigType = { @@ -44,3 +46,12 @@ export type ClientOptionsType = { disableClient?: boolean }; + +export type IdentifyType = { + key?: string, + firstName?: string, + lastName?: string, + email?: string, + custom: Object, + hash?: Object +};