Commit 330e9136 authored by Phil Hughes's avatar Phil Hughes

Uses update URL to update the status of a todo

parent 20d382a8
...@@ -47,27 +47,34 @@ class @Sidebar ...@@ -47,27 +47,34 @@ class @Sidebar
.off 'click', '.js-issuable-todo' .off 'click', '.js-issuable-todo'
.on 'click', '.js-issuable-todo', @toggleTodo .on 'click', '.js-issuable-todo', @toggleTodo
toggleTodo: -> toggleTodo: (e) =>
$this = $(@) $this = $(e.currentTarget)
$todoLoading = $('.js-issuable-todo-loading') $todoLoading = $('.js-issuable-todo-loading')
$btnText = $('.js-issuable-todo-text', $this) $btnText = $('.js-issuable-todo-text', $this)
ajaxType = if $this.attr('data-id') then 'PATCH' else 'POST'
ajaxUrlExtra = if $this.attr('data-id') then "/#{$this.attr('data-id')}" else ''
$.ajax( $.ajax(
url: $this.data('url') url: "#{$this.data('url')}#{ajaxUrlExtra}"
type: 'POST' type: ajaxType
dataType: 'json' dataType: 'json'
data: data:
todo_id: $this.attr('data-id')
issuable_id: $this.data('issuable') issuable_id: $this.data('issuable')
issuable_type: $this.data('issuable-type') issuable_type: $this.data('issuable-type')
beforeSend: -> beforeSend: =>
$this.disable() @beforeTodoSend($this, $todoLoading)
).done (data) =>
@todoUpdateDone(data, $this, $btnText, $todoLoading)
beforeTodoSend: ($btn, $todoLoading) ->
$btn.disable()
$todoLoading.removeClass 'hidden' $todoLoading.removeClass 'hidden'
).done (data) ->
todoUpdateDone: (data, $btn, $btnText, $todoLoading) ->
$todoPendingCount = $('.todos-pending-count') $todoPendingCount = $('.todos-pending-count')
$todoPendingCount.text data.count $todoPendingCount.text data.count
$this.enable() $btn.enable()
$todoLoading.addClass 'hidden' $todoLoading.addClass 'hidden'
if data.count is 0 if data.count is 0
...@@ -76,15 +83,15 @@ class @Sidebar ...@@ -76,15 +83,15 @@ class @Sidebar
$todoPendingCount.removeClass 'hidden' $todoPendingCount.removeClass 'hidden'
if data.todo? if data.todo?
$this $btn
.attr 'aria-label', $this.data('mark-text') .attr 'aria-label', $btn.data('mark-text')
.attr 'data-id', data.todo.id .attr 'data-id', data.todo.id
$btnText.text $this.data('mark-text') $btnText.text $btn.data('mark-text')
else else
$this $btn
.attr 'aria-label', $this.data('todo-text') .attr 'aria-label', $btn.data('todo-text')
.removeAttr 'data-id' .removeAttr 'data-id'
$btnText.text $this.data('todo-text') $btnText.text $btn.data('todo-text')
sidebarDropdownLoading: (e) -> sidebarDropdownLoading: (e) ->
$sidebarCollapsedIcon = $(@).closest('.block').find('.sidebar-collapsed-icon') $sidebarCollapsedIcon = $(@).closest('.block').find('.sidebar-collapsed-icon')
......
class Projects::TodosController < Projects::ApplicationController class Projects::TodosController < Projects::ApplicationController
def create def create
json_data = Hash.new
if params[:todo_id].nil?
TodoService.new.mark_todo(issuable, current_user) TodoService.new.mark_todo(issuable, current_user)
json_data[:todo] = current_user.todos.find_by(state: :pending, action: Todo::MARKED, target_id: issuable.id) render json: {
else todo: current_user.todos.find_by(state: :pending, action: Todo::MARKED, target_id: issuable.id),
current_user.todos.find_by_id(params[:todo_id]).update(state: :done) count: current_user.todos.pending.count,
}
end end
render json: json_data.merge({ count: current_user.todos.pending.count }) def update
current_user.todos.find_by_id(params[:id]).update(state: :done)
render json: {
count: current_user.todos.pending.count,
}
end end
private private
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
%a.gutter-toggle.pull-right.js-sidebar-toggle{ role: "button", href: "#", aria: { label: "Toggle sidebar" } } %a.gutter-toggle.pull-right.js-sidebar-toggle{ role: "button", href: "#", aria: { label: "Toggle sidebar" } }
= sidebar_gutter_toggle_icon = sidebar_gutter_toggle_icon
- if current_user - if current_user
%button.btn.btn-default.issuable-header-btn.pull-right.js-issuable-todo{ type: "button", aria: { label: (todo.nil? ? "Add Todo" : "Mark Done") }, data: { todo_text: "Add Todo", mark_text: "Mark Done", id: (todo.id unless todo.nil?), issuable: issuable.id, issuable_type: issuable.class.name.underscore, url: namespace_project_todos_path(@project.namespace, @project, :json) } } %button.btn.btn-default.issuable-header-btn.pull-right.js-issuable-todo{ type: "button", aria: { label: (todo.nil? ? "Add Todo" : "Mark Done") }, data: { todo_text: "Add Todo", mark_text: "Mark Done", id: (todo.id unless todo.nil?), issuable: issuable.id, issuable_type: issuable.class.name.underscore, url: namespace_project_todos_path(@project.namespace, @project) } }
%span.js-issuable-todo-text %span.js-issuable-todo-text
- if todo.nil? - if todo.nil?
Add Todo Add Todo
......
...@@ -789,7 +789,7 @@ Rails.application.routes.draw do ...@@ -789,7 +789,7 @@ Rails.application.routes.draw do
end end
end end
resources :todos, only: [:create], constraints: { id: /\d+/ } resources :todos, only: [:create, :update], constraints: { id: /\d+/ }
resources :uploads, only: [:create] do resources :uploads, only: [:create] do
collection do collection do
......
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