Giter Club home page Giter Club logo

ld-react-components's Introduction

LD React Components

Semantic component helpers to support LaunchDarkly in your react app.

Build Coverage All Contributors Npm Downloads Vulnerabilities License Scan Auto Release Example

Usage

Install node module

You can use npm or yarn however it is advised to choose one and stick with it. For the purposes of documentation yarn is being used.

yarn add ld-react-components

Importing the components

import {
  FeatureFlag,
  FeatureSwitch,
  FeatureCase,
  FeatureTrue,
  FeatureFalse
} from 'ld-react-components';

API initialization

this._ldclientPromise = launchDarklyClient.initWithPromise(
  user,
  this._sdkKey,
  500
);

const endpoints = {
  baseUrl: 'https://app.launchdarkly.com',
  eventsUrl: 'https://events.launchdarkly.com',
  streamUrl: 'https://stream.launchdarkly.com',
  baseTimeout: 100
};

this._ldclientPromise = launchDarklyClient.initWithPromise(
  user,
  this._sdkKey,
  endpoints,
  500
);

FeatureFlag

Takes flagKey and appFlags as props, which is an object containing list of features.

const applicationKeys = {
  'integration-test': { value: true, version: 3 },
  'multivariate-test': { value: 'multivariate-test-1', version: 5 }
}
<FeatureFlag flagKey="multivariate-test" appFlags={applicationKeys}></FeatureFlag>

FeatureSwitch, FeatureCase and FeatureDefault

FeatureSwitch should be a child of FeatureFlag and can take FeatureCase and FeatureDefault as children.

FeatureCase component takes condition and allowBreak(a boolean) as props, condition is the case feature, while allowBreak used as a break. The reason for name change is case and break are reserved words on JS.

<FeatureFlag flagKey="multivariate-test" appFlags={applicationKeys}>
  <FeatureSwitch>
    <FeatureCase condition="multivariate-test-1" allowBreak>
      <p>Multivariate Test 1 Rendered</p>
    </FeatureCase>
    <FeatureCase condition="multivariate-test-2" allowBreak>
      <p>Multivariate Test 2 Rendered</p>
    </FeatureCase>
    <FeatureCase condition="multivariate-test-3" allowBreak>
      <p>Multivariate Test 3 Rendered</p>
    </FeatureCase>
    <FeatureCase condition="multivariate-test-4" allowBreak>
      <p>Multivariate Test 4 Rendered</p>
    </FeatureCase>
    <FeatureDefault>
      <p>If no conditions are met then render the default</p>
    </FeatureDefault>
  </FeatureSwitch>
</FeatureFlag>

FeatureTrue and FeatureFalse

<FeatureFlag flagKey="integration-test" appFlags={applicationKeys}>
  <FeatureTrue>
    <p>If feature flag is true, then is content will render.</p>
  </FeatureTrue>
  <FeatureFalse>
    <p>If feature flag is false, then is content will render.</p>
  </FeatureFalse>
</FeatureFlag>

Another Use Case

const applicationKeys = {
  'multivariate-test': { value: 'multivariate-test-2', version: 1},
  'integration-test': { value: true }
};
<FeatureFlag flagKey="false-test" appFlags={applicationKeys}>
  <p>This non-component should get rendered</p>
  This is also should get rendered.
  <FeatureTrue>This one should throw a warning and wont be rendred</FeatureTrue>
  <FeatureFalse>
    this one should throw a warning and wont be rendred
  </FeatureFalse>
  <FeatureSwitch>
    <FeatureCase condition="multivariate-test-1" allowBreak>
      <p>This one should throw an error and wont be rendred</p>
    </FeatureCase>
  </FeatureSwitch>
</FeatureFlag>

Nested FeatureFlag

const applicationKeys = {
  'multivariate-test': { value: 'multivariate-test-2' },
  'integration-test': { value: true }
};
<FeatureFlag flagKey="multivariate-test" appFlags={applicationKeys}>
  <p>This non-component will get rendered</p>
  <FeatureFlag flagKey="multivariate-test" appFlags={flags}>
    <FeatureSwitch>
      <FeatureCase condition="multivariate-test-1" allowBreak>
        <p>Multivariate Test 1 Rendered</p>
      </FeatureCase>
      <FeatureCase condition="multivariate-test-2" allowBreak>
        <p>This one will get rendered(Multivariate Test 2 Rendered)</p>
      </FeatureCase>
      <FeatureCase condition="multivariate-test-3" allowBreak>
        <p>Multivariate Test 3 Rendered</p>
      </FeatureCase>
      <FeatureDefault allowBreak>
        <p>This is the default content if no other cases are matched.</p>
      </FeatureDefault>
    </FeatureSwitch>
  </FeatureFlag>
</FeatureFlag>

Using the React Hooks

const appFlags = {
  a: { value: 'a' },
  b: { value: 'b' },
  c: { value: 'c' },
  d: { value: 'd' },
  e: { value: 'e' }
};

