From e6b8686e91cc55bcbc2fcb26e0be95b7e5841a76 Mon Sep 17 00:00:00 2001 From: reckyy Date: Tue, 21 Jan 2025 21:06:12 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=E6=9D=A1=E4=BB=B6=E3=81=AE=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E3=82=92=E6=AD=A3=E3=81=97=E3=81=8F=E3=81=97=E3=80=81?= =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E3=82=92=E8=A7=A3=E6=B6=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/javascript/answer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/javascript/answer.js b/app/javascript/answer.js index 6c30ff62ed9..23d61c10303 100644 --- a/app/javascript/answer.js +++ b/app/javascript/answer.js @@ -14,7 +14,7 @@ document.addEventListener('DOMContentLoaded', () => { const answers = document.querySelectorAll('.answer') const loadingContent = document.querySelector('.loading-content') const answerContent = document.querySelector('.answer-content') - if (answers) { + if (answers.length > 0) { loadingContent.style.display = 'none' answerContent.style.display = 'block' From 919097d73122789dbf0e9e777eb699f73ead97ab Mon Sep 17 00:00:00 2001 From: reckyy Date: Wed, 22 Jan 2025 21:56:09 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=E5=9B=9E=E7=AD=94=E3=81=8C=E3=81=AA?= =?UTF-8?q?=E3=81=84=E5=A0=B4=E5=90=88=E3=81=AB=E3=83=AD=E3=83=BC=E3=83=87?= =?UTF-8?q?=E3=82=A3=E3=83=B3=E3=82=B0=E3=81=8C=E8=A1=A8=E7=A4=BA=E3=81=95?= =?UTF-8?q?=E3=82=8C=E7=B6=9A=E3=81=91=E3=82=8B=E4=B8=8D=E5=85=B7=E5=90=88?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 同時に、不要なクラスも削除している --- app/javascript/answer.js | 5 ++++- app/views/questions/show.html.slim | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/javascript/answer.js b/app/javascript/answer.js index 23d61c10303..d5b7232e14c 100644 --- a/app/javascript/answer.js +++ b/app/javascript/answer.js @@ -14,10 +14,13 @@ document.addEventListener('DOMContentLoaded', () => { const answers = document.querySelectorAll('.answer') const loadingContent = document.querySelector('.loading-content') const answerContent = document.querySelector('.answer-content') - if (answers.length > 0) { + + if (loadingContent && answerContent) { loadingContent.style.display = 'none' answerContent.style.display = 'block' + } + if (answers.length > 0) { answers.forEach((answer) => { initializeAnswer(answer) }) diff --git a/app/views/questions/show.html.slim b/app/views/questions/show.html.slim index 43b1a695813..d21f193efde 100644 --- a/app/views/questions/show.html.slim +++ b/app/views/questions/show.html.slim @@ -35,7 +35,7 @@ hr.a-border .answer-content style='display: none;' - if @question.ai_answer = render 'ai_answer', question: @question - header.thread-comments__header.answer-content + header.thread-comments__header h2.thread-comments__title | 回答・コメント .answers-list From 954b600d550047471232fe8ac32f302fd540c06c Mon Sep 17 00:00:00 2001 From: reckyy Date: Mon, 27 Jan 2025 10:22:17 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=E5=9B=9E=E7=AD=94=E3=81=8C=E4=BD=9C?= =?UTF-8?q?=E6=88=90=E3=81=95=E3=82=8C=E3=81=9F=E3=81=AE=E3=82=92=E7=A2=BA?= =?UTF-8?q?=E8=AA=8D=E3=81=97=E3=81=A6=E3=81=8B=E3=82=89=E3=80=81=E7=94=BB?= =?UTF-8?q?=E9=9D=A2=E3=82=92=E6=9B=B4=E6=96=B0=E3=81=99=E3=82=8B=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/javascript/new-answer.js | 77 ++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 38 deletions(-) diff --git a/app/javascript/new-answer.js b/app/javascript/new-answer.js index 20dd9e5895b..532515f3280 100644 --- a/app/javascript/new-answer.js +++ b/app/javascript/new-answer.js @@ -33,20 +33,22 @@ document.addEventListener('DOMContentLoaded', () => { saveButton.disabled = editorTextarea.value.length === 0 }) - saveButton.addEventListener('click', () => { + saveButton.addEventListener('click', async () => { savedAnswer = editorTextarea.value - createAnswer(savedAnswer, questionId) - editorTextarea.value = '' - answerEditorPreview.innerHTML = markdownInitializer.render( - editorTextarea.value - ) - saveButton.disabled = true - updateAnswerCount(true) - updateWatchable(questionId) - if (previewTab.classList.contains('is-active')) { - toggleVisibility(tabElements, 'is-active') + const answerCreated = await createAnswer(savedAnswer, questionId) + if (answerCreated) { + editorTextarea.value = '' + answerEditorPreview.innerHTML = markdownInitializer.render( + editorTextarea.value + ) + saveButton.disabled = true + updateAnswerCount(true) + updateWatchable(questionId) + if (previewTab.classList.contains('is-active')) { + toggleVisibility(tabElements, 'is-active') + } + resizeTextarea(editorTextarea, defaultTextareaSize) } - resizeTextarea(editorTextarea, defaultTextareaSize) }) const editTab = answerEditor.querySelector('.edit-answer-tab') @@ -70,9 +72,9 @@ document.addEventListener('DOMContentLoaded', () => { } }) -function createAnswer(description, questionId) { +async function createAnswer(description, questionId) { if (description.length < 1) { - return null + return false } const params = { question_id: questionId, @@ -80,33 +82,32 @@ function createAnswer(description, questionId) { description: description } } - fetch('/api/answers', { - method: 'POST', - headers: { - 'Content-Type': 'application/json; charset=utf-8', - 'X-Requested-With': 'XMLHttpRequest', - 'X-CSRF-Token': CSRF.getToken() - }, - credentials: 'same-origin', - redirect: 'manual', - body: JSON.stringify(params) - }) - .then((response) => { - if (response.ok) { - return response.text() - } else { - return response.json().then((data) => { - throw new Error(data.errors.join(', ')) - }) - } + try { + const response = await fetch('/api/answers', { + method: 'POST', + headers: { + 'Content-Type': 'application/json; charset=utf-8', + 'X-Requested-With': 'XMLHttpRequest', + 'X-CSRF-Token': CSRF.getToken() + }, + credentials: 'same-origin', + redirect: 'manual', + body: JSON.stringify(params) }) - .then((html) => { + + if (response.ok) { + const html = await response.text() initializeNewAnswer(html) toast('回答を投稿しました!') - }) - .catch((error) => { - console.warn(error) - }) + return true + } else { + const data = await response.json() + throw new Error(data.errors.join(', ')) + } + } catch (error) { + console.warn(error) + return false + } } function toggleVisibility(elements, className) {