Commit 46b3429a authored by Jannik Lehmann's avatar Jannik Lehmann Committed by Ezekiel Kigbo

Replaced browser confirm modal with GlModal for lock button

This commit solves: https://gitlab.com/gitlab-org/gitlab/-/issues/344120
it replaces a browser confirm modal
with GlModal for the file lock button.

Changelog: changed
EE: true
parent 0535308b
<script> <script>
import { GlButton } from '@gitlab/ui'; import { GlButton, GlModal } from '@gitlab/ui';
import createFlash from '~/flash'; import createFlash from '~/flash';
import { sprintf, __ } from '~/locale'; import { sprintf, __ } from '~/locale';
import lockPathMutation from '~/repository/mutations/lock_path.mutation.graphql'; import lockPathMutation from '~/repository/mutations/lock_path.mutation.graphql';
...@@ -8,9 +8,13 @@ export default { ...@@ -8,9 +8,13 @@ export default {
i18n: { i18n: {
lock: __('Lock'), lock: __('Lock'),
unlock: __('Unlock'), unlock: __('Unlock'),
modalTitle: __('Lock File?'),
actionPrimary: __('Okay'),
actionCancel: __('Cancel'),
}, },
components: { components: {
GlButton, GlButton,
GlModal,
}, },
props: { props: {
name: { name: {
...@@ -36,6 +40,7 @@ export default { ...@@ -36,6 +40,7 @@ export default {
}, },
data() { data() {
return { return {
isModalVisible: false,
lockLoading: false, lockLoading: false,
locked: this.isLocked, locked: this.isLocked,
}; };
...@@ -52,11 +57,14 @@ export default { ...@@ -52,11 +57,14 @@ export default {
}, },
}, },
methods: { methods: {
onLockToggle() { hideModal() {
// eslint-disable-next-line no-alert this.isModalVisible = false;
if (window.confirm(this.lockConfirmText)) { },
handleModalPrimary() {
this.toggleLock(); this.toggleLock();
} },
showModal() {
this.isModalVisible = true;
}, },
toggleLock() { toggleLock() {
this.lockLoading = true; this.lockLoading = true;
...@@ -82,7 +90,23 @@ export default { ...@@ -82,7 +90,23 @@ export default {
</script> </script>
<template> <template>
<gl-button :disabled="!canLock" :loading="lockLoading" @click="onLockToggle"> <span>
<gl-button :disabled="!canLock" :loading="lockLoading" @click="showModal">
{{ lockButtonTitle }} {{ lockButtonTitle }}
</gl-button> </gl-button>
<gl-modal
modal-id="lock-file-modal"
:visible="isModalVisible"
:title="$options.i18n.modalTitle"
:action-primary="{ text: $options.i18n.actionPrimary }"
:action-cancel="{ text: $options.i18n.actionCancel }"
@primary="handleModalPrimary"
@hide="hideModal"
>
<p>
{{ lockConfirmText }}
</p>
</gl-modal>
</span>
</template> </template>
import { GlButton } from '@gitlab/ui'; import { GlButton, GlModal } from '@gitlab/ui';
import { shallowMount, createLocalVue } from '@vue/test-utils'; import { shallowMount, createLocalVue } from '@vue/test-utils';
import { nextTick } from 'vue'; import { nextTick } from 'vue';
import VueApollo from 'vue-apollo'; import VueApollo from 'vue-apollo';
...@@ -39,18 +39,17 @@ describe('LockButton component', () => { ...@@ -39,18 +39,17 @@ describe('LockButton component', () => {
}); });
describe('lock button', () => { describe('lock button', () => {
let confirmSpy;
let lockMutationMock; let lockMutationMock;
const mockEvent = { preventDefault: jest.fn() };
const findLockButton = () => wrapper.find(GlButton); const findLockButton = () => wrapper.find(GlButton);
const findModal = () => wrapper.findComponent(GlModal);
const clickSubmit = () => findModal().vm.$emit('primary', mockEvent);
const clickHide = () => findModal().vm.$emit('hide', mockEvent);
beforeEach(() => { beforeEach(() => {
confirmSpy = jest.spyOn(window, 'confirm');
confirmSpy.mockImplementation(jest.fn());
lockMutationMock = jest.fn(); lockMutationMock = jest.fn();
}); });
afterEach(() => confirmSpy.mockRestore());
it('disables the lock button if canLock is set to false', () => { it('disables the lock button if canLock is set to false', () => {
createComponent({ canLock: false }); createComponent({ canLock: false });
...@@ -78,18 +77,24 @@ describe('LockButton component', () => { ...@@ -78,18 +77,24 @@ describe('LockButton component', () => {
expect(findLockButton().props('loading')).toBe(true); expect(findLockButton().props('loading')).toBe(true);
}); });
it('displays a confirm dialog when the lock button is clicked', () => { it('displays a confirm modal when the lock button is clicked', () => {
createComponent(); createComponent();
findLockButton().vm.$emit('click'); findLockButton().vm.$emit('click');
expect(findModal().text()).toBe('Are you sure you want to lock some_file.js?');
});
expect(confirmSpy).toHaveBeenCalledWith('Are you sure you want to lock some_file.js?'); it('should hide the confirm modal when a hide action is triggered', () => {
createComponent();
findLockButton().vm.$emit('click');
expect(wrapper.vm.isModalVisible).toBe(true);
clickHide();
expect(wrapper.vm.isModalVisible).toBe(false);
}); });
it('executes a lock mutation once lock is confirmed', () => { it('executes a lock mutation once lock is confirmed', async () => {
confirmSpy.mockReturnValue(true);
createComponent({}, lockMutationMock); createComponent({}, lockMutationMock);
findLockButton().vm.$emit('click'); findLockButton().vm.$emit('click');
clickSubmit();
expect(lockMutationMock).toHaveBeenCalledWith({ expect(lockMutationMock).toHaveBeenCalledWith({
filePath: 'some/path', filePath: 'some/path',
lock: true, lock: true,
...@@ -98,7 +103,6 @@ describe('LockButton component', () => { ...@@ -98,7 +103,6 @@ describe('LockButton component', () => {
}); });
it('does not execute a lock mutation if lock not confirmed', () => { it('does not execute a lock mutation if lock not confirmed', () => {
confirmSpy.mockReturnValue(false);
createComponent({}, lockMutationMock); createComponent({}, lockMutationMock);
findLockButton().vm.$emit('click'); findLockButton().vm.$emit('click');
......
...@@ -21420,6 +21420,9 @@ msgstr "" ...@@ -21420,6 +21420,9 @@ msgstr ""
msgid "Lock %{issuableDisplayName}" msgid "Lock %{issuableDisplayName}"
msgstr "" msgstr ""
msgid "Lock File?"
msgstr ""
msgid "Lock memberships to LDAP synchronization" msgid "Lock memberships to LDAP synchronization"
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