Skip to content

Commit 01d2f72

Browse files
Feature react sdk (#58)
* feat: User guidance for React SDK
1 parent 5b8b247 commit 01d2f72

File tree

7 files changed

+92
-4
lines changed

7 files changed

+92
-4
lines changed

src/components/Rule/condition.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ test('RuleContent datetime input', (done) => {
292292
);
293293

294294
await userEvent.click(screen.getByPlaceholderText('Please select'));
295-
await userEvent.click(screen.getByText(`20`));
295+
await userEvent.click(screen.getByText('20'));
296296

297297
await userEvent.click(screen.getAllByText('UTC +8')[0]);
298298
await userEvent.click(screen.getByText('UTC +3'));

src/images/react.svg

Lines changed: 1 addition & 0 deletions
Loading

src/locales/en-US.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,10 @@
421421
"getstarted.miniprogram.third.step.title": "Step 3: Use the instance to get your setting value",
422422
"getstarted.miniprogram.third.step.name.one": "You can get variation value from globalData object, SDK will update globalData.toggles object automatically",
423423
"getstarted.miniprogram.third.step.name.second": "You can also use the SDK API to check which value this user will receive for a given feature toggle",
424+
"getstarted.react.first.step.title": "Step1: Use create-react-app to create a new React application",
425+
"getstarted.react.second.step.title": "Step2: Install SDK",
426+
"getstarted.react.third.step.title": "Step3: Import FPProvider in App.js",
427+
"getstarted.react.fourth.step.title": "Step4: Create a new file home.js in the same directory as App.js, import withFPConsumer in home.js",
424428

425429
"platform.warning.text": "According to the Cybersecurity Law, it is prohibited to use FeatureProbe for illegal financial lending, cryptocurrency, gambling and any applications containing Telemarketing Scams functions, once discovered, you will be immediately stopped from using FeatureProbe and reported to the Cyber Security Department.",
426430

src/locales/zh-CN.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,10 @@
421421
"getstarted.miniprogram.third.step.title": "第三步:使用功能开关",
422422
"getstarted.miniprogram.third.step.name.one": "您可以从 globalData 对象中获取变量值,SDK会自动更新 globalData.toggles 对象",
423423
"getstarted.miniprogram.third.step.name.second": "您还可以使用 SDK 对外暴露的 API 来获取开关返回值",
424+
"getstarted.react.first.step.title": "第一步:使用create-react-app新建一个React项目",
425+
"getstarted.react.second.step.title": "第二步:安装SDK",
426+
"getstarted.react.third.step.title": "第三步:在App.js中引入FPProvider",
427+
"getstarted.react.fourth.step.title": "第四步:在App.js同级目录下创建home.js,并在home.js中引入withFPConsumer",
424428

425429
"platform.warning.text": "根据国家相关规定,严禁将FeatureProbe用于违法金融借贷、区块链虚拟币、博彩以及任何含有网络电信诈骗功能的应用程序,一旦被发现,将会立即停止你使用FeantureProbe相关功能,并上报给网络安全监管部门。",
426430

src/pages/getStarted/components/StepFirst/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import android from 'images/android.svg';
1414
import swift from 'images/swift.svg';
1515
import apple from 'images/apple.svg';
1616
import miniprogram from 'images/wechat-miniprogram.png';
17+
import reactLogo from 'images/react.svg';
1718
import styles from '../Steps/index.module.scss';
1819
import { SdkLanguage } from '../StepSecond';
1920

@@ -29,6 +30,7 @@ const SDK_LOGOS: {[key in SdkLanguage]: string} = {
2930
'Swift': swift,
3031
'Objective-C': apple,
3132
'Mini Program': miniprogram,
33+
'React': reactLogo,
3234
};
3335

3436
const SERVER_SIDE_SDKS = [
@@ -74,6 +76,10 @@ const CLIENT_SIDE_SDKS = [
7476
{
7577
name: 'Mini Program',
7678
logo: miniprogram,
79+
},
80+
{
81+
name: 'React',
82+
logo: reactLogo,
7783
}
7884
];
7985

src/pages/getStarted/components/StepSecond/index.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ import {
2121
getRustCode,
2222
getSwiftCode,
2323
getMiniProgramCode,
24+
getReactCode,
2425
} from '../constants';
2526
import styles from '../Steps/index.module.scss';
2627

27-
export type SdkLanguage = 'Java'
28-
// | 'Java'
28+
export type SdkLanguage =
29+
'Java'
2930
| 'Python'
3031
| 'Rust'
3132
| 'Go'
@@ -34,7 +35,8 @@ export type SdkLanguage = 'Java'
3435
| 'Swift'
3536
| 'Objective-C'
3637
| 'JavaScript'
37-
| 'Mini Program';
38+
| 'Mini Program'
39+
| 'React';
3840

3941
interface IProps {
4042
rules: IRule[]
@@ -258,6 +260,20 @@ const StepSecond = (props: IProps) => {
258260
remoteUrl,
259261
}));
260262
break;
263+
case 'React':
264+
saveLanguage('javascript');
265+
result.forEach(item => {
266+
userWithCode += `user.with("${item}", /* ${item} */);\n `;
267+
});
268+
saveOptions(getReactCode({
269+
clientSdkKey,
270+
toggleKey,
271+
returnType,
272+
intl,
273+
userWithCode,
274+
remoteUrl,
275+
}));
276+
break;
261277
}
262278
}
263279
}, [rules, sdkVersion, currentSDK, clientSdkKey, serverSdkKey, toggleKey, returnType, intl, remoteUrl]);

