Commit 52c4f8ef authored by Filipa Lacerda's avatar Filipa Lacerda

Stores environmnets in tree

parent 2c0f97cd
......@@ -40,7 +40,7 @@ $(() => {
*/
ready() {
gl.environmentsService.all().then((resp) => {
Store.addEnvironments(resp.json());
Store.storeEnvironments(resp.json());
this.loading = false;
});
......
......@@ -3,19 +3,58 @@
window.gl.environmentsList = window.gl.environmentsList || {};
gl.environmentsList.EnvironmentsStore = {
state: {},
create () {
this.state.environments = [];
},
/**
* Stores the received environmnets.
* In order to display a tree view we need to modify the received
* data in to a tree structure based on `environment_type`
* sorted alphabetically.
*
* @example
* it will transform this:
* [
* { name: "environment", environment_type: "review" },
* { name: "environment_1", environment_type: null }
* { name: "environment_2, environment_type: "review" }
* ]
* into this:
* [
* { name: "review", children:
* [
* { name: "environment", environment_type: "review"},
* { name: "environment_2", environment_type: "review"}
* ]
* },
* {name: "environment_1", environment_type: null}
* ]
* @param {Array} environments List of environments
* @return {type}
*/
addEnvironments(environments) {
console.log(environments);
storeEnvironments(environments) {
this.state.environments = environments.reduce((acc, environment) => {
if (environment.environment_type !== null) {
const occurs = acc.find((element, index, array) => {
return element.name === environment.environment_type;
});
if (occurs !== undefined) {
acc[acc.indexOf(occurs)].children.push(environment);
acc[acc.indexOf(occurs)].children.sort();
} else {
acc.push({
name: environment.environment_type,
children: [environment]
});
}
} else {
acc.push(environment);
}
return acc;
}, []).sort();
}
}
})();
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment