svishnev | d3886bb | 2018-01-08 15:21:46 +0200 | [diff] [blame] | 1 | /*! |
| 2 | * Copyright © 2016-2017 European Support Limited |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 13 | * or implied. See the License for the specific language governing |
| 14 | * permissions and limitations under the License. |
| 15 | */ |
| 16 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 17 | /** |
svishnev | d3886bb | 2018-01-08 15:21:46 +0200 | [diff] [blame] | 18 | * Feature toggling decorator |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 19 | * usage: |
| 20 | * |
svishnev | d3886bb | 2018-01-08 15:21:46 +0200 | [diff] [blame] | 21 | * @featureToggle('FeatureName') |
| 22 | * class Example extends React.Component { |
| 23 | * render() { |
| 24 | * return (<div>test feature</div>); |
| 25 | * } |
| 26 | * } |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 27 | * |
| 28 | * OR |
| 29 | * |
svishnev | d3886bb | 2018-01-08 15:21:46 +0200 | [diff] [blame] | 30 | * const TestFeature = () => (<div>test feature</div>) |
| 31 | * export default featureToggle('FeatureName')(TestFeature) |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 32 | * |
svishnev | d3886bb | 2018-01-08 15:21:46 +0200 | [diff] [blame] | 33 | */ |
| 34 | |
| 35 | import React from 'react'; |
| 36 | import PropTypes from 'prop-types'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 37 | import { connect } from 'react-redux'; |
svishnev | d3886bb | 2018-01-08 15:21:46 +0200 | [diff] [blame] | 38 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 39 | export const FeatureComponent = props => { |
| 40 | const { features = [], featureName, InnerComponent, ...otherProps } = props; |
| 41 | const AComp = InnerComponent.AComp ? InnerComponent.AComp : InnerComponent; |
| 42 | |
| 43 | return !!features.find(el => el.name === featureName && el.active) ? ( |
| 44 | <AComp {...otherProps} /> |
| 45 | ) : InnerComponent.BComp ? ( |
| 46 | <InnerComponent.BComp {...otherProps} /> |
| 47 | ) : null; |
svishnev | d3886bb | 2018-01-08 15:21:46 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | FeatureComponent.propTypes = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 51 | features: PropTypes.array, |
| 52 | featureName: PropTypes.string.isRequired |
svishnev | d3886bb | 2018-01-08 15:21:46 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
svishnev | d3886bb | 2018-01-08 15:21:46 +0200 | [diff] [blame] | 55 | export default function featureToggle(featureName) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 56 | return InnerComponent => { |
| 57 | return connect(({ features }) => { |
| 58 | return { features, featureName, InnerComponent }; |
| 59 | })(FeatureComponent); |
| 60 | }; |
svishnev | d3886bb | 2018-01-08 15:21:46 +0200 | [diff] [blame] | 61 | } |