Commit e33c4458 authored by Dennis Tang's avatar Dennis Tang

refactor gke store actions

parent b66aa624
...@@ -16,92 +16,68 @@ export const setMachineType = ({ commit }, selectedMachineType) => { ...@@ -16,92 +16,68 @@ export const setMachineType = ({ commit }, selectedMachineType) => {
commit(types.SET_MACHINE_TYPE, 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) => { new Promise((resolve, reject) => {
const request = gapi.client.cloudresourcemanager.projects.list(); const request = service.list(params);
return request.then( return request.then(
resp => { resp => {
commit(types.SET_PROJECTS, resp.result.projects); const { result } = resp;
commit(mutation, result[payloadKey]);
resolve(); resolve();
}, },
resp => { resp => {
if (resp.result.error) { displayError(resp, errorMessage);
Flash(
sprintf(
s__(
'ClusterIntegration|An error occured while trying to fetch your projects: %{error}',
),
{
error: resp.result.error.message,
},
),
);
}
reject(); reject();
}, },
); );
}); });
export const getZones = ({ commit, state }) => export const getProjects = ({ commit }) =>
new Promise((resolve, reject) => { gapiRequest({
const request = gapi.client.compute.zones.list({ service: gapi.client.cloudresourcemanager.projects,
project: state.selectedProject.projectId, params: {},
commit,
mutation: types.SET_PROJECTS,
payloadKey: 'projects',
errorMessage:
'ClusterIntegration|An error occured while trying to fetch your projects: %{error}',
}); });
return request.then( export const getZones = ({ commit, state }) =>
resp => { gapiRequest({
commit(types.SET_ZONES, resp.result.items); service: gapi.client.compute.zones,
params: {
resolve(); project: state.selectedProject.projectId,
}, },
resp => { commit,
if (resp.result.error) { mutation: types.SET_ZONES,
Flash( payloadKey: 'items',
sprintf( errorMessage:
s__(
'ClusterIntegration|An error occured while trying to fetch project zones: %{error}', 'ClusterIntegration|An error occured while trying to fetch project zones: %{error}',
),
{ error: resp.result.error.message },
),
);
}
reject();
},
);
}); });
export const getMachineTypes = ({ commit, state }) => export const getMachineTypes = ({ commit, state }) =>
new Promise((resolve, reject) => { gapiRequest({
const request = gapi.client.compute.machineTypes.list({ service: gapi.client.compute.machineTypes,
params: {
project: state.selectedProject.projectId, project: state.selectedProject.projectId,
zone: state.selectedZone, zone: state.selectedZone,
});
return request.then(
resp => {
commit(types.SET_MACHINE_TYPES, resp.result.items);
resolve();
}, },
resp => { commit,
if (resp.result.error) { mutation: types.SET_MACHINE_TYPES,
Flash( payloadKey: 'items',
sprintf( errorMessage:
s__(
'ClusterIntegration|An error occured while trying to fetch zone machine types: %{error}', 'ClusterIntegration|An error occured while trying to fetch zone machine types: %{error}',
),
{ error: resp.result.error.message },
),
);
}
reject();
},
);
}); });
// prevent babel-plugin-rewire from generating an invalid default during karma tests // prevent babel-plugin-rewire from generating an invalid default during karma tests
......
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