Skip to content
This repository was archived by the owner on Dec 7, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/FeatureFlag.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
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";

export default function FeatureFlag (props:FeatureFlagType) {
return (
<Subscriber channel={BROADCAST_CHANNEL}>
{
(config) => {
(config:ConfigType) => {
if (config) {
return (<FeatureFlagRenderer {...config} {...props} />);
}
Expand Down
22 changes: 20 additions & 2 deletions src/components/FeatureFlagRenderer.js
Original file line number Diff line number Diff line change
@@ -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<Props, State> {
Expand Down Expand Up @@ -42,6 +48,7 @@ export default class FeatureFlagRenderer extends Component<Props, State> {

this.checkFeatureFlag(ldClient);
this.listenFlagChangeEvent(ldClient);
this.identify(ldClient);
}

componentWillUnmount () {
Expand Down Expand Up @@ -86,6 +93,17 @@ export default class FeatureFlagRenderer extends Component<Props, State> {
});
}

identify (ldClient: LdClientWrapperType) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should add a test for this

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely, still trying to get these flow errors resolved.

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;
Expand Down
15 changes: 13 additions & 2 deletions src/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>,
renderDefaultCallback?: () => ?React$Element<any>,
initialRenderCallback?: () => ?React$Element<any>
initialRenderCallback?: () => ?React$Element<any>,
identify?: IdentifyType
};

export type ConfigType = {
Expand Down Expand Up @@ -44,3 +46,12 @@ export type ClientOptionsType = {

disableClient?: boolean
};

export type IdentifyType = {
key?: string,
firstName?: string,
lastName?: string,
email?: string,
custom: Object,
hash?: Object
};