-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactors feedback form. #770
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
class FeedbackFormsController < ApplicationController | ||
EMAIL_PRESENT_MESSAGE = 'You have filled in a field that makes you appear as a spammer. Please follow the directions for the individual form fields.' | ||
MESSAGE_BLANK_MESSAGE = 'A message is required' | ||
RECAPTCHA_MESSAGE = 'You must pass the reCAPTCHA challenge' | ||
|
||
# The client handles rendering flash in this case, so clear it on the server | ||
# side to prevent it from rendering on the next request. | ||
after_action :discard_flash!, only: :create, if: -> { request.xhr? } | ||
|
||
def new | ||
return render :new_frame if params[:frame] == 'true' | ||
|
||
render :new | ||
end | ||
def new; end | ||
|
||
def create | ||
if validate | ||
if pass_captcha? | ||
FeedbackMailer.submit_feedback(params, request.remote_ip).deliver_now | ||
flash[:success] = 'Thank you! Your feedback has been sent.' | ||
else | ||
flash[:error] = 'You must pass the reCAPTCHA challenge' | ||
end | ||
|
||
respond_to do |format| | ||
format.json do | ||
render json: flash | ||
end | ||
format.html do | ||
redirect_to params[:url] | ||
end | ||
end | ||
redirect_to params[:url] | ||
end | ||
|
||
protected | ||
|
||
def validate | ||
errors = [] | ||
errors << MESSAGE_BLANK_MESSAGE if params[:message].blank? | ||
errors << EMAIL_PRESENT_MESSAGE if params[:email_address].present? | ||
errors << RECAPTCHA_MESSAGE if current_user.blank? && !verify_recaptcha | ||
|
||
flash[:error] = errors.join('<br/>') unless errors.empty? | ||
flash[:error].nil? | ||
end | ||
|
||
def discard_flash! | ||
flash.discard | ||
def pass_captcha? | ||
current_user.present? || verify_recaptcha | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails | ||
import "controllers" | ||
import 'bootstrap'; | ||
import 'feedback_form'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Goodbye. |
||
import "@hotwired/turbo-rails" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Controller } from '@hotwired/stimulus' | ||
|
||
export default class extends Controller { | ||
static targets = ['form'] | ||
|
||
connect() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the only remaining JS that is necessary. |
||
this.formTarget['user_agent'].value = navigator.userAgent | ||
this.formTarget['viewport'].value = `width: ${window.innerWidth} height: ${innerHeight}` | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,53 @@ | ||
<%= render 'shared/feedback_forms/form' %> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Merged the many feedback form partials. |
||
<turbo-frame id="feedback-form-frame"> | ||
<div class="row justify-content-md-center"> | ||
<div class="col-sm-6 my-3"> | ||
<%= form_tag feedback_form_path, method: :post, class:"form-horizontal feedback-form", role:"form", data: { turbo: false, controller: 'feedback', feedback_target: 'form' } do %> | ||
<div class="row"> | ||
<div class="col-sm-12 px-4 mb-2"> | ||
<div class="alert alert-info" role="alert"> | ||
Reporting from: <span class="reporting-from-field"><%= request.referer %></span> | ||
<%= hidden_field_tag :url, request.referer, class:"reporting-from-field" %> | ||
</div> | ||
</div> | ||
</div> | ||
<%= hidden_field_tag :user_agent %> | ||
<%= hidden_field_tag :viewport %> | ||
<div class="mx-3"> | ||
<div class="form-group row mb-3"> | ||
<%= label_tag(:message, 'Message', class: "col-sm-3 col-form-label text-end") %> | ||
<div class="col-sm-9"> | ||
<%= text_area_tag :message, "", rows:"5", class:"form-control", required: true %> | ||
</div> | ||
</div> | ||
<div class="form-group row mb-3"> | ||
<%= label_tag(:name, 'Your name', class: "col-sm-3 col-form-label text-end") %> | ||
<div class="col-sm-9"> | ||
<%= text_field_tag :name, "", class:"form-control", required: true %> | ||
</div> | ||
</div> | ||
<div class="form-group row mb-3"> | ||
<%= label_tag(:to, 'Your email', class: "col-sm-3 col-form-label text-end") %> | ||
<div class="col-sm-9"> | ||
<%= email_field_tag :to, "", class:"form-control", required: true %> | ||
</div> | ||
</div> | ||
<% if current_user.blank? %> | ||
<div class="form-group row mb-3"> | ||
<div class="offset-sm-3 col-sm-9"> | ||
<%= recaptcha_tags %> | ||
|
||
<p>(Stanford users can avoid this Captcha by logging in.)</p> | ||
</div> | ||
</div> | ||
<% end %> | ||
<div class="form-group row"> | ||
<div class="offset-sm-3 col-sm-9"> | ||
<button type="submit" class="btn btn-primary">Send</button> | ||
<%= button_tag "Cancel", type: 'button', class: 'btn btn-link', data: { bs_toggle: 'collapse', bs_target: '#feedback-form' }, aria: { expanded: false, controls: 'feedback-form' } %> | ||
</div> | ||
</div> | ||
</div> | ||
<% end %> | ||
</div> | ||
</div> | ||
</turbo-frame> |
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
# Pin npm packages by running ./bin/importmap | ||
|
||
pin "application", preload: true | ||
pin "jquery", to: "https://ga.jspm.io/npm:[email protected]/dist/jquery.js" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Goodbye. |
||
pin "bootstrap", to: "https://ga.jspm.io/npm:[email protected]/dist/js/bootstrap.esm.js" | ||
pin "@popperjs/core", to: "https://ga.jspm.io/npm:@popperjs/[email protected]/lib/index.js" | ||
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This validation is now client-side.