diff options
author | Dennis Tang <dtang@gitlab.com> | 2018-05-09 16:57:01 +0200 |
---|---|---|
committer | Dennis Tang <dtang@gitlab.com> | 2018-05-09 16:57:01 +0200 |
commit | e33c44587a6d4bb82424a148b543c17079ad615b (patch) | |
tree | 2f57af14330a8e727d8cd80e97738fac76bc3c06 /app/assets | |
parent | b66aa62431d28efd21eab272e56a0a44d76bae2d (diff) | |
download | gitlab-ce-e33c44587a6d4bb82424a148b543c17079ad615b.tar.gz |
refactor gke store actions
Diffstat (limited to 'app/assets')
-rw-r--r-- | app/assets/javascripts/projects/gke_cluster_dropdowns/stores/actions.js | 106 |
1 files changed, 41 insertions, 65 deletions
diff --git a/app/assets/javascripts/projects/gke_cluster_dropdowns/stores/actions.js b/app/assets/javascripts/projects/gke_cluster_dropdowns/stores/actions.js index dba12ac28ca..60bfae456c3 100644 --- a/app/assets/javascripts/projects/gke_cluster_dropdowns/stores/actions.js +++ b/app/assets/javascripts/projects/gke_cluster_dropdowns/stores/actions.js @@ -16,92 +16,68 @@ export const setMachineType = ({ commit }, selectedMachineType) => { commit(types.SET_MACHINE_TYPE, selectedMachineType); }; -export const getProjects = ({ commit }) => +const displayError = (resp, errorMessage) => { + if (resp.result && resp.result.error) { + Flash(sprintf(s__(errorMessage), { error: resp.result.error.message })); + } +}; + +const gapiRequest = ({ service, params, commit, mutation, payloadKey, errorMessage }) => new Promise((resolve, reject) => { - const request = gapi.client.cloudresourcemanager.projects.list(); + const request = service.list(params); return request.then( resp => { - commit(types.SET_PROJECTS, resp.result.projects); + const { result } = resp; + + commit(mutation, result[payloadKey]); resolve(); }, resp => { - if (resp.result.error) { - Flash( - sprintf( - s__( - 'ClusterIntegration|An error occured while trying to fetch your projects: %{error}', - ), - { - error: resp.result.error.message, - }, - ), - ); - } + displayError(resp, errorMessage); reject(); }, ); }); +export const getProjects = ({ commit }) => + gapiRequest({ + service: gapi.client.cloudresourcemanager.projects, + params: {}, + commit, + mutation: types.SET_PROJECTS, + payloadKey: 'projects', + errorMessage: + 'ClusterIntegration|An error occured while trying to fetch your projects: %{error}', + }); + export const getZones = ({ commit, state }) => - new Promise((resolve, reject) => { - const request = gapi.client.compute.zones.list({ + gapiRequest({ + service: gapi.client.compute.zones, + params: { project: state.selectedProject.projectId, - }); - - return request.then( - resp => { - commit(types.SET_ZONES, resp.result.items); - - resolve(); - }, - resp => { - if (resp.result.error) { - Flash( - sprintf( - s__( - 'ClusterIntegration|An error occured while trying to fetch project zones: %{error}', - ), - { error: resp.result.error.message }, - ), - ); - } - - reject(); - }, - ); + }, + commit, + mutation: types.SET_ZONES, + payloadKey: 'items', + errorMessage: + 'ClusterIntegration|An error occured while trying to fetch project zones: %{error}', }); export const getMachineTypes = ({ commit, state }) => - new Promise((resolve, reject) => { - const request = gapi.client.compute.machineTypes.list({ + gapiRequest({ + service: gapi.client.compute.machineTypes, + params: { project: state.selectedProject.projectId, zone: state.selectedZone, - }); - - return request.then( - resp => { - commit(types.SET_MACHINE_TYPES, resp.result.items); - - resolve(); - }, - resp => { - if (resp.result.error) { - Flash( - sprintf( - s__( - 'ClusterIntegration|An error occured while trying to fetch zone machine types: %{error}', - ), - { error: resp.result.error.message }, - ), - ); - } - - reject(); - }, - ); + }, + commit, + mutation: types.SET_MACHINE_TYPES, + payloadKey: 'items', + errorMessage: + 'ClusterIntegration|An error occured while trying to fetch zone machine types: %{error}', }); // prevent babel-plugin-rewire from generating an invalid default during karma tests |