Skip to content

Commit

Permalink
Allow updating of visa information by admin
Browse files Browse the repository at this point in the history
  • Loading branch information
jahseng-lee committed Apr 9, 2024
1 parent f63cd96 commit a86ca4a
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.bootstrap.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ $background-link-hover-dark: lighten($primary, 20%);
@import 'reviews';
@import 'search_location';
@import 'users';
@import 'visa_informations';

body {
padding-top: 4rem;
Expand Down
6 changes: 6 additions & 0 deletions app/assets/stylesheets/visa_informations.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.edit-visa-form {
height: 100%;

position: sticky;
top: 5rem;
}
50 changes: 50 additions & 0 deletions app/controllers/visa_informations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,50 @@ def show
@location = Location.find(params[:location_id])
end

def edit
@location = Location.find(params[:location_id])
@visa_information = params[:id].present? ? (
VisaInformation.find_by(id: params[:id])
) : (
VisaInformation.generic(country: @location.country)
)
@visa_informations = VisaInformation
.includes(:citizenship)
.where(
country: @location.country
)
.order("countries.name")
end

def update
@location = Location.find(params[:location_id])
@visa_information = VisaInformation.find(params[:id])

@visa_information.assign_attributes(
body: update_params[:body]
)

if @visa_information.save
flash.now[:success_update_visa] = "Updated visa info."
else
flash.now[:error_update_visa] = "Couldn't update visa info." \
" Please try again."
end

respond_to do |format|
format.turbo_stream do
render turbo_stream: turbo_stream.replace(
"update-visa-info-form",
partial: "visa_informations/update_visa_info_form",
locals: {
location: @location,
visa_information: @visa_information
}
)
end
end
end

def content
@location = Location.find(params[:location_id])

Expand Down Expand Up @@ -62,4 +106,10 @@ def report_issue_modal
end
end
end

private

def update_params
params.require(:visa_information).permit(:body)
end
end
10 changes: 10 additions & 0 deletions app/helpers/visa_informations_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module VisaInformationsHelper
def helper_edit_visa_info_heading(location:, visa_information:)
heading = "Editing visa information for #{location.country.name}"
if visa_information.citizenship.present?
heading << " for citizens of #{visa_information.citizenship.name}"
end

heading
end
end
26 changes: 26 additions & 0 deletions app/views/visa_informations/_update_visa_info_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<%= turbo_frame_tag "update-visa-info-form" do %>
<% if flash[:success_update_visa].present? %>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<%= flash[:success_update_visa] %>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<% end %>

<% if flash[:error_update_visa].present? %>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<%= flash[:error_update_visa] %>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<% end %>

<%= form_for [@location, @visa_information] do |f| %>
<%
# Need to set the ID as the route is `resource`, not
# `resources`
%>
<%= hidden_field_tag :id, @visa_information.id %>

<%= f.text_area :body, class: "form-control mb-3", rows: 15 %>
<%= f.submit "Save", class: "btn btn-success" %>
<% end %>
<% end %>
50 changes: 50 additions & 0 deletions app/views/visa_informations/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<div class="container mt-3">
<%= link_to location_visa_information_path(@location) do %>
<i class="bi bi-chevron-left"></i>
Back
<% end %>

<div class="row">
<div class="col-2 mt-2">
<strong>For citizens of...</strong>

<ul class="nav nav-pills flex-column">
<% @visa_informations.each do |visa_info| %>
<li class="nav-item">
<%= link_to(
visa_info.citizenship&.name || "Generic",
edit_location_visa_information_path(
location_id: @location.id,
id: visa_info.id
),
class: "nav-link#{ visa_info.id == @visa_information&.id ? " active" : nil}"
) %>
</li>
<% end %>
</ul>
</div>

<div class="edit-visa-form col">
<h1 class="fs-3 mt-2 mb-3">
<%= helper_edit_visa_info_heading(
location: @location,
visa_information: @visa_information
) %>
</h1>

<%= render partial: "visa_informations/update_visa_info_form",
locals: {
location: @location,
visa_information: @visa_information
}
%>
</div>
</div>
</div>

<%
set_meta_tags(
index: false,
follow: false
)
%>
12 changes: 12 additions & 0 deletions app/views/visa_informations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@

<div class="col">
<% if current_user.present? %>
<% if current_user.admin? %>
<%= link_to(
edit_location_visa_information_path(
location_id: @location.id
),
class: "btn btn-primary"
) do %>
<i class="bi bi-pencil-fill"></i>
Edit
<% end %>
<% end %>

<%= link_to "Report issue",
report_issue_modal_location_visa_information_path,
method: :get,
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
resources :banner_images, only: [:create, :update]
resources :countries, only: [:edit, :update]
resources :reviews, only: [:index, :show, :new, :create, :edit, :update]
resource :visa_information, only: [:show] do
resource :visa_information, only: [:show, :edit, :update] do
collection do
get :content
get :report_issue_modal
Expand Down

0 comments on commit a86ca4a

Please sign in to comment.