const UsingHooks = () => {
  const [count, setCount] = useState(65);
  return (
    <div>
      <button onClick={() => setCount(count + 1)}>Add count</button>
      <FeatureFlag
        flagKey={String.fromCharCode(count).toLowerCase()}
        appFlags={appFlags}
      >
        <FeatureSwitch>
          <FeatureCase condition="a" allowBreak>
            A is being rendered
          </FeatureCase>
          <FeatureCase condition="b" allowBreak>
            B is being rendered
          </FeatureCase>
          <FeatureCase condition="c" allowBreak>
            C is being rendered
          </FeatureCase>
          <FeatureCase condition="d" allowBreak>
            D is being rendered
          </FeatureCase>
          <FeatureCase condition="e" allowBreak>
            E is being rendered
          </FeatureCase>
          <FeatureDefault>No value matches, this is default</FeatureDefault>
        </FeatureSwitch>
      </FeatureFlag>
    </div>
  );
};

Using the API

Importing

import launchDarklyClient from 'ld-react-components/API';

const endpoints = {
  baseUrl: 'https://app.launchdarkly.com',
  eventsUrl: 'https://events.launchdarkly.com',
  streamUrl: 'https://stream.launchdarkly.com',
  baseTimeout: 100
};
this._ldclientPromise = launchDarklyClient.initWithPromise(user, this._sdkKey, endpoints, 500);

getFeatureFlag(featureId, defaultValue = false) {
  if (typeof window !== 'undefined') {
    return new Promise((resolve, reject) => {
      this._ldclientPromise
      .then((client) => {
        resolve(client.getFeatureFlag(featureId, defaultValue));
      })
      .catch((error) => {
        reject(error);
      });
    });
  }
}

For development

For the API

Testing

yarn
yarn test

For React

The module includes a demo demonstrating how to use the components

yarn
yarn dev

To see the demo go to http://localhost:8080

Contributors โœจ


Dave Bergschneider

๐Ÿ’ป ๐ŸŽจ ๐Ÿ“– ๐Ÿ’ก ๐Ÿšง

Andrew Lisowski

๐Ÿš‡

Harshit Jain

๐Ÿ“–

Jakob S

๐Ÿšง

Vasiliy Vanchuk

๐Ÿš‡

Buranch

๐Ÿ’ป

Shubham Arora

๐Ÿ’ป

Gary Grumbley

๐Ÿš‡ ๐Ÿ“–

Bennett Hreherchuk

โš ๏ธ

Jose Diego

๐Ÿš‡ ๐Ÿ’ป ๐Ÿ“– โš ๏ธ

Sam Nesbitt

๐Ÿ’ป

Pramod

๐Ÿ’ป

Mohit Mayank

๐Ÿ’ป

ankita sinha

๐Ÿ’ป

Kausam

๐Ÿ’ป

Rohan Patel

๐Ÿ“–

tekgal

๐Ÿš‡

Norman Yee

๐Ÿš‡

Himanshu Pant

๐Ÿ“–

Trevor Erwin

๐Ÿ›

Winter Faulk

๐Ÿšง

Susmitha Kodamarthi

โš ๏ธ

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

Adding a Contributor

To add a contributor comment on Issue or Pull Request, asking @all-contributors to add a contributor:

Example: @all-contributors please add <username> for <contributions>

<contribution>: See the Emoji Key (Contribution Types Reference) for a list of valid contribution types.

The bot will then create a Pull Request to add the contributor, then reply with the pull request details.

License

FOSSA Status

ld-react-components's People

Contributors

allcontributors[bot] avatar anki08 avatar buranch avatar dependabot[bot] avatar erwintj avatar faulker avatar funkadelic avatar ggrumbley avatar himanshupnt avatar hipstersmoothie avatar hjaintech avatar hreherch avatar ishubhamarora avatar josediego avatar kausam avatar mmissey avatar mmynk avatar poorpaddy avatar rohan8594 avatar skodamarthi avatar snyk-bot avatar swazimodo avatar vvscode avatar zjael avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

ld-react-components's Issues

Setup & Configure Linting

This repo is coming along nicely in terms of quality control but it can always improve. Perhaps adding some linting will help further improve it. Ideally this repo would be setup using the configuration from https://github.com/paulolramos/eslint-prettier-airbnb-react.

Then of course without modifying a bunch of rules go thru and fix the issues found. Once issues are found we can then add the prettier badge to the readme and feel good about it.

Github Pages Builds Failing

Describe the bug

Currently when merging/releasing a new version, CircleCi successfully builds the main portion of the project however it fails to force push to the orphaned gh-branch for publishing the demo/docs.

To Reproduce

Create a PR, merge PR then look at the CircleCi builds. Alternatively, can visit the following link to see failing builds in CircleCi - https://circleci.com/gh/intuit/LD-React-Components

Expected behavior

Releases will successfully push to the gh-pages branch and report back a passing build to Github.

