Welcome to mirror list, hosted at ThFree Co, Russian Federation.

actions.js « store « metric_images « components « vue_shared « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 832fb89183861ca178d77b872ad8010a00c3e1dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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,
});