Commit 17735dc8 authored by Phil Hughes's avatar Phil Hughes

Fixed file templates not being fully fetched in Web IDE

Closes #51912
parent ba66e0cc
import Api from '~/api'; import Api from '~/api';
import { __ } from '~/locale'; import { __ } from '~/locale';
import { normalizeHeaders } from '~/lib/utils/common_utils';
import * as types from './mutation_types'; import * as types from './mutation_types';
import eventHub from '../../../eventhub'; import eventHub from '../../../eventhub';
...@@ -22,13 +23,21 @@ export const receiveTemplateTypesError = ({ commit, dispatch }) => { ...@@ -22,13 +23,21 @@ export const receiveTemplateTypesError = ({ commit, dispatch }) => {
export const receiveTemplateTypesSuccess = ({ commit }, templates) => export const receiveTemplateTypesSuccess = ({ commit }, templates) =>
commit(types.RECEIVE_TEMPLATE_TYPES_SUCCESS, templates); commit(types.RECEIVE_TEMPLATE_TYPES_SUCCESS, templates);
export const fetchTemplateTypes = ({ dispatch, state }) => { export const fetchTemplateTypes = ({ dispatch, state }, page = 1) => {
if (!Object.keys(state.selectedTemplateType).length) return Promise.reject(); if (!Object.keys(state.selectedTemplateType).length) return Promise.reject();
dispatch('requestTemplateTypes'); dispatch('requestTemplateTypes');
return Api.templates(state.selectedTemplateType.key) return Api.templates(state.selectedTemplateType.key, { page })
.then(({ data }) => dispatch('receiveTemplateTypesSuccess', data)) .then(({ data, headers }) => {
const nextPage = parseInt(normalizeHeaders(headers)['X-NEXT-PAGE'], 10);
dispatch('receiveTemplateTypesSuccess', data);
if (nextPage) {
dispatch('fetchTemplateTypes', nextPage);
}
})
.catch(() => dispatch('receiveTemplateTypesError')); .catch(() => dispatch('receiveTemplateTypesError'));
}; };
......
...@@ -9,7 +9,7 @@ export default { ...@@ -9,7 +9,7 @@ export default {
}, },
[types.RECEIVE_TEMPLATE_TYPES_SUCCESS](state, templates) { [types.RECEIVE_TEMPLATE_TYPES_SUCCESS](state, templates) {
state.isLoading = false; state.isLoading = false;
state.templates = templates; state.templates = state.templates.concat(templates);
}, },
[types.SET_SELECTED_TEMPLATE_TYPE](state, type) { [types.SET_SELECTED_TEMPLATE_TYPE](state, type) {
state.selectedTemplateType = type; state.selectedTemplateType = type;
......
---
title: Fixed file templates not fully being fetched in Web IDE
merge_request:
author:
type: fixed
...@@ -69,11 +69,17 @@ describe('IDE file templates actions', () => { ...@@ -69,11 +69,17 @@ describe('IDE file templates actions', () => {
describe('fetchTemplateTypes', () => { describe('fetchTemplateTypes', () => {
describe('success', () => { describe('success', () => {
let nextPage;
beforeEach(() => { beforeEach(() => {
mock.onGet(/api\/(.*)\/templates\/licenses/).replyOnce(200, [ mock.onGet(/api\/(.*)\/templates\/licenses/).replyOnce(() => [
{ 200,
name: 'MIT', [
}, {
name: 'MIT',
},
],
{ 'X-NEXT-PAGE': nextPage },
]); ]);
}); });
...@@ -116,6 +122,38 @@ describe('IDE file templates actions', () => { ...@@ -116,6 +122,38 @@ describe('IDE file templates actions', () => {
done, done,
); );
}); });
it('dispatches actions for next page', done => {
nextPage = '2';
state.selectedTemplateType = {
key: 'licenses',
};
testAction(
actions.fetchTemplateTypes,
null,
state,
[],
[
{
type: 'requestTemplateTypes',
},
{
type: 'receiveTemplateTypesSuccess',
payload: [
{
name: 'MIT',
},
],
},
{
type: 'fetchTemplateTypes',
payload: 2,
},
],
done,
);
});
}); });
describe('error', () => { describe('error', () => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment