Skip to content

Commit

Permalink
Add link to update task state
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuki-jpn20 committed Jun 13, 2023
1 parent c410fa8 commit b7e82ea
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
18 changes: 16 additions & 2 deletions app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# coding: utf-8
class TasksController < ApplicationController
before_action :set_task, only: %i[ show edit update destroy ]
before_action :set_task, only: %i[ show edit update destroy complete ]
before_action :get_form_data, only: %i[ new edit ]
before_action :logged_in_user, only: %i[ new create edit update destroy]
before_action :logged_in_user, only: %i[ new create edit update destroy complete ]
before_action :search, only: %i[ index ]

def search
Expand Down Expand Up @@ -67,6 +67,16 @@ def update
end
end

def complete
task_state_name = params[:task_state_name]
@task.task_state_id = TaskState.find_by(name: task_state_name).id
if @task.update(task_state_params)
flash[:success] = "タスクの状態を更新しました"
else
flash[:warning] = "タスクの状態の更新に失敗しました"
end
end

# DELETE /tasks/1 or /tasks/1.json
def destroy
@task.destroy
Expand Down Expand Up @@ -95,6 +105,10 @@ def task_params
params.require(:task).permit(:assigner_id, :due_at, :content, :description, :project_id, :task_state_id)
end

def task_state_params
params.permit(:task_state_id)
end

def parse_tag_names(tag_names)
@task.tags = tag_names.split.map do |tag_name|
tag = Tag.find_by(name: tag_name)
Expand Down
18 changes: 14 additions & 4 deletions app/views/tasks/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
<div>
<p class="text-xl">
<% if task.state.name=="todo" %>
<i class="far fa-dot-circle"></i>
<% end %>
<% if task.state.name=="done" %>
<i class="far fa-check-circle"></i>
<i id="task_<%= task.id %>" class="far fa-dot-circle"></i>
<% elsif task.state.name=="done" %>
<i id="task_<%= task.id %>" class="far fa-check-circle"></i>
<% elsif task.state.name="draft" %>
<i id="task_<%= task.id %>" class="far fa-circle"></i>
<% end %>
<%= link_to task.content, task %>
</p>
Expand All @@ -45,6 +46,7 @@
<%= link_to '詳細', task %>
<%= link_to '編集', edit_task_path(task) %>
<%= link_to '削除', task, method: :delete, data: { confirm: 'このタスクを削除しますか?' } %>
<%= link_to '完了', complete_path(task, task_state_name: "done"), method: :put, onclick: "updateState(#{task.id})" %>
<% end %>
</div>
<% end %>
Expand All @@ -54,3 +56,11 @@
<%= raw(paginate @tasks) %>
</div>
</div>

<script>
function updateState(task_id){
id = "task_" + task_id;
element = document.getElementById(id);
element.classList.toggle('fa-check-circle');
}
</script>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
delete '/logout', to: 'sessions#destroy'
get '/login', to: 'sessions#login_with_passwd_auth'
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
put '/tasks/:id/complete', to: 'tasks#complete', as: :complete
end

0 comments on commit b7e82ea

Please sign in to comment.