Commit 14ac61dc by nnnghia98

fix conflict between fav and apply button

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