Add contact form

This commit is contained in:
Paul Chavard 2018-08-29 10:44:12 +01:00
parent 41815cfb1c
commit bad107ffae
5 changed files with 201 additions and 0 deletions

View file

@ -0,0 +1,12 @@
$default-space: 15px;
$contact-padding: $default-space * 2;
#contact-form {
width: 1040px;
margin: 0 auto;
padding-top: $contact-padding;
.description {
padding-bottom: $contact-padding;
}
}

View file

@ -0,0 +1,74 @@
class SupportController < ApplicationController
layout "new_application"
def index
setup_context
end
def create
if direct_message? && create_commentaire
flash.notice = "Votre message a été envoyé sur la messagerie de votre dossier."
redirect_to users_dossier_recapitulatif_path(dossier)
elsif create_conversation
flash.notice = "Votre message a été envoyé."
redirect_to root_path
else
setup_context
flash.now.alert = "Une erreur est survenue. Vous pouvez nous contactez à #{CONTACT_EMAIL}."
render :index
end
end
private
def setup_context
@dossier_id = dossier&.id
@tags = params[:tags]
@options = Helpscout::FormAdapter::OPTIONS
end
def create_conversation
Helpscout::FormAdapter.new(
subject: params[:subject],
email: email,
text: params[:text],
file: params[:file],
dossier_id: dossier&.id,
browser: browser_name,
tags: tags
).send_form
end
def create_commentaire
dossier.commentaires.create(
email: email,
file: params[:file],
body: "[#{params[:subject]}]<br><br>#{params[:text]}"
)
end
def tags
[params[:tags], params[:type]].flatten.compact
end
def browser_name
if browser.known?
"#{browser.name} #{browser.version} (#{browser.platform.name})"
end
end
def direct_message?
user_signed_in? && params[:type] == Helpscout::FormAdapter::TYPE_INSTRUCTION && dossier.present?
end
def dossier
@dossier ||= current_user&.dossiers&.find_by(id: params[:dossier_id])
end
def email
logged_user ? logged_user.email : params[:email]
end
end

View file

@ -0,0 +1,48 @@
- content_for(:title, 'Contact')
#contact-form
.container
%h1.new-h1 Contact
.description
Contactez-nous via ce formulaire et nous vous répondrons dans les plus brefs délais.
Pensez bien à nous donner le plus d'informations possible pour que nous puissions vous aider au mieux.
= form_tag contact_path, method: :post, multipart: true, class: 'form' do |f|
- if !logged_in?
.contact-champ
= label_tag :email do
Email
%span.mandatory *
= text_field_tag :email, '', required: true
.contact-champ
= label_tag :type do
Votre problème
%span.mandatory *
= select_tag :type, options_for_select(@options)
.contact-champ
= label_tag :dossier_id, 'Numéro du dossier concerné'
= text_field_tag :dossier_id, @dossier_id
.contact-champ
= label_tag :subject do
Sujet
%span.mandatory *
= text_field_tag :subject, '', required: true
.contact-champ
= label_tag :text do
Message
%span.mandatory *
= text_area_tag :text, '', rows: 6, required: true
.contact-champ
= label_tag :text, 'Pièce jointe'
= file_field_tag :file
= hidden_field_tag :tags, @tags
.send-wrapper
= button_tag 'Envoyer le message', type: :submit, class: 'button send primary'

View file

@ -120,6 +120,9 @@ Rails.application.routes.draw do
get "patron" => "root#patron"
get "contact" => "support#index"
post "contact" => "support#create"
#
# Deprecated UI
#

View file

@ -0,0 +1,64 @@
require 'spec_helper'
describe SupportController, type: :controller do
render_views
context 'signed in' do
before do
sign_in user
end
let(:user) { create(:user) }
describe "with dossier" do
let(:user) { dossier.user }
let(:dossier) { create(:dossier) }
it 'should fill dossier_id' do
get :index, params: { dossier_id: dossier.id }
expect(response.status).to eq(200)
expect(response.body).to include("#{dossier.id}")
end
it 'should not have email field' do
get :index
expect(response.status).to eq(200)
expect(response.body).not_to have_content("Email *")
end
end
describe "with dossier" do
let(:tag) { 'yolo' }
it 'should fill tags' do
get :index, params: { tags: [tag] }
expect(response.status).to eq(200)
expect(response.body).to include(tag)
end
end
end
context 'signed out' do
describe "with dossier" do
it 'should have email field' do
get :index
expect(response.status).to eq(200)
expect(response.body).to have_content("Email *")
end
end
describe "with dossier" do
let(:tag) { 'yolo' }
it 'should fill tags' do
get :index, params: { tags: [tag] }
expect(response.status).to eq(200)
expect(response.body).to include(tag)
end
end
end
end