summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/lib/utils/select2_utils.js
blob: 03c0e608b79d67343cd7d9a467baeb8ef09ddb95 (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
import axios from './axios_utils';
import { normalizeHeaders, parseIntPagination } from './common_utils';

// This is used in the select2 config to replace jQuery.ajax with axios
export const select2AxiosTransport = (params) => {
  axios({
    method: params.type?.toLowerCase() || 'get',
    url: params.url,
    params: params.data,
  })
    .then((res) => {
      const results = res.data || [];
      const headers = normalizeHeaders(res.headers);
      const pagination = parseIntPagination(headers);
      const more = pagination.nextPage > pagination.page;

      params.success({
        results,
        pagination: {
          more,
        },
      });
    })
    .catch(params.error);
};