Commit 14ac61dc by nnnghia98

fix conflict between fav and apply button

parent 9ebefcc0
class FavoritesController < ApplicationController class FavoritesController < ApplicationController
before_action :authenticate_user!, only: [:create, :destroy] before_action :authenticate_user!, only: [:favorite, :unfavorite]
def create def favorite
redirect_to jobs_path if params[:id].blank? redirect_to jobs_path if params[:job_id].blank?
job_id = params[:id] job_id = params[:job_id]
if get_user_job if get_user_job
@favorite_jobs = get_user_job.where.not(favorited_at: nil) || @favorite_jobs = get_user_job.update(favorited_at: Time.current)
get_user_job.update(favorited_at: Time.current)
else else
@favorite_jobs = UserJob.create!(user_id: current_user.id, job_id: job_id, favorited_at: Time.current) @favorite_jobs = UserJob.create!(user_id: current_user.id, job_id: job_id, favorited_at: Time.current)
end end
redirect_to job_path(job_id)
end end
def destroy def unfavorite
redirect_to jobs_path if @job.id.blank? redirect_to jobs_path if params[:job_id].blank?
job_id = params[:id] job_id = params[:job_id]
@favorited_jobs = get_user_job.where.not(favorited_at: nil)
@favorited_jobs = get_user_job.update(favorited_at: nil) @favorited_jobs = get_user_job.update(favorited_at: nil)
redirect_to job_path(job_id) redirect_to job_path(job_id)
...@@ -26,6 +26,6 @@ class FavoritesController < ApplicationController ...@@ -26,6 +26,6 @@ class FavoritesController < ApplicationController
private private
def get_user_job def get_user_job
UserJob.find_by(user_id: current_user.id, job_id: params[:id]) UserJob.find_by(user_id: current_user.id, job_id: params[:job_id])
end end
end end
class JobsController < ApplicationController class JobsController < ApplicationController
before_action :authenticate_user!, only: [:apply, :confirm_apply, :finish_apply] before_action :authenticate_user!, only: [:apply, :confirm_apply, :finish_apply, :index, :show]
before_action :find_user, only: :apply_available before_action :find_user, only: :apply_available
before_action :validate_city_industry, only: :index before_action :validate_city_industry, only: :index
before_action :find_applied_jobs, only: [:apply, :confirm_apply, :finish_apply] before_action :find_applied_jobs, only: [:apply, :confirm_apply, :finish_apply]
...@@ -49,7 +49,8 @@ class JobsController < ApplicationController ...@@ -49,7 +49,8 @@ class JobsController < ApplicationController
end end
def apply_available def apply_available
user_jobs.find_by(job_id: @job_id, user_id: @user.id) binding.pry
user_jobs.where.not(applied_at: nil).find_by(job_id: @job_id, user_id: @user.id)
end end
private private
......
...@@ -7,4 +7,12 @@ module JobHelper ...@@ -7,4 +7,12 @@ module JobHelper
def job_applied_at(job) def job_applied_at(job)
job.user_jobs.find_by(user_id: current_user.id).applied_at job.user_jobs.find_by(user_id: current_user.id).applied_at
end end
def verify_favorited_job
UserJob.where.not(favorited_at: nil).find_by(user_id: current_user.id, job_id: @job.id)
end
def verify_applied_job
UserJob.where.not(applied_at: nil).find_by(user_id: current_user.id, job_id: @job.id)
end
end end
$("#favorite_form").html("<%= escape_javascript(render "shared/unfavorite_btn", job: @job %>");
$("#favorite_form").html("<%= escape_javascript(render "shared/favorite_btn", job: @job) %>"); $("#favorite_form").html("<%= escape_javascript(render "shared/favorite_btn") %>");
$("#favorite_form").html("<%= escape_javascript(render "shared/unfavorite_btn") %>");
...@@ -17,11 +17,4 @@ ...@@ -17,11 +17,4 @@
<dd><%= job["city"] %></dd> <dd><%= job["city"] %></dd>
</dl> </dl>
</div> </div>
<div id="favorite_form">
<% if current_user.favorites.exists?(job_id: job.id) %>
<%= render "shared/unfavorite_btn", job_id: job.id %>
<% else %>
<%= render "shared/favorite_btn", job_id: job.id %>
<% end %>
</div>
</div> </div>
...@@ -31,8 +31,15 @@ ...@@ -31,8 +31,15 @@
</div> </div>
<div class="job_detail_button"> <div class="job_detail_button">
<%= render "shared/favorite_btn" %> <div id="favorite_form">
<% if user_signed_in? && @job.apply_available(current_user) %> <% if verify_favorited_job %>
<%= render "shared/unfavorite_btn" %>
<% else %>
<%= render "shared/favorite_btn" %>
<% end %>
</div>
<% if user_signed_in? && verify_applied_job %>
<button type="button" class="btn btn-primary float-right mx-3" disabled>Apply</button> <button type="button" class="btn btn-primary float-right mx-3" disabled>Apply</button>
<% else %> <% else %>
<%= link_to "Apply", apply_path(job_id: @job.id), class: "btn btn-primary float-right mx-3" %> <%= link_to "Apply", apply_path(job_id: @job.id), class: "btn btn-primary float-right mx-3" %>
......
<% if user_signed_in? %> <% if user_signed_in? %>
<%= form_tag(jobs_favorites_path(job_id: @job.id) , remote: true) do %> <%= form_tag(favorite_path(job_id: @job.id), remote: true) do %>
<%= submit_tag "Favorite", class: "btn btn-primary float-right" %> <%= submit_tag "Favorite", class: "btn btn-primary float-right" %>
<% end %> <% end %>
<% else %> <% else %>
......
<% if user_signed_in? %> <% if user_signed_in? %>
<%= form_tag(jobs_favorite_path(job_id: @job.id), method: :delete, remote: true) do %> <%= form_tag(unfavorite_path(job_id: @job.id), remote: true) do %>
<%= submit_tag "Unfavorite", class: "btn btn-primary float-right" %> <%= submit_tag "Unfavorite", class: "btn btn-primary float-right" %>
<% end %> <% end %>
<% else %> <% else %>
......
...@@ -21,11 +21,7 @@ Rails.application.routes.draw do ...@@ -21,11 +21,7 @@ Rails.application.routes.draw do
end end
end end
get "admin", to: "admins#index", as: :admin get "admin", to: "admins#index", as: :admin
resource :favorites, only: [:create, :destroy]
# post "favorite", to: "favorites#create"
# delete "unfavorite", to: "favorites#destroy"
resource :jobs do post "favorite", to: "favorites#favorite"
resource :favorites, only: [:create, :destroy] post "unfavorite", to: "favorites#unfavorite"
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