Commit 141a7bed authored by Ezekiel Kigbo's avatar Ezekiel Kigbo

Merge branch 'vs/refactor-merge-options' into 'master'

Refactor init logic of merge request options on project settings page

See merge request gitlab-org/gitlab!67334
parents 076b96b6 50466932
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import '~/pages/projects/edit'; import '~/pages/projects/edit';
import mountApprovals from 'ee/approvals/mount_project_settings'; import mountApprovals from 'ee/approvals/mount_project_settings';
import initMergeOptionSettings from 'ee/pages/projects/edit/merge_options'; import { initMergeOptionSettings } from 'ee/pages/projects/edit/merge_options';
import initProjectAdjournedDeleteButton from 'ee/projects/project_adjourned_delete_button'; import initProjectAdjournedDeleteButton from 'ee/projects/project_adjourned_delete_button';
import mountStatusChecks from 'ee/status_checks/mount'; import mountStatusChecks from 'ee/status_checks/mount';
import groupsSelect from '~/groups_select'; import groupsSelect from '~/groups_select';
......
import gql from 'graphql-tag'; import gql from 'graphql-tag';
import createFlash from '~/flash';
import createDefaultClient from '~/lib/graphql'; import createDefaultClient from '~/lib/graphql';
import { s__ } from '~/locale';
const containerEl = document.querySelector('#project-merge-options'); export const ERROR_LOADING_MERGE_OPTION_SETTINGS = s__(
'Settings|Unable to load the merge request options settings. Try reloading the page.',
);
const mergePipelinesCheckbox = document.querySelector('.js-merge-options-merge-pipelines'); const mergePipelinesCheckbox = document.querySelector('.js-merge-options-merge-pipelines');
const mergeTrainsCheckbox = document.querySelector('.js-merge-options-merge-trains'); const mergeTrainsCheckbox = document.querySelector('.js-merge-options-merge-trains');
...@@ -18,39 +22,49 @@ const getCiCdSettingsQuery = (projectFullPath) => ...@@ -18,39 +22,49 @@ const getCiCdSettingsQuery = (projectFullPath) =>
} }
`; `;
const disableMergeTrains = () => { const initMergeOptions = (mergePipelinesEnabled, mergeTrainsEnabled) => {
mergeTrainsCheckbox.disabled = true; if (!mergePipelinesCheckbox) {
return;
}
mergePipelinesCheckbox.disabled = false;
mergePipelinesCheckbox.checked = mergePipelinesEnabled;
if (mergeTrainsCheckbox) {
mergeTrainsCheckbox.disabled = !mergePipelinesEnabled;
mergeTrainsCheckbox.checked = mergeTrainsEnabled;
mergePipelinesCheckbox.addEventListener('change', () => {
if (!mergePipelinesCheckbox.checked) {
mergeTrainsCheckbox.checked = false; mergeTrainsCheckbox.checked = false;
}
mergeTrainsCheckbox.disabled = !mergePipelinesCheckbox.checked;
});
}
}; };
export default function initMergeOptions() { const fetchMergeOptions = () => {
const containerEl = document.querySelector('#project-merge-options');
const { projectFullPath } = containerEl.dataset; const { projectFullPath } = containerEl.dataset;
const defaultClient = createDefaultClient(); const defaultClient = createDefaultClient();
defaultClient return defaultClient
.query({ .query({
query: getCiCdSettingsQuery(projectFullPath), query: getCiCdSettingsQuery(projectFullPath),
}) })
.then((result) => { .then((result) => {
const { mergePipelinesEnabled, mergeTrainsEnabled } = result?.data?.project?.ciCdSettings; const { mergePipelinesEnabled, mergeTrainsEnabled } = result?.data?.project?.ciCdSettings;
mergePipelinesCheckbox.checked = mergePipelinesEnabled;
mergeTrainsCheckbox.checked = mergeTrainsEnabled;
if (!mergePipelinesEnabled) { return { mergePipelinesEnabled, mergeTrainsEnabled };
disableMergeTrains();
}
})
.catch(() => {
if (!mergePipelinesCheckbox.checked) {
disableMergeTrains();
}
}); });
};
mergePipelinesCheckbox.addEventListener('change', () => { export const initMergeOptionSettings = () => {
mergeTrainsCheckbox.disabled = !mergePipelinesCheckbox.checked; return fetchMergeOptions()
.then(({ mergePipelinesEnabled, mergeTrainsEnabled }) => {
if (!mergePipelinesCheckbox.checked) { initMergeOptions(mergePipelinesEnabled, mergeTrainsEnabled);
mergeTrainsCheckbox.checked = false; })
} .catch((error) => {
createFlash({ message: ERROR_LOADING_MERGE_OPTION_SETTINGS, error, captureError: true });
}); });
} };
- return unless merge_pipelines_available? - return unless merge_pipelines_available?
.form-check.builds-feature .form-check.builds-feature
= form.check_box :merge_pipelines_enabled, class: 'form-check-input js-merge-options-merge-pipelines', data: { qa_selector: 'merged_results_pipeline_checkbox' } = form.check_box :merge_pipelines_enabled, class: 'form-check-input js-merge-options-merge-pipelines', data: { qa_selector: 'merged_results_pipeline_checkbox' }, disabled: 'disabled'
= form.label :merge_pipelines_enabled, class: 'form-check-label' do = form.label :merge_pipelines_enabled, class: 'form-check-label' do
= s_('ProjectSettings|Enable merged results pipelines') = s_('ProjectSettings|Enable merged results pipelines')
.text-secondary.mb-2 .text-secondary.mb-2
......
import {
initMergeOptionSettings,
ERROR_LOADING_MERGE_OPTION_SETTINGS,
} from 'ee/pages/projects/edit/merge_options';
import { setHTMLFixture } from 'helpers/fixtures';
import createFlash from '~/flash';
import * as createDefaultClient from '~/lib/graphql';
jest.mock('~/flash');
describe('MergOptions', () => {
beforeEach(() => {
setHTMLFixture(`
<div id="project-merge-options"></div/>
`);
createDefaultClient.default = jest.fn(() => ({
query: jest.fn().mockRejectedValue('Error'),
}));
});
it('shows flash message on network error', async () => {
await initMergeOptionSettings();
expect(createFlash).toHaveBeenCalledWith({
message: ERROR_LOADING_MERGE_OPTION_SETTINGS,
error: 'Error',
captureError: true,
});
});
});
...@@ -30372,6 +30372,9 @@ msgstr "" ...@@ -30372,6 +30372,9 @@ msgstr ""
msgid "Settings" msgid "Settings"
msgstr "" msgstr ""
msgid "Settings|Unable to load the merge request options settings. Try reloading the page."
msgstr ""
msgid "Setup" msgid "Setup"
msgstr "" msgstr ""
......
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