summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/releases/stores/modules/list/actions.js
blob: 02e67415e630cc14d64a4453836844a17f82ccc2 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import * as types from './mutation_types';
import { deprecatedCreateFlash as createFlash } from '~/flash';
import { __ } from '~/locale';
import api from '~/api';
import {
  normalizeHeaders,
  parseIntPagination,
  convertObjectPropsToCamelCase,
} from '~/lib/utils/common_utils';
import allReleasesQuery from '~/releases/queries/all_releases.query.graphql';
import { gqClient, convertAllReleasesGraphQLResponse } from '../../../util';
import { PAGE_SIZE } from '../../../constants';

/**
 * Gets a paginated list of releases from the server
 *
 * @param {Object} vuexParams
 * @param {Object} actionParams
 * @param {Number} [actionParams.page] The page number of results to fetch
 * (this parameter is only used when fetching results from the REST API)
 * @param {String} [actionParams.before] A GraphQL cursor. If provided,
 * the items returned will proceed the provided cursor (this parameter is only
 * used when fetching results from the GraphQL API).
 * @param {String} [actionParams.after] A GraphQL cursor. If provided,
 * the items returned will follow the provided cursor (this parameter is only
 * used when fetching results from the GraphQL API).
 */
export const fetchReleases = ({ dispatch, rootGetters }, { page = 1, before, after }) => {
  if (rootGetters.useGraphQLEndpoint) {
    dispatch('fetchReleasesGraphQl', { before, after });
  } else {
    dispatch('fetchReleasesRest', { page });
  }
};

/**
 * Gets a paginated list of releases from the GraphQL endpoint
 */
export const fetchReleasesGraphQl = (
  { dispatch, commit, state },
  { before = null, after = null },
) => {
  commit(types.REQUEST_RELEASES);

  let paginationParams;
  if (!before && !after) {
    paginationParams = { first: PAGE_SIZE };
  } else if (before && !after) {
    paginationParams = { last: PAGE_SIZE, before };
  } else if (!before && after) {
    paginationParams = { first: PAGE_SIZE, after };
  } else {
    throw new Error(
      'Both a `before` and an `after` parameter were provided to fetchReleasesGraphQl. These parameters cannot be used together.',
    );
  }

  gqClient
    .query({
      query: allReleasesQuery,
      variables: {
        fullPath: state.projectPath,
        ...paginationParams,
      },
    })
    .then(response => {
      const { data, paginationInfo: graphQlPageInfo } = convertAllReleasesGraphQLResponse(response);

      commit(types.RECEIVE_RELEASES_SUCCESS, {
        data,
        graphQlPageInfo,
      });
    })
    .catch(() => dispatch('receiveReleasesError'));
};

/**
 * Gets a paginated list of releases from the REST endpoint
 */
export const fetchReleasesRest = ({ dispatch, commit, state }, { page }) => {
  commit(types.REQUEST_RELEASES);

  api
    .releases(state.projectId, { page })
    .then(({ data, headers }) => {
      const restPageInfo = parseIntPagination(normalizeHeaders(headers));
      const camelCasedReleases = convertObjectPropsToCamelCase(data, { deep: true });

      commit(types.RECEIVE_RELEASES_SUCCESS, {
        data: camelCasedReleases,
        restPageInfo,
      });
    })
    .catch(() => dispatch('receiveReleasesError'));
};

export const receiveReleasesError = ({ commit }) => {
  commit(types.RECEIVE_RELEASES_ERROR);
  createFlash(__('An error occurred while fetching the releases. Please try again.'));
};