Update NPM Dependencies

Is your feature request related to a problem? Please describe.

Some of the dependencies are out of date and it is crucial for the maintenance of this package to keep up with React and TypeScript. This may mean updating code for compatibility with newer standards.

An easy way to do this is to setup npm-check-updates via yarn global add npm-check-updates.
This will provide a list of all dependencies needing to be updated and wether they are major breaking changes or not.

Example in screenshot below:
Screen Shot 2020-09-10 at 12 37 43 PM

Resolve Dependencybot Issues

Describe the bug

There are some notable security issues found in the dependency tree. Please review and resolve them.

Screenshots

Screen Shot 2020-09-10 at 12 10 56 PM

Refactor for TypeScript: Scaffolding

There's a lot of benefit to using TypeScript for collaboration but also for self documenting code. Would be nice to have this project follow suit.

This issue is for setting up the scaffolding to support incremental TS building.

[ ] Backwards compatible
[ ] Tests Pass
[ ] Linting Passes

Troubleshoot CircleCi Failing Status

I have a theory. While builds are succeeding and the gh-page's branch is now being correctly skipped. It's old status of failed on the gh-pages branch is being syndicated since it reports the last known status for skipped branches. Just a theory.

The goal is to once again have a passing status come from CircleCi. It may even require contacting and working with them to fix.

Components Are Unreachable

It would appear since the change over to TypeScript. The js files in dist are no longer found when compiling inside applications. Please see code block below.

ERROR in ../node_modules/ld-react-components/API/index.js
Module not found: Error: Can't resolve '../dist/LaunchDarklyClient/index.js' in '/Users/dbergschneider/site/node_modules/ld-react-components/API'

ERROR in ../node_modules/ld-react-components/dist/index.js
Module not found: Error: Can't resolve './FeatureCase' in '/Users/dbergschneider/site/node_modules/ld-react-components/dist'

ERROR in ../node_modules/ld-react-components/dist/index.js
Module not found: Error: Can't resolve './FeatureDefault' in '/Users/dbergschneider/site/node_modules/ld-react-components/dist'

ERROR in ../node_modules/ld-react-components/dist/index.js
Module not found: Error: Can't resolve './FeatureFalse' in '/Users/dbergschneider/site/node_modules/ld-react-components/dist'

ERROR in ../node_modules/ld-react-components/dist/index.js
Module not found: Error: Can't resolve './FeatureSwitch' in '/Users/dbergschneider/site/node_modules/ld-react-components/dist'

ERROR in ../node_modules/ld-react-components/dist/index.js
Module not found: Error: Can't resolve './FeatureTrue' in '/Users/dbergschneider/site/node_modules/ld-react-components/dist'

ERROR in ../node_modules/ld-react-components/dist/index.js
Module not found: Error: Can't resolve './LaunchDarklyClient' in '/Users/dbergschneider/site/node_modules/ld-react-components/dist'

Refactor for TypeScript: LaunchDarklyClient

This is one part of a set of issues for refactoring to TypeScript.

Refactor LaunchDarklyClient component to TypeScript (src/lib/LaunchDarklyClient).

[ ] Passes linting
[ ] Unit Tests Pass

Dependency Updates

Update all current dependencies and run tests and ensure tests are still passing.

Commit Lint Support

Is your feature request related to a problem? Please describe.
Since we have multiple contributors , its essential to maintain commit hygiene and ensure that commit message conventions are followed

Describe the solution you'd like

Setup Commitlint and configure a husky pre-commit hook to ensure that all commit messages are checked.

Describe alternatives you've considered

Commitlint paired with husky is the most recommended setup

Additional context

Screenshot 2020-10-01 at 11 13 39 PM

References not updated for extension changes

Describe the bug

When a large majority of file extensions were changed from .js the references were not changed accordingly.

Example:

var LDApi = require('../dist/LaunchDarklyClient/index.js');

However what ends up in dist is /dist/LaunchDarklyClient/index.ts.

I believe this is also affecting import references that assume .js and not .ts extensions such as import FeatureCase from '../FeatureCase';.

Also package.json references index.js that doesn't exist.

"main": "dist/index.js",

Fix Contribution Generation

Describe the bug

Currently this repo utilized all-contributors-cli for adding contributors to the readme. At this time it would appear the generator is not working correctly.

To Reproduce

  1. Clone package and run yarn to install dependencies.
  2. Run yarn contributors:add to start the process of adding a contributor.
  3. Select any option and it should encounter an error.

Expected behavior

Able to get thru whole process and contributors are automatically added to the correct place in the readme file.

All Contributors Bot

Is your feature request related to a problem? Please describe.

Currently contributors must manually add themselves as contributors via CLI. Ideally this would be a step that can be skipped if the all-contributors-bot were implemented.

Describe the solution you'd like

Implement the all-contributors-bot so that contributors are automatically added to the readme and the all contributors badge count number is updated to correctly reflected.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.