Remove AJAX question generation code

This commit is contained in:
Andreas Nedbal 2022-11-17 21:56:23 +01:00 committed by Andreas Nedbal
parent d3eae22f13
commit 44c0136c9e
5 changed files with 0 additions and 85 deletions

View File

@ -1,25 +1,4 @@
class Ajax::InboxController < AjaxController
def create
unless user_signed_in?
@response[:status] = :noauth
@response[:message] = t(".noauth")
return
end
question = Question.create!(content: QuestionGenerator.generate,
author_is_anonymous: true,
author_identifier: "justask",
user: current_user)
inbox = Inbox.create!(user: current_user, question_id: question.id, new: true)
@response[:status] = :okay
@response[:message] = t(".success")
@response[:success] = true
@response[:render] = render_to_string(partial: 'inbox/entry', locals: { i: inbox })
inbox.update(new: false)
end
def remove
params.require :id

View File

@ -1,21 +0,0 @@
import { post } from '@rails/request.js';
import I18n from 'retrospring/i18n';
import { updateDeleteButton } from './delete';
import { showErrorNotification } from 'utilities/notifications';
export function generateQuestionHandler(): void {
post('/ajax/generate_question')
.then(async response => {
const data = await response.json;
if (!data.success) return false;
document.querySelector('#entries').insertAdjacentHTML('afterbegin', data.render);
updateDeleteButton();
})
.catch(err => {
console.log(err);
showErrorNotification(I18n.translate('frontend.error.message'));
});
}

View File

@ -2,11 +2,9 @@ import registerEvents from 'utilities/registerEvents';
import registerInboxEntryEvents from './entry';
import { authorSearchHandler } from './author';
import { deleteAllAuthorQuestionsHandler, deleteAllQuestionsHandler } from './delete';
import { generateQuestionHandler } from './generate';
export default (): void => {
registerEvents([
{ type: 'click', target: '#ib-generate-question', handler: generateQuestionHandler, global: true },
{ type: 'click', target: '#ib-delete-all', handler: deleteAllQuestionsHandler, global: true },
{ type: 'click', target: '#ib-delete-all-author', handler: deleteAllAuthorQuestionsHandler, global: true },
{ type: 'submit', target: '#author-form', handler: authorSearchHandler, global: true }

View File

@ -110,7 +110,6 @@ Rails.application.routes.draw do
namespace :ajax do
post "/ask", to: "question#create", as: :ask
post "/destroy_question", to: "question#destroy", as: :destroy_question
post "/generate_question", to: "inbox#create", as: :generate_question
post "/delete_inbox", to: "inbox#remove", as: :delete_inbox
post "/delete_all_inbox", to: "inbox#remove_all", as: :delete_all_inbox
post "/delete_all_inbox/:author", to: "inbox#remove_all_author", as: :delete_all_author

View File

@ -4,46 +4,6 @@
require "rails_helper"
describe Ajax::InboxController, :ajax_controller, type: :controller do
describe "#create" do
subject { post(:create) }
context "when user is signed in" do
before(:each) { sign_in(user) }
let(:expected_response) do
{
"success" => true,
"status" => "okay",
"message" => anything,
"render" => anything
}
end
it "creates a generated question to the user's inbox" do
allow(QuestionGenerator).to receive(:generate).and_return("Is Mayonnaise an instrument?")
expect { subject }.to(change { user.inboxes.count }.by(1))
expect(user.inboxes.last.question.author_is_anonymous).to eq(true)
expect(user.inboxes.last.question.author_identifier).to eq("justask")
expect(user.inboxes.last.question.user).to eq(user)
expect(user.inboxes.last.question.content).to eq("Is Mayonnaise an instrument?")
end
include_examples "returns the expected response"
end
context "when user is not signed in" do
let(:expected_response) do
{
"success" => false,
"status" => "noauth",
"message" => anything
}
end
include_examples "returns the expected response"
end
end
describe "#remove" do
let(:params) do
{