Skip to content

Commit

Permalink
Add user trait functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-ssg committed Nov 13, 2018
1 parent 4bf6b50 commit 5c131e0
Show file tree
Hide file tree
Showing 15 changed files with 4,283 additions and 4,229 deletions.
1,854 changes: 927 additions & 927 deletions bullet-train-client/example/package-lock.json

Large diffs are not rendered by default.

32 changes: 24 additions & 8 deletions bullet-train-client/example/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,43 @@ export default class App extends Component {
this.forceUpdate();
};

submitTrait = ()=> {
bulletTrain.setTrait('example_trait', !bulletTrain.getTrait('example_trait'));
}

render() {

const fontSize = parseInt(bulletTrain.getValue("font_size"));
const trait = bulletTrain.getTrait("example_trait") + "";
const {submitTrait} = this;
const {isLoading, logs} = this.state;
return isLoading ? <div>Loading</div> : (
<div>
<h2>{environmentID}</h2>
<p style={{fontSize}}>
{JSON.stringify(bulletTrain.flags)}
</p>
<h3>
Events
</h3>
{bulletTrain.identity ? (
<button onClick={this.logout}>
logout
</button>
<div>
<div>
<button onClick={submitTrait}>
Toggle user trait
</button>
<div>
example_trait: {trait}
</div>
</div>
<button onClick={this.logout}>
logout
</button>
</div>
) : <button onClick={this.login}>
Login as sample user
</button>}
{logs.map(({timestamp, data, params, oldData},i) => (
<h3>
Events
</h3>
{logs.map(({timestamp, data, params, oldData}, i) => (
<p key={i}>
{timestamp}: {data} {params} {oldData}
</p>
Expand All @@ -76,7 +92,7 @@ export default class App extends Component {
timestamp: new Date().toDateString(),
params: JSON.stringify(params),
oldData: JSON.stringify(oldFlags),
data: JSON.stringify(bulletTrain.getAllFlags())
data: JSON.stringify(bulletTrain.getAllFlags()),
}].concat(this.state.logs)
});
};
Expand Down
2 changes: 1 addition & 1 deletion bullet-train-client/example/src/bullet-train.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bullet-train-client/example/src/bullet-train.js.map

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions bullet-train-client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bullet-train-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bullet-train-client",
"version": "0.0.35",
"version": "0.0.40",
"description": "Feature flagging to support continuous development",
"main": "./lib/index.js",
"repository": {
Expand Down
54 changes: 37 additions & 17 deletions bullet-train-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,60 @@ const BulletTrain = class {
AsyncStorage = props.AsyncStorage;
}

getJSON = (url, method) => {
const {environmentID} = this;
console.log(url, environmentID)
return fetch(url + '?format=json', {
getJSON = (url, method, body) => {
const { environmentID } = this;
const options = {
method: method || 'GET',
body,
headers: {
'x-environment-key': environmentID
}
})
};
if (method !== "GET")
options.headers['Content-Type'] = 'application/json; charset=utf-8'

return fetch(url + '?format=json', options)
.then(res => res.json());
};

getFlags = () => {
const {onChange, onError, identity, api, disableCache} = this;
const { onChange, onError, identity, api, disableCache } = this;

const handleResponse = (res) => {
const handleResponse = ({ flags: features, traits }) => {
// Handle server response
let flags = {};
res.forEach(feature => {
let userTraits = {};
features.forEach(feature => {
flags[feature.feature.name.toLowerCase().replace(/ /g, '_')] = {
enabled: feature.enabled,
value: feature.feature_state_value
};
});
traits.forEach(trait => {
userTraits[trait.trait_key.toLowerCase().replace(/ /g, '_')] = trait.trait_value
});
this.oldFlags = flags;
this.flags = flags;
this.traits = userTraits;
if (!disableCache) {
AsyncStorage.setItem(BULLET_TRAIN_KEY, JSON.stringify(this.flags))
}
onChange && onChange(this.oldFlags, {isFromServer: true});
onChange && onChange(this.oldFlags, { isFromServer: true });
};

if (identity) {
return this.getJSON(api + 'flags/' + identity)
return this.getJSON(api + 'identities/' + identity + '/')
.then(res => {
handleResponse(res)
}).catch(({message}) => {
onError && onError({message})
}).catch(({ message }) => {
onError && onError({ message })
});
} else {
return this.getJSON(api + "flags/")
.then(res => {
handleResponse(res)
}).catch(({message}) => {
onError && onError({message})
}).catch(({ message }) => {
onError && onError({ message })
});
}
};
Expand Down Expand Up @@ -89,7 +98,7 @@ const BulletTrain = class {
AsyncStorage.getItem(BULLET_TRAIN_KEY, (err, res) => {
this.flags = res ? JSON.parse(res) : defaultFlags;
if (this.flags) {
onChange(null, {isFromServer: false});
onChange(null, { isFromServer: false });
}
this.getFlags();
});
Expand Down Expand Up @@ -134,6 +143,17 @@ const BulletTrain = class {
return res;
}

getTrait = (key) => {
const trait = this.traits && this.traits[key];
return trait;
}

setTrait = (key, trait_value) => {
const { getJSON, identity, api } = this;

return getJSON(`${api}identities/${identity}/traits/${encodeURIComponent(key)}`, 'POST', JSON.stringify({ trait_value }))
.then(this.getFlags)
};

hasFeature = (key) => {
const flag = this.flags && this.flags[key];
Expand All @@ -148,8 +168,8 @@ const BulletTrain = class {

};

module.exports = function ({fetch, AsyncStorage}) {
return new BulletTrain({fetch, AsyncStorage});
module.exports = function ({ fetch, AsyncStorage }) {
return new BulletTrain({ fetch, AsyncStorage });
};


0 comments on commit 5c131e0

Please sign in to comment.