Skip to content

Commit

Permalink
Make sure review error state handled for review form
Browse files Browse the repository at this point in the history
  • Loading branch information
jahseng-lee committed Dec 7, 2023
1 parent 13af0e9 commit deaeefe
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 127 deletions.
46 changes: 40 additions & 6 deletions app/controllers/reviews_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,26 @@ def create
location_id: @location.id
)
else
flash[:error_save_review] = "Couldn't save your review"

render :new
respond_to do |format|
format.html do
flash[:error_save_review] = "Couldn't save your review"

render :new
end

format.turbo_stream do
flash.now[:error_save_review] = "Couldn't save your review"

render turbo_stream: turbo_stream.replace(
"review-form",
partial: "reviews/shared/form",
locals: {
location: @location,
review: @review
}
)
end
end
end
end

Expand All @@ -62,9 +79,26 @@ def update
location_id: @location.id
)
else
flash[:error_save_review] = "Couldn't save your review"

render :new
respond_to do |format|
format.html do
flash[:error_save_review] = "Couldn't save your review"

render :edit
end

format.turbo_stream do
flash.now[:error_save_review] = "Couldn't save your review"

render turbo_stream: turbo_stream.replace(
"review-form",
partial: "reviews/shared/form",
locals: {
location: @location,
review: @review
}
)
end
end
end
end

Expand Down
244 changes: 123 additions & 121 deletions app/views/reviews/shared/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,140 +1,142 @@
<% if flash[:error_save_review].present? %>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<%= flash[:error_save_review] %>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<% end %>
<%= turbo_frame_tag "review-form" do %>
<% if flash[:error_save_review].present? %>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<%= flash[:error_save_review] %>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<% end %>

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

<%= form_for [location, review], data: { turbo_frame: "_top" } do |f| %>
<div class="row mb-3">
<%= f.label :overall, class: "col-2 col-form-label" %>
<div class="col-2">
<%= f.select(
:overall,
helper_review_rating_options,
{
include_blank: "",
selected: review.overall
},
{
class: "form-select#{review.errors[:overall].any? ? ' is-invalid' : ''}",
aria: {
label: "Overall rating",
<%= form_for [location, review], data: { turbo_frame: "_top" } do |f| %>
<div class="row mb-3">
<%= f.label :overall, class: "col-2 col-form-label" %>
<div class="col-2">
<%= f.select(
:overall,
helper_review_rating_options,
{
include_blank: "",
selected: review.overall
},
{
class: "form-select#{review.errors[:overall].any? ? ' is-invalid' : ''}",
aria: {
label: "Overall rating",
}
}
}
) %>
) %>
</div>
</div>
</div>

<div class="row mb-3">
<%= f.label :fun, class: "col-2 col-form-label" %>
<div class="col-2">
<%= f.select(
:fun,
helper_review_rating_options,
{
include_blank: "",
selected: review.fun
},
{
class: "form-select#{review.errors[:fun].any? ? ' is-invalid' : ''}",
aria: {
label: "Fun rating",
<div class="row mb-3">
<%= f.label :fun, class: "col-2 col-form-label" %>
<div class="col-2">
<%= f.select(
:fun,
helper_review_rating_options,
{
include_blank: "",
selected: review.fun
},
{
class: "form-select#{review.errors[:fun].any? ? ' is-invalid' : ''}",
aria: {
label: "Fun rating",
}
}
}
) %>
) %>
</div>
</div>
</div>

<div class="row mb-3">
<%= f.label :cost, class: "col-2 col-form-label" %>
<div class="col-2">
<%= f.select(
:cost,
helper_review_rating_options,
{
include_blank: "",
selected: review.cost
},
{
class: "form-select#{review.errors[:cost].any? ? ' is-invalid' : ''}",
aria: {
label: "Cost rating",
<div class="row mb-3">
<%= f.label :cost, class: "col-2 col-form-label" %>
<div class="col-2">
<%= f.select(
:cost,
helper_review_rating_options,
{
include_blank: "",
selected: review.cost
},
{
class: "form-select#{review.errors[:cost].any? ? ' is-invalid' : ''}",
aria: {
label: "Cost rating",
}
}
}
) %>
) %>
</div>
</div>
</div>

<div class="row mb-3">
<%= f.label :internet, class: "col-2 col-form-label" %>
<div class="col-2">
<%= f.select(
:internet,
helper_review_rating_options,
{
include_blank: "",
selected: review.internet
},
{
class: "form-select#{review.errors[:internet].any? ? ' is-invalid' : ''}",
aria: {
label: "Internet rating",
<div class="row mb-3">
<%= f.label :internet, class: "col-2 col-form-label" %>
<div class="col-2">
<%= f.select(
:internet,
helper_review_rating_options,
{
include_blank: "",
selected: review.internet
},
{
class: "form-select#{review.errors[:internet].any? ? ' is-invalid' : ''}",
aria: {
label: "Internet rating",
}
}
}
) %>
) %>
</div>
</div>
</div>

<div class="row mb-3">
<%= f.label :safety, class: "col-2 col-form-label" %>
<div class="col-2">
<%= f.select(
:safety,
helper_review_rating_options,
{
include_blank: "",
selected: review.safety
},
{
class: "form-select#{review.errors[:safety].any? ? ' is-invalid' : ''}",
aria: {
label: "Safety rating",
<div class="row mb-3">
<%= f.label :safety, class: "col-2 col-form-label" %>
<div class="col-2">
<%= f.select(
:safety,
helper_review_rating_options,
{
include_blank: "",
selected: review.safety
},
{
class: "form-select#{review.errors[:safety].any? ? ' is-invalid' : ''}",
aria: {
label: "Safety rating",
}
}
}
) %>
) %>
</div>
</div>
</div>

<%= f.label :body, "Summary", class: "form-label"%>
<%= f.text_area :body,
class: "form-control mb-3",
rows: 6
%>
<%= f.button(
"Finish",
type: :submit,
class: "btn btn-primary mb-2",
) %>
<% end %>
<%= f.label :body, "Summary", class: "form-label"%>
<%= f.text_area :body,
class: "form-control mb-3",
rows: 6
%>
<%= f.button(
"Finish",
type: :submit,
class: "btn btn-primary mb-2",
) %>
<% end %>

<% if current_user.admin? %>
<%= button_to(
"Auto-generate review",
generate_review_location_reviews_path(
review, location_id: location.id
),
method: :post,
class: "btn btn-secondary",
data: {
turbo: false,
}
) %>
<% if current_user.admin? %>
<%= button_to(
"Auto-generate review",
generate_review_location_reviews_path(
review, location_id: location.id
),
method: :post,
class: "btn btn-secondary",
data: {
turbo: false,
}
) %>
<% end %>
<% end %>

0 comments on commit deaeefe

Please sign in to comment.