# frozen_string_literal: true module Account class FeedbacksController < Account::BaseController respond_to :json, only: [:create] respond_to :js, only: [:ranking] skip_before_action :verify_authenticity_token, only: [:create] prepend_before_action :perpage!, only: [:index, :show] prepend_before_action :group_by_params prepend_before_action :find_preview_structure, only: :show before_action :load_records, only: :index before_action :build_form, only: :create append_before_action :redirect_to_last_page_if_empty, only: :index skip_before_action :authenticate_account!, only: :check def index find_structure('feedbacks-text') respond_with @records end def create if !@violation || (@violation.valid? && @violation.submit) @form.submit if @form.valid? end result = !@violation || @violation.errors.blank? ? @form : @violation respond_with(result, location: account_feedbacks_path) end def orders @orders = TranslationCms::Api::Customers::Feedbacks::Writer.find(params[:id]) respond_to do |format| format.json { render } format.all { redirect_to account_dashboard_path } end end def check respond_to do |format| begin api_responce = TranslationCms::Api::Customers::Writer.find(params[:id]) if current_account format.json { render json: current_account.present? && api_responce.present? } rescue JsonApiClient::Errors::NotFound format.json { render json: false } end format.all { redirect_to account_dashboard_path } end end def show api_responce = TranslationCms::Api::Customers::Writer.find(params[:id]) raise ActiveRecord::RecordNotFound, "Writer #{params[:id]} not found" if api_responce.errors.present? @writer = api_responce.first @feedbacks = @writer.writer_feedbacks(skip_user_id: @account.profile.id) @industries = TranslationCms::Api::IndustryExpertise.all! @orders = TranslationCms::Api::Customers::Feedbacks::Writer.find(params[:id]) # Seo meta tags @meta_record = @writer respond_with @writer end private def find_preview_structure # load_structure(StructureType.writers) unless feature_enabled?(:writer_preview) raise ActiveRecord::RecordNotFound, "Writer #{params[:id]} not found" end end def redirect_to_last_page_if_empty if params.fetch(:page, 1) > 1 && @records.pages.total_pages < @records.current_page redirect_to url_for(params.merge(page: @records.pages.total_pages)) end end def load_records @records = case @group when FeedbacksPageType.top_writers TranslationCms::Api::Customers::Feedbacks::Writer.where(tops: true) when FeedbacksPageType.pending TranslationCms::Api::Customers::Feedbacks::Order else TranslationCms::Api::Customers::Feedbacks::Writer end.page(params[:page]).per(params[:per]).all end def build_form @form = Account::FeedbackForm.new(params[:feedback]) if @form.valid? && params[:violation].present? @violation = Account::ViolationForm.new(params[:violation]) end end def group_by_params @group = FeedbacksPageType.find_by(slug: params[:page_type]) end def feedsback_params params.require(:feedback).permit( :writer_id, :order_id, :id, :content, :rate, violation_attributes: [:content, :is_revise, :deadline] ) end end end