From b4e96590287074813cfe359dc2dd0ecc0ba5253e Mon Sep 17 00:00:00 2001 From: Giulio Troccoli-Allard Date: Thu, 30 Jan 2020 10:01:16 +0000 Subject: [PATCH] Add project selector (#4) * Move the project ID out of the GitLabService * Add ability to select a project * Change the format of the deployment datetime * Sort the projects alphabetically * Add ability to search a project --- src/GitLabEnvironments.vue | 111 ++++++++++++++--------- src/components/Environment.vue | 8 +- src/components/EnvironmentDeployment.vue | 2 +- src/services/GitLabService.js | 24 ++++- src/store.js | 36 +++++--- 5 files changed, 117 insertions(+), 64 deletions(-) diff --git a/src/GitLabEnvironments.vue b/src/GitLabEnvironments.vue index 0be8379..79227ac 100644 --- a/src/GitLabEnvironments.vue +++ b/src/GitLabEnvironments.vue @@ -1,50 +1,58 @@ diff --git a/src/components/Environment.vue b/src/components/Environment.vue index dd08fa2..dd994b5 100644 --- a/src/components/Environment.vue +++ b/src/components/Environment.vue @@ -2,7 +2,7 @@ - +

{{ environment.name }}

@@ -39,7 +39,6 @@ disabled: false, loading : false, environmentRef: null, - environmentId: null, deployableStatus: null, userAvatarUrl: null, deployableFinishedAt: null, @@ -60,12 +59,13 @@ } }, methods : { - loadEnvironment(environmentId) { + loadEnvironment(environment) { this.disabled = true; this.loading = true; this.$store.dispatch("fetchEnvironment", { - environmentId: environmentId + projectId: environment.project.id, + environmentId: environment.id }).then(response => { let lastDeployment = response.data.last_deployment; diff --git a/src/components/EnvironmentDeployment.vue b/src/components/EnvironmentDeployment.vue index a0e6ba8..fd3abbc 100644 --- a/src/components/EnvironmentDeployment.vue +++ b/src/components/EnvironmentDeployment.vue @@ -31,7 +31,7 @@ data() { return { readableDate(date) { - return moment(date).format('Do MMM Y @ HH:mm'); + return moment(date).format('dddd, MMMM Do YYYY, h:mm:ss a'); } } } diff --git a/src/services/GitLabService.js b/src/services/GitLabService.js index 13a16a7..7f89db7 100644 --- a/src/services/GitLabService.js +++ b/src/services/GitLabService.js @@ -10,10 +10,26 @@ const apiClient = axios.create({ } }); -const projectId = 39 - export default { - async getEnvironments() { + async getProjects() { + let projects = [] + let page = 1 + + try { + let response + do { + response = await apiClient.get("/projects?archived=false&page=" + page++) + + projects = projects.concat(response.data) + } while ('x-next-page' in response.headers && response.headers['x-next-page'].length) + } catch (e) { + // eslint-disable-next-line no-console + console.log(e) + } + + return projects + }, + async getEnvironments(projectId) { let environments = [] let page = 1 @@ -31,7 +47,7 @@ export default { return environments }, - getEnvironment(environmentId) { + getEnvironment(projectId, environmentId) { return apiClient.get("/projects/" + projectId + "/environments/" + environmentId); } }; diff --git a/src/store.js b/src/store.js index 53f192d..ef51f7d 100644 --- a/src/store.js +++ b/src/store.js @@ -6,41 +6,51 @@ Vue.use(Vuex); export default new Vuex.Store({ state : { + projects : [], environments : [], - isLoading : true, }, mutations: { + SET_PROJECTS(state, projects) { + state.projects = projects; + }, SET_ENVIRONMENTS(state, environments) { state.environments = environments; }, - SET_LOADING(state, isLoading) { - state.isLoading = isLoading - } }, actions : { - fetchEnvironments({commit}) { - commit("SET_LOADING", true); + fetchProjects({commit}) { + commit("SET_PROJECTS", []); + return GitLabService.getProjects() + .then(projects => { + projects = projects.filter((project) => { + return project['empty_repo'] === false + }); + commit("SET_PROJECTS", projects); + return this; + }) + .catch(error => { + // eslint-disable-next-line no-console + console.log('There was an error: ', error); + }); + }, + fetchEnvironments({commit}, {projectId}) { commit("SET_ENVIRONMENTS", []); - return GitLabService.getEnvironments() + return GitLabService.getEnvironments(projectId) .then(environments => { environments = environments.filter((environment) => { return environment.state === 'available' }); commit("SET_ENVIRONMENTS", environments); - commit("SET_LOADING", false); return this; }) .catch(error => { // eslint-disable-next-line no-console console.log('There was an error: ', error); - commit("SET_LOADING", false) }); }, - fetchEnvironment({commit}, {environmentId}) { - commit("SET_LOADING", true); - return GitLabService.getEnvironment(environmentId) + fetchEnvironment({commit}, {projectId, environmentId}) { + return GitLabService.getEnvironment(projectId, environmentId) .then(response => { - commit("SET_LOADING", false); return response }) .catch(error => {