Commit 2207528c authored by Filipa Lacerda's avatar Filipa Lacerda

Uses vue computed data

Fixes some eslint errors
Adds some documentation
Improves data manipulation
parent 4ea04c40
/* globals Vue */
/* eslint-disable no-param-reassign, no-return-assign */
(() => { (() => {
/** /**
* Envrionment Item Component * Envrionment Item Component
* *
...@@ -19,12 +20,12 @@ ...@@ -19,12 +20,12 @@
template: '#environment-item-template', template: '#environment-item-template',
props: { props: {
model: Object model: Object,
}, },
data () { data() {
return { return {
open: false open: false,
}; };
}, },
...@@ -37,8 +38,8 @@ ...@@ -37,8 +38,8 @@
* *
* @returns {Boolean} * @returns {Boolean}
*/ */
isFolder () { isFolder() {
return this.model.children && this.model.children.length ? true : false; return this.$options.hasKey(this.model, 'children') && this.model.children.length > 0;
}, },
/** /**
...@@ -47,7 +48,7 @@ ...@@ -47,7 +48,7 @@
* *
* @returns {Boolean|undefined} * @returns {Boolean|undefined}
*/ */
isChildren () { isChildren() {
return this.model['vue-isChildren']; return this.model['vue-isChildren'];
}, },
...@@ -57,9 +58,79 @@ ...@@ -57,9 +58,79 @@
* *
* @returns {Boolean} The number of environments for the current folder * @returns {Boolean} The number of environments for the current folder
*/ */
childrenCounter () { childrenCounter() {
return this.model.children && this.model.children.length; return this.$options.hasKey(this.model, 'children') && this.model.children.length;
} },
/**
* Returns the value of the `last?` key sent in the API.
* Used to know wich title to render when the environment can be re-deployed
*
* @returns {Boolean}
*/
isLast() {
return this.$options.hasKey(this.model, 'last_deployment') && this.model.last_deployment['last?'];
},
/**
* Verifies if `last_deployment` key exists in the current Envrionment.
* This key is required to render most of the html - this method works has
* an helper.
*
* @returns {Boolean}
*/
hasLastDeploymentKey() {
return this.$options.hasKey(this.model, 'last_deployment');
},
/**
* Verifies is the given environment has manual actions.
* Used to verify if we should render them or nor.
*
* @returns {Boolean} description
*/
hasManualActions() {
return this.$options.hasKey(this.model, 'manual_actions') && this.model.manual_actions.length;
},
/**
* Returns the value of the `stoppable?` key provided in the response.
*
* @returns {Boolean}
*/
isStoppable() {
return this.model['stoppable?'];
},
/**
* Verifies if the `deployable` key is present in `last_deployment` key.
* Used to verify whether we should or not render the rollback partial.
*
* @returns {Boolean}
*/
canRetry() {
return this.hasLastDeploymentKey && this.model.last_deployment && this.$options.hasKey(this.model.last_deployment, 'deployable');
},
createdDate() {
return $.timeago(this.model.created_at);
},
manualActions() {
this.model.manual_actions.map(action => action.name = gl.text.humanize(action.name));
},
},
/**
* Helper to verify if key is present in an object.
* Can be removed once we start using lodash.
*
* @param {Object} obj
* @param {String} key
* @returns {Boolean}
*/
hasKey(obj, key) {
return {}.hasOwnProperty.call(obj, key);
}, },
methods: { methods: {
...@@ -67,11 +138,11 @@ ...@@ -67,11 +138,11 @@
/** /**
* Toggles the visibility of a folders' children. * Toggles the visibility of a folders' children.
*/ */
toggle () { toggle() {
if (this.isFolder) { if (this.isFolder) {
this.open = !this.open; this.open = !this.open;
} }
} },
} },
}) });
})(); })();
...@@ -5,8 +5,9 @@ ...@@ -5,8 +5,9 @@
//= require ./components/environment_item //= require ./components/environment_item
//= require ../boards/vue_resource_interceptor //= require ../boards/vue_resource_interceptor
$(() => {
/* eslint-disable */
$(() => {
const environmentsListApp = document.getElementById('environments-list-view'); const environmentsListApp = document.getElementById('environments-list-view');
const Store = gl.environmentsList.EnvironmentsStore; const Store = gl.environmentsList.EnvironmentsStore;
...@@ -16,10 +17,11 @@ $(() => { ...@@ -16,10 +17,11 @@ $(() => {
gl.EnvironmentsListApp.$destroy(true); gl.EnvironmentsListApp.$destroy(true);
} }
const filterState = (state) => (environment) => environment.state === state && environment; const filterState = state => environment => environment.state === state && environment;
// recursiveMap :: (Function, Array) -> Array // recursiveMap :: (Function, Array) -> Array
const recursiveMap = (fn, arr) => { const recursiveMap = (fn, arr) => {
return arr.map((item) => { return arr.map((item) => {
if (!item.children) { return fn(item); } if (!item.children) { return fn(item); }
...@@ -37,20 +39,20 @@ $(() => { ...@@ -37,20 +39,20 @@ $(() => {
el: '#environments-list-view', el: '#environments-list-view',
components: { components: {
'item': gl.environmentsList.EnvironmentItem item: gl.environmentsList.EnvironmentItem
}, },
data: { data: {
state: Store.state, state: Store.state,
endpoint: environmentsListApp.dataset.endpoint, endpoint: environmentsListApp.dataset.endpoint,
loading: true, loading: true,
visibility: 'available' visibility: 'available',
}, },
computed: { computed: {
filteredEnvironments () { filteredEnvironments (){
return recursiveMap(filterState(this.visibility), this.state.environments); return recursiveMap(filterState(this.visibility), this.state.environments);
} },
}, },
init: Store.create.bind(Store), init: Store.create.bind(Store),
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
gl.environmentsList.EnvironmentsStore = { gl.environmentsList.EnvironmentsStore = {
state: {}, state: {},
create () { create() {
this.state.environments = []; this.state.environments = [];
this.state.stoppedCounter = 0; this.state.stoppedCounter = 0;
this.state.availableCounter = 0; this.state.availableCounter = 0;
...@@ -44,39 +44,23 @@ ...@@ -44,39 +44,23 @@
* @returns {Array} Tree structured array with the received environments. * @returns {Array} Tree structured array with the received environments.
*/ */
storeEnvironments(environments = []) { storeEnvironments(environments = []) {
this.state.stoppedCounter = this.countByState(environments, 'stopped'); this.state.stoppedCounter = this.countByState(environments, 'stopped');
this.state.availableCounter = this.countByState(environments, 'available'); this.state.availableCounter = this.countByState(environments, 'available');
const environmentsTree = environments.reduce((acc, environment) => { const environmentsTree = environments.reduce((acc, environment) => {
if (environment.last_deployment) {
//humanizes actions names if there are any actions
if (environment.last_deployment.manual_actions) {
environment.last_deployment.manual_actions = environment.last_deployment.manual_actions.map((action) => action.name = gl.text.humanize(action.name));
}
//transforms created date for deployment in a human readable format
if (environment.last_deployment.created_at) {
// TODO - how to do this without jquery
}
}
if (environment.environment_type !== null) { if (environment.environment_type !== null) {
const occurs = acc.filter((element, index, array) => { const occurs = acc.filter(element => element.children &&
return element.children && element.name === environment.environment_type; element.name === environment.environment_type);
});
environment["vue-isChildren"] = true; environment['vue-isChildren'] = true;
if (occurs.length) { if (occurs.length) {
acc[acc.indexOf(occurs[0])].children.push(environment); acc[acc.indexOf(occurs[0])].children.push(environment);
acc[acc.indexOf(occurs[0])].children.sort(this.sortByName) acc[acc.indexOf(occurs[0])].children.sort(this.sortByName);
} else { } else {
acc.push({ acc.push({
name: environment.environment_type, name: environment.environment_type,
children: [environment] children: [environment],
}); });
} }
} else { } else {
...@@ -100,7 +84,7 @@ ...@@ -100,7 +84,7 @@
* @returns {Number} * @returns {Number}
*/ */
countByState(environments, state) { countByState(environments, state) {
return environments.filter((env) => env.state === state).length; return environments.filter(env => env.state === state).length;
}, },
/** /**
...@@ -110,7 +94,7 @@ ...@@ -110,7 +94,7 @@
* @param {Object} b * @param {Object} b
* @returns {Number} * @returns {Number}
*/ */
sortByName (a, b) { sortByName(a, b) {
const nameA = a.name.toUpperCase(); const nameA = a.name.toUpperCase();
const nameB = b.name.toUpperCase(); const nameB = b.name.toUpperCase();
...@@ -123,6 +107,6 @@ ...@@ -123,6 +107,6 @@
} }
return 0; return 0;
} },
} };
})(); })();
- if can?(current_user, :create_deployment, @project) - if can?(current_user, :create_deployment, @project)
.inline{ "v-if" => "model.last_deployment && model.last_deployment.manual_actions && model.last_deployment.manual_actions.present"} .inline{ "v-if" => "hasManualActions"}
.dropdown .dropdown
%a.dropdown-new.btn.btn-default{type: "button", "data-toggle" => "dropdown"} %a.dropdown-new.btn.btn-default{type: "button", "data-toggle" => "dropdown"}
= custom_icon('icon_play') = custom_icon('icon_play')
= icon('caret-down') = icon('caret-down')
%ul.dropdown-menu.dropdown-menu-align-right %ul.dropdown-menu.dropdown-menu-align-right
%li{ "v-for" => "action in model.last_deployment.manual_actions" } %li{ "v-for" => "action in manualActions" }
%a{ ":ref" => "'#{namespace_project_path(@project.namespace, @project)}/' + action.id + '/play'", %a{ ":ref" => "action.play_url",
"data-method" => "post", "data-method" => "post",
"rel" => "nofollow" } "rel" => "nofollow" }
......
...@@ -44,8 +44,8 @@ ...@@ -44,8 +44,8 @@
No deployments yet No deployments yet
%td %td
%span{ "v-if" => "!isFolder && model.last_deployment" } %span.environment-created-date-timeago{ "v-if" => "!isFolder && model.last_deployment" }
{{model.last_deployment.created_at}} {{createdDate}}
%td.hidden-xs %td.hidden-xs
.pull-right{ "v-if" => "!isFolder" } .pull-right{ "v-if" => "!isFolder" }
......
- if can?(current_user, :create_deployment, @project) - if can?(current_user, :create_deployment, @project)
%a.btn.btn-build{ "v-if" => "model.last_deployment && model.last_deployment.deployable", %a.btn.btn-build{ "v-if" => "canRetry",
":href" => "'#{namespace_project_builds_path(@project.namespace, @project)}/' + model.last_deployment.deployable.id + '/retry'", ":href" => "model.last_deployment.deployable.retry_url",
"data-method" => "post", "data-method" => "post",
"rel" => "nofollow" } "rel" => "nofollow" }
%span{ "v-if" => "model.last_deployment.last" } %span{ "v-if" => "isLastDeployment" }
Re-deploy Re-deploy
%span{ "v-if" => "!model.last_deployment.last" } %span{ "v-if" => "!isLastDeployment" }
Rollback Rollback
- if can?(current_user, :create_deployment, @project) - if can?(current_user, :create_deployment, @project)
.inline{ "v-if" => "model.stop_action" } .inline{ "v-if" => "isStoppable" }
%a.btn.stop-env-link{":href" => "'#{namespace_project_environments_path(@project.namespace, @project)}/' + model.id", %a.btn.stop-env-link{":href" => "model.environment_url",
"method" => ":post", "method" => ":post",
"rel" => "nofollow", "rel" => "nofollow",
"confirm" => "Are you sure you want to stop this environment?"} "confirm" => "Are you sure you want to stop this environment?"}
......
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