Commit 50b1b777 authored by Kushal Pandya's avatar Kushal Pandya

Refactor `handleToggleTodo` to be DRY

parent b2aac5ee
...@@ -178,6 +178,14 @@ export default { ...@@ -178,6 +178,14 @@ export default {
saveEndDate(date) { saveEndDate(date) {
return this.saveDate('end', date); return this.saveDate('end', date);
}, },
saveTodoState({ count, deletePath }) {
this.savingTodoAction = false;
this.store.setTodoExists(!this.store.todoExists);
if (deletePath) {
this.store.setTodoDeletePath(deletePath);
}
$(document).trigger('todo:toggle', count);
},
handleLabelClick(label) { handleLabelClick(label) {
if (label.isAny) { if (label.isAny) {
this.epicContext.labels = []; this.epicContext.labels = [];
...@@ -223,12 +231,11 @@ export default { ...@@ -223,12 +231,11 @@ export default {
if (!this.store.todoExists) { if (!this.store.todoExists) {
this.service this.service
.addTodo(this.epicId) .addTodo(this.epicId)
.then(res => res.data) .then(({ data }) => {
.then(data => { this.saveTodoState({
this.savingTodoAction = false; count: data.count,
this.store.setTodoDeletePath(data.delete_path); deletePath: data.delete_path,
this.store.setTodoExists(!this.store.todoExists); });
$(document).trigger('todo:toggle', data.count);
}) })
.catch(() => { .catch(() => {
this.savingTodoAction = false; this.savingTodoAction = false;
...@@ -237,11 +244,10 @@ export default { ...@@ -237,11 +244,10 @@ export default {
} else { } else {
this.service this.service
.deleteTodo(this.store.todoDeletePath) .deleteTodo(this.store.todoDeletePath)
.then(res => res.data) .then(({ data }) => {
.then(data => { this.saveTodoState({
this.savingTodoAction = false; count: data.count,
this.store.setTodoExists(!this.store.todoExists); });
$(document).trigger('todo:toggle', data.count);
}) })
.catch(() => { .catch(() => {
this.savingTodoAction = false; this.savingTodoAction = false;
......
...@@ -99,7 +99,7 @@ describe('epicSidebar', () => { ...@@ -99,7 +99,7 @@ describe('epicSidebar', () => {
}); });
it('should render collapsed grouped date picker', () => { it('should render collapsed grouped date picker', () => {
expect(vm.$el.querySelector('.sidebar-collapsed-icon span').innerText.trim()).toEqual('From Jan 1 2017'); expect(vm.$el.querySelector('.sidebar-grouped-item .sidebar-collapsed-icon span').innerText.trim()).toEqual('From Jan 1 2017');
}); });
it('should render collapsed labels picker', () => { it('should render collapsed labels picker', () => {
...@@ -225,7 +225,7 @@ describe('epicSidebar', () => { ...@@ -225,7 +225,7 @@ describe('epicSidebar', () => {
beforeEach(() => { beforeEach(() => {
mock = new MockAdapter(axios); mock = new MockAdapter(axios);
document.body.innerHTML += '<div class="flash-container"></div>'; setFixtures('<div class="flash-container"></div>');
}); });
afterEach(() => { afterEach(() => {
......
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