create favorite page

parent f92e7a55
Pipeline #1041 failed with stages
in 0 seconds
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
margin: 10px; margin: 10px;
} }
#button-follow{ #button-favorite, #button-unfavorite, #button-remove{
position: absolute; position: absolute;
top: 20%; top: 20%;
right: 5%; right: 5%;
...@@ -133,3 +133,8 @@ ...@@ -133,3 +133,8 @@
transition: 1s; transition: 1s;
text-decoration: none; text-decoration: none;
} }
.radio-btn {
position: relative;
top: 150px;
left: 40px;
}
class JobFavoritesController < ApplicationController class JobFavoritesController < ApplicationController
before_action :sign_in_validation, only: [:create, :destroy] before_action :sign_in_validation, only: %i[create destroy show]
def show
@count = current_user.favorite_jobs.count
@favorited_jobs = current_user.favorite_jobs.order_favorite.page(params[:page]).per(Job::LIMIT_PAGE)
end
def create def create
@job = Job.find_by_id(params[:job_id])
return if current_user.favorite_jobs.exists?(job_id: params[:job_id])
@follow = current_user.favorite!(params[:job_id])
respond_to { |format| format.js }
end end
def destroy def destroy
@job = Job.find_by_id(params[:job_id])
@unfollow = current_user.unfavorite!(params[:job_id])
respond_to { |format| format.js }
@count = current_user.favorite_jobs.count
count_on_page = @count % Job::LIMIT_PAGE
page_number = @count / Job::LIMIT_PAGE
link_url = request.referer.to_s.split("=")
return redirect_to link_url[0] + "=" + page_number.to_s if count_on_page == 0 && link_url[1].to_i == page_number + 1
return redirect_to request.referer if count_on_page == 0
end end
private private
def sign_in_validation def sign_in_validation
return if signed_in? return if signed_in?
store_location session[:return_to] = request.referer
flash[:warning] = Settings.user.warning_signin flash[:warning] = Settings.user.warning_signin
redirect_to login_path redirect_to login_path
end end
......
class FavoriteJob < ApplicationRecord class FavoriteJob < ApplicationRecord
belongs_to :user belongs_to :user
belongs_to :job belongs_to :job
scope :order_favorite, -> { order("favorite_jobs.updated_at DESC") }
end end
...@@ -3,7 +3,7 @@ class User < ApplicationRecord ...@@ -3,7 +3,7 @@ class User < ApplicationRecord
before_create :create_remember_token before_create :create_remember_token
mount_uploader :cv_user, UserCvUploader mount_uploader :cv_user, UserCvUploader
has_many :favorite_jobs, foreign_key: "job_id", dependent: :destroy has_many :favorite_jobs, foreign_key: "user_id", dependent: :destroy
has_many :jobs, through: :favorite_jobs has_many :jobs, through: :favorite_jobs
has_many :job_applieds has_many :job_applieds
has_many :jobs, through: :job_applieds has_many :jobs, through: :job_applieds
...@@ -20,15 +20,15 @@ class User < ApplicationRecord ...@@ -20,15 +20,15 @@ class User < ApplicationRecord
PASSWORD_FORMAT = /\A(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/x PASSWORD_FORMAT = /\A(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/x
validates :password, format: { with: PASSWORD_FORMAT, message: "is too short or not strength" } validates :password, format: { with: PASSWORD_FORMAT, message: "is too short or not strength" }
def following?(job_id) def favoriting?(job_id)
favorite_jobs.find_by(job_id: job_id) favorite_jobs.find_by(job_id: job_id)
end end
def follow!(job_id) def favorite!(job_id)
favorite_jobs.create!(job_id: job_id) favorite_jobs.create!(job_id: job_id)
end end
def unfollow!(job_id) def unfavorite!(job_id)
favorite_jobs.find_by(job_id: job_id).destroy favorite_jobs.find_by(job_id: job_id).destroy
end end
......
<div class="job-id-<%= favorited_job.job.id %>">
<div class="border border-dark rounded">
<div class="row">
<div class="radio-btn col-1">
<%= radio_button_tag :job_id, favorited_job.job.id %>
</div>
<div class="job-details col-10">
<div class="title">
<%= link_to job_detail_path(favorited_job.job.id) do %><strong>
<%= favorited_job.job.title %></strong>
<% end %>
</div>
<div class="row">
<div class="introduction col-10">
<%= strip_tags(favorited_job.job.format_desc) %><br>
<%= link_to 'Read more..', job_detail_path(favorited_job.job.id) %>
</div>
<div class="col-6">
<%= favorited_job.job.cities.map(&:name).join(' | ') %>
</div>
<div class="salary col-3">
Salary: <%= favorited_job.job.salary %>
</div>
<%= form_for(:job_favorite, url: unfavorite_job_path(job_id: favorited_job.job.id), method: :delete, remote: true) do |f| %>
<%= f.submit "Remove", class: "btn btn-danger", id: "button-remove" %>
<% end %>
</div>
</div>
</div>
</div>
</div>
<br>
$("#favorite-form-<%= @job.id %>").html("<%=escape_javascript render 'users/unfavorite', job_id: @job.id %>");
$("#favorite-form-<%= @job.id %>").html("<%= j render 'users/favorite', job_id: @job.id %>");
$(".job-id-<%= @job.id %>").remove();
$(".count-favorite-jobs").html("<%= @count %>");
<div class="container">
<% if @favorited_jobs.count > 0 %>
<h1 class="text-center my-job-label">My Favorite Job (<span class="count-favorite-jobs"><%= @count %></span> jobs)</h1>
<%= paginate @favorited_jobs, outer_window: 2, window: 1 %>
<div class="favorite-page">
<%= render partial: "job_favorites/my_favorite_jobs", collection: @favorited_jobs, as: :favorited_job %>
</div>
<%= paginate @favorited_jobs, outer_window: 2, window: 1 %>
<% else %>
<h1 class="let-favorite">Let's Save Job in Your Favorite</h1>
<% end %>
</div>
...@@ -16,7 +16,11 @@ ...@@ -16,7 +16,11 @@
<%= link_to 'Read more..', job_detail_path(job.id) %> <%= link_to 'Read more..', job_detail_path(job.id) %>
</div> </div>
</div> </div>
<button type="button" class="btn btn-success" id="button-follow">♥ Favorite</button> <% if signed_in? %>
<%= render partial: 'users/favorite_form', locals: { job: job } %>
<% else %>
<%= render 'users/favorite', job_id: job.id %>
<% end %>
</div> </div>
</div> </div>
<br> <br>
......
...@@ -75,22 +75,22 @@ ...@@ -75,22 +75,22 @@
<div class="row under-descrip"> <div class="row under-descrip">
<div class="col-6"> <div class="col-6">
<% if signed_in? && @user.present? %> <% if signed_in? && @user.present? %>
<div class="btn btn-info btn-lg apply-btn"> <div class="btn btn-info apply-btn">
<strong>Applied</strong> <strong>Applied</strong>
</div> </div>
<% else %> <% else %>
<%= link_to apply_job_path(job_id: @job.id) do %> <%= link_to apply_job_path(job_id: @job.id) do %>
<div class="btn btn-info btn-lg apply-btn"> <div class="btn btn-info apply-btn">
<strong>Apply Now</strong> <strong>Apply Now</strong>
</div> </div>
<% end %> <% end %>
<% end %> <% end %>
</div> </div>
<div class="col-6"> <div class="col-3">
<%= link_to '#' do %> <% if signed_in? %>
<div class="btn btn-danger btn-lg favorite-btn"> <%= render partial: 'users/favorite_form', locals: { job: @job } %>
<strong>Favorite</strong> <% else %>
</div> <%= render 'users/favorite', job_id: @job.id %>
<% end %> <% end %>
</div> </div>
</div> </div>
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
<li><%= link_to "Log In", login_path , class: "nav-item nav-link" %></li> <li><%= link_to "Log In", login_path , class: "nav-item nav-link" %></li>
<li><%= link_to "Register", register_step1_path, class: "nav-item nav-link" %></li> <li><%= link_to "Register", register_step1_path, class: "nav-item nav-link" %></li>
<% end %> <% end %>
<li><%= link_to "Favorite", '#', class: "nav-item nav-link" %></li> <li><%= link_to "Favorite", my_favorite_job_path, class: "nav-item nav-link" %></li>
<li><%= link_to "History", '#', class: "nav-item nav-link" %></li> <li><%= link_to "History", '#', class: "nav-item nav-link" %></li>
</ul> </ul>
</nav> </nav>
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
<%= render 'layouts/search_bar' %> <%= render 'layouts/search_bar' %>
</div> </div>
<div class="job-list"> <div class="job-list">
<%= render partial: "layouts/show_job", collection: @jobs, as: :job %> <%= render partial: "users/show_job", collection: @jobs, as: :job %>
</div> </div>
<div class="city-banner rounded">City</div> <div class="city-banner rounded">City</div>
......
<%= form_for(:job_favorite, url: favorite_job_path(job_id: job_id), method: :post, remote: true) do |f| %>
<%= f.submit "Favorite", class: "btn btn-danger", id: "button-favorite" %>
<% end %>
<div id="favorite-form-<%= job.id %>">
<% if current_user.favoriting?(job.id) %>
<%= render 'users/unfavorite', job_id: job.id %>
<% else %>
<%= render 'users/favorite', job_id: job.id %>
<% end %>
</div>
...@@ -17,8 +17,10 @@ ...@@ -17,8 +17,10 @@
<%= link_to 'Read more..', job_detail_path(job.id) %> <%= link_to 'Read more..', job_detail_path(job.id) %>
</div> </div>
</div> </div>
<%= form_for(:job_favorite, url: follow_job_path(job.id), remote: true) do |f| %> <% if signed_in? %>
<%= f.submit "Follow", class: "btn btn-primary", id: "button-follow" %> <%= render partial: 'users/favorite_form', locals: { job: job } %>
<% else %>
<%= render 'users/favorite', job_id: job.id %>
<% end %> <% end %>
</div> </div>
</div> </div>
......
<%= form_for(:job_unfavorite, url: unfavorite_job_path(job_id: job_id), method: :delete, remote: true) do |f| %>
<%= f.submit "Unfavorite", class: "btn btn-outline-danger", id: "button-unfavorite" %>
<% end %>
...@@ -33,7 +33,9 @@ Rails.application.routes.draw do ...@@ -33,7 +33,9 @@ Rails.application.routes.draw do
get 'jobs/industry/:converted_name', to: 'jobs#industry_jobs', as: :industry_jobs get 'jobs/industry/:converted_name', to: 'jobs#industry_jobs', as: :industry_jobs
get 'jobs/company/:converted_name', to: 'jobs#company_jobs', as: :company_jobs get 'jobs/company/:converted_name', to: 'jobs#company_jobs', as: :company_jobs
post 'follow_job/:id', to: 'job_favorites#create', as: :follow_job post 'favorite_job', to: 'job_favorites#create', as: :favorite_job
delete 'unfavorite_job', to: 'job_favorites#destroy', as: :unfavorite_job
get 'favorite', to: 'job_favorites#show', as: :my_favorite_job
resources :job_favorites, only: [:create, :destroy] resources :job_favorites, only: [:create, :destroy]
resources :job_applieds,only: [:new, :create] resources :job_applieds,only: [:new, :create]
......
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