Skip to content

Commit 0870ca4

Browse files
authored
Merge pull request #15 from TheMagoo73/add-traits-support
Add traits support
2 parents 3f65b36 + 9a6abab commit 0870ca4

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

index.d.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,31 @@ export interface Flagsmith {
6161
* Force a re-evaluation and fetch of flags
6262
*/
6363
getFlags:() => Promise<IFlags>
64+
65+
/**
66+
* Get the value of a specific trait for the currently identified user
67+
*/
68+
getTrait:(key:string) => string|number|boolean
69+
70+
/**
71+
* Set the value of a trait for the current user. Triggers a fetch of the flags
72+
*/
73+
setTrait:(key: string, value: string|number|boolean) => Promise<IFlags>
74+
75+
/**
76+
* Increment the value of a trait for the current user. Triggers a fetch of the flags
77+
*/
78+
incrementTrait:(key: string, incrementBy: number) => Promise<IFlags>
79+
80+
/**
81+
* Set a key value set of traits for a given user, triggers a fetch of the flags
82+
*/
83+
setTraits:(traits: Record<string, string|number|boolean>) => Promise<IFlags>
84+
85+
6486
}
6587

88+
6689
/**
6790
* Hook function to access Flagsmith functionality
6891
*/

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flagsmith-react",
3-
"version": "0.1.1-alpha.3",
3+
"version": "0.1.2-alpha.1",
44
"preview": true,
55
"description": "Flagsmith integration for React Single Page Applications (SPA)",
66
"repository": {

src/flagsmith-provider.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,30 @@ const FlagsmithProvider = ({ environmentId, children, flagsmith = reactFlagsmith
9494
}, [flagsmith]
9595
)
9696

97+
const getTrait = useCallback(
98+
(key) => {
99+
return flagsmith.getTrait(key)
100+
}, [flagsmith]
101+
)
102+
103+
const setTrait = useCallback(
104+
async (key, value) => {
105+
return flagsmith.setTrait(key, value)
106+
}, [flagsmith]
107+
)
108+
109+
const incrementTrait = useCallback(
110+
async (key, incrementBy) => {
111+
return flagsmith.incrementTrait(key, incrementBy)
112+
}, [flagsmith]
113+
)
114+
115+
const setTraits = useCallback(
116+
async (traits) => {
117+
return flagsmith.setTraits(traits)
118+
}, [flagsmith]
119+
)
120+
97121
return (
98122
<FlagsmithContext.Provider value={{
99123
...state,
@@ -104,7 +128,11 @@ const FlagsmithProvider = ({ environmentId, children, flagsmith = reactFlagsmith
104128
logout,
105129
startListening,
106130
stopListening,
107-
getFlags}}>
131+
getFlags,
132+
getTrait,
133+
setTrait,
134+
setTraits,
135+
incrementTrait}}>
108136
{children}
109137
</FlagsmithContext.Provider>
110138
)

0 commit comments

Comments
 (0)