summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/releases/stores/modules/index/actions.js
blob: d3bb11cab30b31ff87cbd85f65f69374b8b0127c (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
import createFlash from '~/flash';
import { __ } from '~/locale';
import { PAGE_SIZE } from '~/releases/constants';
import allReleasesQuery from '~/releases/graphql/queries/all_releases.query.graphql';
import { gqClient, convertAllReleasesGraphQLResponse } from '~/releases/util';
import * as types from './mutation_types';

/**
 * Gets a paginated list of releases from the GraphQL endpoint
 *
 * @param {Object} vuexParams
 * @param {Object} actionParams
 * @param {String} [actionParams.before] A GraphQL cursor. If provided,
 * the items returned will proceed the provided cursor.
 * @param {String} [actionParams.after] A GraphQL cursor. If provided,
 * the items returned will follow the provided cursor.
 */
export const fetchReleases = ({ dispatch, commit, state }, { before, after }) => {
  commit(types.REQUEST_RELEASES);

  const { sort, orderBy } = state.sorting;
  const orderByParam = orderBy === 'created_at' ? 'created' : orderBy;
  const sortParams = `${orderByParam}_${sort}`.toUpperCase();

  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 fetchReleases. These parameters cannot be used together.',
    );
  }

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

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

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

export const setSorting = ({ commit }, data) => commit(types.SET_SORTING, data);