summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/metric_images/store/actions.js
diff options
context:
space:
mode:
Diffstat (limited to 'app/assets/javascripts/vue_shared/components/metric_images/store/actions.js')
-rw-r--r--app/assets/javascripts/vue_shared/components/metric_images/store/actions.js85
1 files changed, 85 insertions, 0 deletions
diff --git a/app/assets/javascripts/vue_shared/components/metric_images/store/actions.js b/app/assets/javascripts/vue_shared/components/metric_images/store/actions.js
new file mode 100644
index 00000000000..832fb891838
--- /dev/null
+++ b/app/assets/javascripts/vue_shared/components/metric_images/store/actions.js
@@ -0,0 +1,85 @@
+import createFlash from '~/flash';
+import { s__ } from '~/locale';
+import * as types from './mutation_types';
+
+export const fetchImagesFactory = (service) => async ({ state, commit }) => {
+ commit(types.REQUEST_METRIC_IMAGES);
+ const { modelIid, projectId } = state;
+
+ try {
+ const response = await service.getMetricImages({ id: projectId, modelIid });
+ commit(types.RECEIVE_METRIC_IMAGES_SUCCESS, response);
+ } catch (error) {
+ commit(types.RECEIVE_METRIC_IMAGES_ERROR);
+ createFlash({ message: s__('MetricImages|There was an issue loading metric images.') });
+ }
+};
+
+export const uploadImageFactory = (service) => async (
+ { state, commit },
+ { files, url, urlText },
+) => {
+ commit(types.REQUEST_METRIC_UPLOAD);
+
+ const { modelIid, projectId } = state;
+
+ try {
+ const response = await service.uploadMetricImage({
+ file: files.item(0),
+ id: projectId,
+ modelIid,
+ url,
+ urlText,
+ });
+ commit(types.RECEIVE_METRIC_UPLOAD_SUCCESS, response);
+ } catch (error) {
+ commit(types.RECEIVE_METRIC_UPLOAD_ERROR);
+ createFlash({ message: s__('MetricImages|There was an issue uploading your image.') });
+ }
+};
+
+export const updateImageFactory = (service) => async (
+ { state, commit },
+ { imageId, url, urlText },
+) => {
+ commit(types.REQUEST_METRIC_UPLOAD);
+
+ const { modelIid, projectId } = state;
+
+ try {
+ const response = await service.updateMetricImage({
+ modelIid,
+ id: projectId,
+ imageId,
+ url,
+ urlText,
+ });
+ commit(types.RECEIVE_METRIC_UPDATE_SUCCESS, response);
+ } catch (error) {
+ commit(types.RECEIVE_METRIC_UPLOAD_ERROR);
+ createFlash({ message: s__('MetricImages|There was an issue updating your image.') });
+ }
+};
+
+export const deleteImageFactory = (service) => async ({ state, commit }, imageId) => {
+ const { modelIid, projectId } = state;
+
+ try {
+ await service.deleteMetricImage({ imageId, id: projectId, modelIid });
+ commit(types.RECEIVE_METRIC_DELETE_SUCCESS, imageId);
+ } catch (error) {
+ createFlash({ message: s__('MetricImages|There was an issue deleting the image.') });
+ }
+};
+
+export const setInitialData = ({ commit }, data) => {
+ commit(types.SET_INITIAL_DATA, data);
+};
+
+export default (service) => ({
+ fetchImages: fetchImagesFactory(service),
+ uploadImage: uploadImageFactory(service),
+ updateImage: updateImageFactory(service),
+ deleteImage: deleteImageFactory(service),
+ setInitialData,
+});