Commit 6409921e authored by Jose Vargas's avatar Jose Vargas

Add GlAlert component

This replaces the createFlash call for the GlAlert
component, updates some vuex related code and tests
parent c78144c0
<script> <script>
import { import {
GlAlert,
GlButton, GlButton,
GlModal, GlModal,
GlModalDirective, GlModalDirective,
...@@ -13,6 +14,7 @@ import { __, s__ } from '~/locale'; ...@@ -13,6 +14,7 @@ import { __, s__ } from '~/locale';
export default { export default {
components: { components: {
GlAlert,
GlButton, GlButton,
GlButtonGroup, GlButtonGroup,
GlDropdown, GlDropdown,
...@@ -28,6 +30,7 @@ export default { ...@@ -28,6 +30,7 @@ export default {
'availablePlatforms', 'availablePlatforms',
'instructions', 'instructions',
'selectedArchitecture', 'selectedArchitecture',
'showAlert',
]), ]),
...mapGetters('installRunnerPopup', [ ...mapGetters('installRunnerPopup', [
'getSupportedArchitectures', 'getSupportedArchitectures',
...@@ -53,6 +56,7 @@ export default { ...@@ -53,6 +56,7 @@ export default {
'requestPlatforms', 'requestPlatforms',
'selectPlatform', 'selectPlatform',
'startInstructionsRequest', 'startInstructionsRequest',
'toggleAlert',
]), ]),
}, },
modalId: 'installation-instructions-modal', modalId: 'installation-instructions-modal',
...@@ -63,6 +67,7 @@ export default { ...@@ -63,6 +67,7 @@ export default {
downloadLatestBinary: s__('Runners|Download Latest Binary'), downloadLatestBinary: s__('Runners|Download Latest Binary'),
registerRunner: s__('Runners|Register Runner'), registerRunner: s__('Runners|Register Runner'),
method: __('Method'), method: __('Method'),
genericError: __('An error has occurred'),
}, },
}; };
</script> </script>
...@@ -76,6 +81,9 @@ export default { ...@@ -76,6 +81,9 @@ export default {
:title="$options.i18n.installARunner" :title="$options.i18n.installARunner"
:action-secondary="closeButton" :action-secondary="closeButton"
> >
<gl-alert v-if="showAlert" variant="danger" @dismiss="toggleAlert(false)">
{{ $options.i18n.genericError }}
</gl-alert>
<h5>{{ __('Environment') }}</h5> <h5>{{ __('Environment') }}</h5>
<gl-button-group class="gl-mb-5"> <gl-button-group class="gl-mb-5">
<gl-button <gl-button
...@@ -113,7 +121,7 @@ export default { ...@@ -113,7 +121,7 @@ export default {
</div> </div>
</template> </template>
<template v-if="!instructionsEmpty"> <template v-if="!instructionsEmpty">
<div v-if="!instructionsEmpty" class="gl-display-flex"> <div class="gl-display-flex">
<pre class="bg-light gl-flex-fill-1" data-testid="binary-instructions"> <pre class="bg-light gl-flex-fill-1" data-testid="binary-instructions">
{{ instructions.install.trimStart() }} {{ instructions.install.trimStart() }}
</pre> </pre>
......
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import statusCodes from '~/lib/utils/http_status';
import createFlash from '~/flash';
import { __ } from '~/locale';
import * as types from './mutation_types'; import * as types from './mutation_types';
export const requestPlatforms = ({ state, commit, getters, dispatch }) => { export const requestPlatforms = ({ state, commit, getters, dispatch }) => {
axios axios
.get(state.platformsPath) .get(state.platformsPath)
.then(resp => { .then(resp => {
if (resp.status === statusCodes.OK) { commit(types.SET_AVAILABLE_PLATFORMS, resp?.data);
commit(types.SET_AVAILABLE_PLATFORMS, resp?.data); // Select the first platform and architecture
// Select the first platform and architecture const platform = Object.keys(resp.data)[0];
const platform = Object.keys(resp.data)[0];
commit(types.SET_AVAILABLE_PLATFORM, platform);
commit(types.SET_AVAILABLE_PLATFORM, platform); dispatch('selectArchitecture', getters.getSupportedArchitectures[0]);
dispatch('selectArchitecture', getters.getSupportedArchitectures[0]); dispatch('requestPlatformsInstructions');
dispatch('requestPlatformsInstructions');
}
}) })
.catch(() => createFlash({ message: __('An error has occurred') })); .catch(() => dispatch('toggleAlert', true));
}; };
export const requestPlatformsInstructions = ({ commit, state }) => { export const requestPlatformsInstructions = ({ commit, state, dispatch }) => {
let path = `${state.instructionsPath}?os=${state.selectedAvailablePlatform}`; let path = `${state.instructionsPath}?os=${state.selectedAvailablePlatform}`;
path = path =
state.selectedArchitecture !== '' state.selectedArchitecture !== ''
...@@ -30,14 +25,8 @@ export const requestPlatformsInstructions = ({ commit, state }) => { ...@@ -30,14 +25,8 @@ export const requestPlatformsInstructions = ({ commit, state }) => {
axios axios
.get(path) .get(path)
.then(resp => { .then(resp => commit(types.SET_INSTRUCTIONS, resp?.data))
if (resp.status === statusCodes.OK) { .catch(() => dispatch('toggleAlert', true));
commit(types.SET_INSTRUCTIONS, resp?.data);
}
})
.catch(() =>
createFlash({ message: __('An error has occurred fetching platform instructions') }),
);
}; };
export const startInstructionsRequest = ({ dispatch }, architecture) => { export const startInstructionsRequest = ({ dispatch }, architecture) => {
...@@ -58,3 +47,7 @@ export const selectPlatform = ({ commit, dispatch, getters }, platform) => { ...@@ -58,3 +47,7 @@ export const selectPlatform = ({ commit, dispatch, getters }, platform) => {
export const selectArchitecture = ({ commit }, architecture) => { export const selectArchitecture = ({ commit }, architecture) => {
commit(types.SET_ARCHITECTURE, architecture); commit(types.SET_ARCHITECTURE, architecture);
}; };
export const toggleAlert = ({ commit }, state) => {
commit(types.SET_SHOW_ALERT, state);
};
...@@ -3,16 +3,13 @@ export const hasDownloadLocationsAvailable = state => { ...@@ -3,16 +3,13 @@ export const hasDownloadLocationsAvailable = state => {
}; };
export const getSupportedArchitectures = state => { export const getSupportedArchitectures = state => {
if (hasDownloadLocationsAvailable(state)) { return Object.keys(
return Object.keys( state.availablePlatforms[state.selectedAvailablePlatform]?.download_locations || {},
state.availablePlatforms[state.selectedAvailablePlatform]?.download_locations, );
);
}
return [];
}; };
export const instructionsEmpty = state => { export const instructionsEmpty = state => {
return !Object.keys(state.instructions).length > 0; return !Object.keys(state.instructions).length;
}; };
export const getDownloadLocation = state => { export const getDownloadLocation = state => {
......
...@@ -2,3 +2,4 @@ export const SET_AVAILABLE_PLATFORMS = 'SET_AVAILABLE_PLATFORMS'; ...@@ -2,3 +2,4 @@ export const SET_AVAILABLE_PLATFORMS = 'SET_AVAILABLE_PLATFORMS';
export const SET_AVAILABLE_PLATFORM = 'SET_AVAILABLE_PLATFORM'; export const SET_AVAILABLE_PLATFORM = 'SET_AVAILABLE_PLATFORM';
export const SET_ARCHITECTURE = 'SET_ARCHITECTURE'; export const SET_ARCHITECTURE = 'SET_ARCHITECTURE';
export const SET_INSTRUCTIONS = 'SET_INSTRUCTIONS'; export const SET_INSTRUCTIONS = 'SET_INSTRUCTIONS';
export const SET_SHOW_ALERT = 'SET_SHOW_ALERT';
...@@ -13,4 +13,7 @@ export default { ...@@ -13,4 +13,7 @@ export default {
[types.SET_INSTRUCTIONS](state, instructions) { [types.SET_INSTRUCTIONS](state, instructions) {
state.instructions = instructions; state.instructions = instructions;
}, },
[types.SET_SHOW_ALERT](state, show) {
state.showAlert = show;
},
}; };
...@@ -5,4 +5,5 @@ export default (initialState = {}) => ({ ...@@ -5,4 +5,5 @@ export default (initialState = {}) => ({
selectedAvailablePlatform: initialState.selectedAvailablePlatform || '', // index from the availablePlatforms array selectedAvailablePlatform: initialState.selectedAvailablePlatform || '', // index from the availablePlatforms array
selectedArchitecture: initialState.selectedArchitecture || '', selectedArchitecture: initialState.selectedArchitecture || '',
instructions: initialState.instructions || {}, instructions: initialState.instructions || {},
showAlert: false,
}); });
...@@ -2810,9 +2810,6 @@ msgstr "" ...@@ -2810,9 +2810,6 @@ msgstr ""
msgid "An error has occurred" msgid "An error has occurred"
msgstr "" msgstr ""
msgid "An error has occurred fetching platform instructions"
msgstr ""
msgid "An error occured while making the changes: %{error}" msgid "An error occured while making the changes: %{error}"
msgstr "" msgstr ""
......
...@@ -76,7 +76,14 @@ describe('Runner Instructions actions', () => { ...@@ -76,7 +76,14 @@ describe('Runner Instructions actions', () => {
}); });
it('shows an error', done => { it('shows an error', done => {
testAction(actions.requestPlatformsInstructions, null, state, [], [], done); testAction(
actions.requestPlatformsInstructions,
null,
state,
[],
[{ type: 'toggleAlert', payload: true }],
done,
);
}); });
}); });
}); });
...@@ -85,6 +92,8 @@ describe('Runner Instructions actions', () => { ...@@ -85,6 +92,8 @@ describe('Runner Instructions actions', () => {
describe('successful request', () => { describe('successful request', () => {
beforeEach(() => { beforeEach(() => {
state.platformsPath = '/platforms'; state.platformsPath = '/platforms';
state.availablePlatforms = mockPlatformsObject;
state.getSupportedArchitectures = ['linux', 'windows'];
axiosMock.onGet(state.platformsPath).reply(statusCodes.OK, mockPlatformsObject); axiosMock.onGet(state.platformsPath).reply(statusCodes.OK, mockPlatformsObject);
}); });
...@@ -97,7 +106,10 @@ describe('Runner Instructions actions', () => { ...@@ -97,7 +106,10 @@ describe('Runner Instructions actions', () => {
{ type: types.SET_AVAILABLE_PLATFORMS, payload: mockPlatformsObject }, { type: types.SET_AVAILABLE_PLATFORMS, payload: mockPlatformsObject },
{ type: types.SET_AVAILABLE_PLATFORM, payload: 'linux' }, { type: types.SET_AVAILABLE_PLATFORM, payload: 'linux' },
], ],
[], [
{ type: 'selectArchitecture', payload: 'linux' },
{ type: 'requestPlatformsInstructions' },
],
done, done,
); );
}); });
...@@ -110,7 +122,14 @@ describe('Runner Instructions actions', () => { ...@@ -110,7 +122,14 @@ describe('Runner Instructions actions', () => {
}); });
it('shows an error', done => { it('shows an error', done => {
testAction(actions.requestPlatforms, null, state, [], [], done); testAction(
actions.requestPlatforms,
null,
state,
[],
[{ type: 'toggleAlert', payload: true }],
done,
);
}); });
}); });
}); });
...@@ -130,4 +149,17 @@ describe('Runner Instructions actions', () => { ...@@ -130,4 +149,17 @@ describe('Runner Instructions actions', () => {
); );
}); });
}); });
describe('toggleAlert', () => {
it('commits the SET_SHOW_ALERT mutation', done => {
testAction(
actions.toggleAlert,
true,
state,
[{ type: types.SET_SHOW_ALERT, payload: true }],
[],
done,
);
});
});
}); });
...@@ -40,4 +40,12 @@ describe('Runner Instructions mutations', () => { ...@@ -40,4 +40,12 @@ describe('Runner Instructions mutations', () => {
expect(localState.instructions).toEqual(mockInstructions); expect(localState.instructions).toEqual(mockInstructions);
}); });
}); });
describe('SET_SHOW_ALERT', () => {
it('should set the showAlert boolean', () => {
mutations.SET_SHOW_ALERT(localState, true);
expect(localState.showAlert).toEqual(true);
});
});
}); });
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