# frozen_string_literal: true class RegistrationsController < ApplicationController include Devise::Controllers::Helpers # layout 'cabinet' before_action :redirect_if_logined, except: [:success] before_action :build_customer, except: [:success] before_action :check_cookies, only: [:success] before_action :meta_structure, only: [:new] skip_before_action :verify_authenticity_token, only: [:create] respond_to :json, only: [:create] def new @registration_form.country_code ||= request_country_code respond_with @registration_form end def success @structure = structure_by(slug: 'additional-registration-success') @customer_content = @structure.try(:main) @translator_content = @structure.try(:footer) respond_with @structure end def create is_translator = params[:translator].present? if @registration_form.valid? && @registration_form.submit(is_translator) cookies[:registration_success] = { value: is_translator ? 'translator' : 'customer', expires: 1.day.from_now } sign_in :account, @registration_form.session if !is_translator && @registration_form.session end respond_with @registration_form, location: registration_success_path # respond_with(@registration_form, location: order_payment_provider_redirect_url(@registration_form)) end private def meta_structure @structure = structure_by(slug: 'registration-text') end def check_cookies if cookies[:registration_success].blank? if account_signed_in? redirect_to account_dashboard_path else redirect_to new_customer_path end end @is_customer_mode = (account_signed_in? && cookies[:registration_success] == 'customer') # if we need visit success page only once # cookies.delete :registration_success end def redirect_if_logined redirect_to account_dashboard_path if account_signed_in? end def build_customer @registration_form = RegistrationForm.new(params[:customer] || params[:translator]) end end