src/pages/getStarted/components/constants.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,60 @@ const value = app.globalData.toggles[${toggleKey}].value;
409409
}
410410
];
411411
};
412+
413+
export const getReactCode = (options: IOption) => {
414+
const { intl, clientSdkKey, userWithCode, returnType, toggleKey, remoteUrl } = options;
415+
return [
416+
{
417+
title: intl.formatMessage({id: 'getstarted.react.first.step.title'}),
418+
code: 'npx create-react-app react-demo && cd react-demo'
419+
},
420+
{
421+
title: intl.formatMessage({id: 'getstarted.react.second.step.title'}),
422+
code: 'npm install featureprobe-client-sdk-react --save'
423+
},
424+
{
425+
title: intl.formatMessage({id: 'getstarted.react.third.step.title'}),
426+
code:
427+
`import { FPProvider } from 'featureprobe-client-sdk-react';
428+
import Home from './home';
429+
430+
function App() {
431+
const user = new FPUser();
432+
${userWithCode}
433+
return (
434+
<FPProvider
435+
config={{
436+
remoteUrl: "${remoteUrl}",
437+
clientSdkKey: "${clientSdkKey}",
438+
user,
439+
}}
440+
>
441+
<Home />
442+
</FPProvider>
443+
);
444+
}
445+
446+
export default App;
447+
`
448+
},
449+
{
450+
title: intl.formatMessage({id: 'getstarted.react.fourth.step.title'}),
451+
code:
452+
`import { withFPConsumer } from 'featureprobe-client-sdk-client';
453+
454+
const Home = ({ toggles, client }) => {
455+
${returnType === 'boolean' ? `const value = client?.boolValue("${toggleKey}", false);` : ''}${returnType === 'number' ? `const value = client?.numberValue("${toggleKey}", 1.0);` : ''}${returnType === 'string' ? `const value = client?.stringValue("${toggleKey}", "s");` : ''}${returnType === 'json' ? `const value = client.jsonValue("${toggleKey}", {});` : ''}
456+
return (
457+
<div>
458+
<div>You can use toggle value like this: \${value}</div>
459+
<div>You can also get toggle detail from toggles object like this: \${toggles?.["${toggleKey}"]}</div>
460+
</div>
461+
)
462+
};
463+
464+
export default withFPConsumer(Home);
465+
`
466+
},
467+
];
468+
};

0 commit comments

Comments
 (0)