Commit 7dc16fe6 authored by Jacob Schatz's avatar Jacob Schatz

Merge branch 'issue_14678' into 'master'

Refresh page according remaining todos

Fixes #14678 

See merge request !3428
parents 7e6d5906 3602aa7d
class @Todos
constructor: (@name) ->
constructor: (opts = {}) ->
{
@el = $('.js-todos-options')
} = opts
@perPage = @el.data('perPage')
@clearListeners()
@initBtnListeners()
......@@ -26,6 +32,7 @@ class @Todos
dataType: 'json'
data: '_method': 'delete'
success: (data) =>
@redirectIfNeeded data.count
@clearDone $this.closest('li')
@updateBadges data
......@@ -57,6 +64,40 @@ class @Todos
$('.todos-pending .badge, .todos-pending-count').text data.count
$('.todos-done .badge').text data.done_count
getTotalPages: ->
@el.data('totalPages')
getCurrentPage: ->
@el.data('currentPage')
getTodosPerPage: ->
@el.data('perPage')
redirectIfNeeded: (total) ->
currPages = @getTotalPages()
currPage = @getCurrentPage()
# Refresh if no remaining Todos
if not total
location.reload()
return
# Do nothing if no pagination
return if not currPages
newPages = Math.ceil(total / @getTodosPerPage())
url = location.href # Includes query strings
# If new total of pages is different than we have now
if newPages isnt currPages
# Redirect to previous page if there's one available
if currPages > 1 and currPage is currPages
pageParams =
page: currPages - 1
url = gl.utils.mergeUrlParams(pageParams, url)
Turbolinks.visit(url)
goToTodoUrl: (e)->
todoLink = $(this).data('url')
return unless todoLink
......
......@@ -45,6 +45,7 @@
.prepend-top-default
- if @todos.any?
.js-todos-options{ data: {per_page: @todos.limit_value, current_page: @todos.current_page, total_pages: @todos.total_pages} }
- @todos.group_by(&:project).each do |group|
.panel.panel-default.panel-small.js-todos-list
- project = group[0]
......
require 'spec_helper'
describe 'Dashboard Todos', feature: true do
let(:user){ create(:user) }
let(:author){ create(:user) }
let(:project){ create(:project) }
let(:issue){ create(:issue) }
let(:todos_per_page){ Todo.default_per_page }
let(:todos_total){ todos_per_page + 1 }
describe 'GET /dashboard/todos' do
context 'User does not have todos' do
before do
login_as(user)
visit dashboard_todos_path
end
it 'shows "All done" message' do
expect(page).to have_content "You're all done!"
end
end
context 'User has a todo', js: true do
before do
create(:todo, :mentioned, user: user, project: project, target: issue, author: author)
login_as(user)
visit dashboard_todos_path
end
it 'todo is present' do
expect(page).to have_selector('.todos-list .todo', count: 1)
end
describe 'deleting the todo' do
before do
first('.done-todo').click
end
it 'is removed from the list' do
expect(page).not_to have_selector('.todos-list .todo')
end
it 'shows "All done" message' do
expect(page).to have_content("You're all done!")
end
end
end
context 'User has multiple pages of Todos' do
let(:todo_total_pages){ (todos_total.to_f/todos_per_page).ceil }
before do
todos_total.times do
create(:todo, :mentioned, user: user, project: project, target: issue, author: author)
end
login_as(user)
visit dashboard_todos_path
end
it 'is paginated' do
expect(page).to have_selector('.gl-pagination')
end
it 'is has the right number of pages' do
expect(page).to have_selector('.gl-pagination .page', count: todo_total_pages)
end
describe 'deleting last todo from last page', js: true do
it 'redirects to the previous page' do
page.within('.gl-pagination') do
click_link todo_total_pages.to_s
end
first('.done-todo').click
expect(page).to have_content(Todo.last.body)
end
end
end
end
end
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