Commit 02bbb50f authored by Kushal Pandya's avatar Kushal Pandya

Merge branch '244270-boards-create-moveissue-vuex-action' into 'master'

Boards - Move issue using VueX action

See merge request gitlab-org/gitlab!47866
parents a4255a00 ee9834f6
...@@ -85,6 +85,7 @@ export default { ...@@ -85,6 +85,7 @@ export default {
:disabled="disabled" :disabled="disabled"
:issues="listIssues" :issues="listIssues"
:list="list" :list="list"
:can-admin-list="canAdminList"
/> />
<!-- Will be only available in EE --> <!-- Will be only available in EE -->
......
...@@ -6,7 +6,6 @@ import boardCard from './board_card.vue'; ...@@ -6,7 +6,6 @@ import boardCard from './board_card.vue';
import eventHub from '../eventhub'; import eventHub from '../eventhub';
import boardsStore from '../stores/boards_store'; import boardsStore from '../stores/boards_store';
import { sprintf, __ } from '~/locale'; import { sprintf, __ } from '~/locale';
import glFeatureFlagMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { deprecatedCreateFlash as createFlash } from '~/flash'; import { deprecatedCreateFlash as createFlash } from '~/flash';
import { import {
getBoardSortableDefaultOptions, getBoardSortableDefaultOptions,
...@@ -25,7 +24,6 @@ export default { ...@@ -25,7 +24,6 @@ export default {
boardNewIssue, boardNewIssue,
GlLoadingIcon, GlLoadingIcon,
}, },
mixins: [glFeatureFlagMixin()],
props: { props: {
disabled: { disabled: {
type: Boolean, type: Boolean,
......
<script> <script>
import Draggable from 'vuedraggable';
import { mapActions, mapState } from 'vuex'; import { mapActions, mapState } from 'vuex';
import { GlLoadingIcon } from '@gitlab/ui'; import { GlLoadingIcon } from '@gitlab/ui';
import defaultSortableConfig from '~/sortable/sortable_config';
import { sortableStart, sortableEnd } from '~/boards/mixins/sortable_default_options';
import BoardNewIssue from './board_new_issue_new.vue'; import BoardNewIssue from './board_new_issue_new.vue';
import BoardCard from './board_card.vue'; import BoardCard from './board_card.vue';
import eventHub from '../eventhub'; import eventHub from '../eventhub';
import boardsStore from '../stores/boards_store'; import boardsStore from '../stores/boards_store';
import { sprintf, __ } from '~/locale'; import { sprintf, __ } from '~/locale';
import glFeatureFlagMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
export default { export default {
name: 'BoardList', name: 'BoardList',
...@@ -15,7 +17,6 @@ export default { ...@@ -15,7 +17,6 @@ export default {
BoardNewIssue, BoardNewIssue,
GlLoadingIcon, GlLoadingIcon,
}, },
mixins: [glFeatureFlagMixin()],
props: { props: {
disabled: { disabled: {
type: Boolean, type: Boolean,
...@@ -29,6 +30,11 @@ export default { ...@@ -29,6 +30,11 @@ export default {
type: Array, type: Array,
required: true, required: true,
}, },
canAdminList: {
type: Boolean,
required: false,
default: false,
},
}, },
data() { data() {
return { return {
...@@ -55,12 +61,32 @@ export default { ...@@ -55,12 +61,32 @@ export default {
loading() { loading() {
return this.listsFlags[this.list.id]?.isLoading; return this.listsFlags[this.list.id]?.isLoading;
}, },
listRef() {
// When list is draggable, the reference to the list needs to be accessed differently
return this.canAdminList ? this.$refs.list.$el : this.$refs.list;
},
treeRootWrapper() {
return this.canAdminList ? Draggable : 'ul';
},
treeRootOptions() {
const options = {
...defaultSortableConfig,
fallbackOnBody: false,
group: 'boards-list',
tag: 'ul',
'ghost-class': 'board-card-drag-active',
'data-list-id': this.list.id,
value: this.issues,
};
return this.canAdminList ? options : {};
},
}, },
watch: { watch: {
filters: { filters: {
handler() { handler() {
this.list.loadingMore = false; this.list.loadingMore = false;
this.$refs.list.scrollTop = 0; this.listRef.scrollTop = 0;
}, },
deep: true, deep: true,
}, },
...@@ -76,26 +102,26 @@ export default { ...@@ -76,26 +102,26 @@ export default {
}, },
mounted() { mounted() {
// Scroll event on list to load more // Scroll event on list to load more
this.$refs.list.addEventListener('scroll', this.onScroll); this.listRef.addEventListener('scroll', this.onScroll);
}, },
beforeDestroy() { beforeDestroy() {
eventHub.$off(`toggle-issue-form-${this.list.id}`, this.toggleForm); eventHub.$off(`toggle-issue-form-${this.list.id}`, this.toggleForm);
eventHub.$off(`scroll-board-list-${this.list.id}`, this.scrollToTop); eventHub.$off(`scroll-board-list-${this.list.id}`, this.scrollToTop);
this.$refs.list.removeEventListener('scroll', this.onScroll); this.listRef.removeEventListener('scroll', this.onScroll);
}, },
methods: { methods: {
...mapActions(['fetchIssuesForList']), ...mapActions(['fetchIssuesForList', 'moveIssue']),
listHeight() { listHeight() {
return this.$refs.list.getBoundingClientRect().height; return this.listRef.getBoundingClientRect().height;
}, },
scrollHeight() { scrollHeight() {
return this.$refs.list.scrollHeight; return this.listRef.scrollHeight;
}, },
scrollTop() { scrollTop() {
return this.$refs.list.scrollTop + this.listHeight(); return this.listRef.scrollTop + this.listHeight();
}, },
scrollToTop() { scrollToTop() {
this.$refs.list.scrollTop = 0; this.listRef.scrollTop = 0;
}, },
loadNextPage() { loadNextPage() {
const loadingDone = () => { const loadingDone = () => {
...@@ -120,6 +146,52 @@ export default { ...@@ -120,6 +146,52 @@ export default {
} }
}); });
}, },
handleDragOnStart() {
sortableStart();
},
handleDragOnEnd(params) {
sortableEnd();
const { newIndex, oldIndex, from, to, item } = params;
const { issueId, issueIid, issuePath } = item.dataset;
const { children } = to;
let moveBeforeId;
let moveAfterId;
const getIssueId = el => Number(el.dataset.issueId);
// If issue is being moved within the same list
if (from === to) {
if (newIndex > oldIndex && children.length > 1) {
// If issue is being moved down we look for the issue that ends up before
moveBeforeId = getIssueId(children[newIndex]);
} else if (newIndex < oldIndex && children.length > 1) {
// If issue is being moved up we look for the issue that ends up after
moveAfterId = getIssueId(children[newIndex]);
} else {
// If issue remains in the same list at the same position we do nothing
return;
}
} else {
// We look for the issue that ends up before the moved issue if it exists
if (children[newIndex - 1]) {
moveBeforeId = getIssueId(children[newIndex - 1]);
}
// We look for the issue that ends up after the moved issue if it exists
if (children[newIndex]) {
moveAfterId = getIssueId(children[newIndex]);
}
}
this.moveIssue({
issueId,
issueIid,
issuePath,
fromListId: from.dataset.listId,
toListId: to.dataset.listId,
moveBeforeId,
moveAfterId,
});
},
}, },
}; };
</script> </script>
...@@ -139,13 +211,18 @@ export default { ...@@ -139,13 +211,18 @@ export default {
<gl-loading-icon /> <gl-loading-icon />
</div> </div>
<board-new-issue v-if="list.type !== 'closed' && showIssueForm" :list="list" /> <board-new-issue v-if="list.type !== 'closed' && showIssueForm" :list="list" />
<ul <component
:is="treeRootWrapper"
v-show="!loading" v-show="!loading"
ref="list" ref="list"
v-bind="treeRootOptions"
:data-board="list.id" :data-board="list.id"
:data-board-type="list.type" :data-board-type="list.type"
:class="{ 'bg-danger-100': issuesSizeExceedsMax }" :class="{ 'bg-danger-100': issuesSizeExceedsMax }"
class="board-list gl-w-full gl-h-full gl-list-style-none gl-mb-0 gl-p-2 js-board-list" class="board-list gl-w-full gl-h-full gl-list-style-none gl-mb-0 gl-p-2 js-board-list"
data-testid="tree-root-wrapper"
@start="handleDragOnStart"
@end="handleDragOnEnd"
> >
<board-card <board-card
v-for="(issue, index) in issues" v-for="(issue, index) in issues"
...@@ -161,6 +238,6 @@ export default { ...@@ -161,6 +238,6 @@ export default {
<span v-if="issues.length === list.issuesSize">{{ __('Showing all issues') }}</span> <span v-if="issues.length === list.issuesSize">{{ __('Showing all issues') }}</span>
<span v-else>{{ paginatedIssueText }}</span> <span v-else>{{ paginatedIssueText }}</span>
</li> </li>
</ul> </component>
</div> </div>
</template> </template>
...@@ -9,7 +9,7 @@ import BoardList from '~/boards/components/board_list_new.vue'; ...@@ -9,7 +9,7 @@ import BoardList from '~/boards/components/board_list_new.vue';
import BoardCard from '~/boards/components/board_card.vue'; import BoardCard from '~/boards/components/board_card.vue';
import '~/boards/models/issue'; import '~/boards/models/issue';
import '~/boards/models/list'; import '~/boards/models/list';
import { listObj, mockIssuesByListId, issues } from './mock_data'; import { listObj, mockIssuesByListId, issues, mockIssues } from './mock_data';
import defaultState from '~/boards/stores/state'; import defaultState from '~/boards/stores/state';
const localVue = createLocalVue(); const localVue = createLocalVue();
...@@ -71,6 +71,7 @@ const createComponent = ({ ...@@ -71,6 +71,7 @@ const createComponent = ({
disabled: false, disabled: false,
list, list,
issues: [issue], issues: [issue],
canAdminList: true,
...componentProps, ...componentProps,
}, },
store, store,
...@@ -87,17 +88,19 @@ const createComponent = ({ ...@@ -87,17 +88,19 @@ const createComponent = ({
describe('Board list component', () => { describe('Board list component', () => {
let wrapper; let wrapper;
const findByTestId = testId => wrapper.find(`[data-testid="${testId}"]`);
useFakeRequestAnimationFrame(); useFakeRequestAnimationFrame();
afterEach(() => {
wrapper.destroy();
wrapper = null;
});
describe('When Expanded', () => { describe('When Expanded', () => {
beforeEach(() => { beforeEach(() => {
wrapper = createComponent(); wrapper = createComponent();
}); });
afterEach(() => {
wrapper.destroy();
});
it('renders component', () => { it('renders component', () => {
expect(wrapper.find('.board-list-component').exists()).toBe(true); expect(wrapper.find('.board-list-component').exists()).toBe(true);
}); });
...@@ -107,7 +110,7 @@ describe('Board list component', () => { ...@@ -107,7 +110,7 @@ describe('Board list component', () => {
state: { listsFlags: { 'gid://gitlab/List/1': { isLoading: true } } }, state: { listsFlags: { 'gid://gitlab/List/1': { isLoading: true } } },
}); });
expect(wrapper.find('[data-testid="board_list_loading"').exists()).toBe(true); expect(findByTestId('board_list_loading').exists()).toBe(true);
}); });
it('renders issues', () => { it('renders issues', () => {
...@@ -171,19 +174,15 @@ describe('Board list component', () => { ...@@ -171,19 +174,15 @@ describe('Board list component', () => {
}); });
}); });
afterEach(() => {
wrapper.destroy();
});
it('loads more issues after scrolling', () => { it('loads more issues after scrolling', () => {
wrapper.vm.$refs.list.dispatchEvent(new Event('scroll')); wrapper.vm.listRef.dispatchEvent(new Event('scroll'));
expect(actions.fetchIssuesForList).toHaveBeenCalled(); expect(actions.fetchIssuesForList).toHaveBeenCalled();
}); });
it('does not load issues if already loading', () => { it('does not load issues if already loading', () => {
wrapper.vm.$refs.list.dispatchEvent(new Event('scroll')); wrapper.vm.listRef.dispatchEvent(new Event('scroll'));
wrapper.vm.$refs.list.dispatchEvent(new Event('scroll')); wrapper.vm.listRef.dispatchEvent(new Event('scroll'));
expect(actions.fetchIssuesForList).toHaveBeenCalledTimes(1); expect(actions.fetchIssuesForList).toHaveBeenCalledTimes(1);
}); });
...@@ -204,10 +203,6 @@ describe('Board list component', () => { ...@@ -204,10 +203,6 @@ describe('Board list component', () => {
}); });
}); });
afterEach(() => {
wrapper.destroy();
});
describe('when issue count exceeds max issue count', () => { describe('when issue count exceeds max issue count', () => {
it('sets background to bg-danger-100', async () => { it('sets background to bg-danger-100', async () => {
wrapper.setProps({ list: { issuesSize: 4, maxIssueCount: 3 } }); wrapper.setProps({ list: { issuesSize: 4, maxIssueCount: 3 } });
...@@ -233,4 +228,43 @@ describe('Board list component', () => { ...@@ -233,4 +228,43 @@ describe('Board list component', () => {
}); });
}); });
}); });
describe('drag & drop issue', () => {
beforeEach(() => {
wrapper = createComponent();
});
describe('handleDragOnStart', () => {
it('adds a class `is-dragging` to document body', () => {
expect(document.body.classList.contains('is-dragging')).toBe(false);
findByTestId('tree-root-wrapper').vm.$emit('start');
expect(document.body.classList.contains('is-dragging')).toBe(true);
});
});
describe('handleDragOnEnd', () => {
it('removes class `is-dragging` from document body', () => {
jest.spyOn(wrapper.vm, 'moveIssue').mockImplementation(() => {});
document.body.classList.add('is-dragging');
findByTestId('tree-root-wrapper').vm.$emit('end', {
oldIndex: 1,
newIndex: 0,
item: {
dataset: {
issueId: mockIssues[0].id,
issueIid: mockIssues[0].iid,
issuePath: mockIssues[0].referencePath,
},
},
to: { children: [], dataset: { listId: 'gid://gitlab/List/1' } },
from: { dataset: { listId: 'gid://gitlab/List/2' } },
});
expect(document.body.classList.contains('is-dragging')).toBe(false);
});
});
});
}); });
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