Skip to content

Commit

Permalink
Merge pull request #7470 from fjordllc/feature/convert-user-mentor-me…
Browse files Browse the repository at this point in the history
…mo-vue-to-slim

user_mentor_memo.vueを削除し、htmlとjsでの実装に変更
  • Loading branch information
komagata authored Apr 24, 2024
2 parents 3e8699a + 975b29d commit 206f5c8
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 180 deletions.
174 changes: 0 additions & 174 deletions app/javascript/components/user_mentor_memo.vue

This file was deleted.

3 changes: 1 addition & 2 deletions app/javascript/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import '../register-address.js'
import '../upload-image-to-article.js'
import '../header-dropdown.js'
import '../editor-selection-form.js'
import '../user_mentor_memo.js'

import VueMounter from '../VueMounter.js'
import Announcements from '../components/announcements.vue'
Expand All @@ -66,7 +67,6 @@ import UsersAnswers from '../components/users-answers.vue'
import User from '../components/user.vue'
import Watches from '../components/watches.vue'
import WatchToggle from '../components/watch-toggle.vue'
import UserMentorMemo from '../components/user_mentor_memo.vue'
import UserRecentReports from '../components/user-recent-reports.vue'
import Footprints from '../components/footprints.vue'
import QuestionAnswers from '../components/question-answers.vue'
Expand All @@ -87,7 +87,6 @@ mounter.addComponent(UsersAnswers)
mounter.addComponent(User)
mounter.addComponent(Watches)
mounter.addComponent(WatchToggle)
mounter.addComponent(UserMentorMemo)
mounter.addComponent(UserRecentReports)
mounter.addComponent(Footprints)
mounter.addComponent(QuestionAnswers)
Expand Down
144 changes: 144 additions & 0 deletions app/javascript/user_mentor_memo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import CSRF from 'csrf'
import TextareaInitializer from 'textarea-initializer'
import MarkdownInitializer from 'markdown-initializer'

document.addEventListener('DOMContentLoaded', () => {
const mentorMemo = document.querySelector('.user-mentor-memo')
if (mentorMemo) {
TextareaInitializer.initialize('#js-user-mentor-memo')
const markdownInitializer = new MarkdownInitializer()
const userId = mentorMemo.dataset.user_id
let savedMemo = ''

const memoDisplay = mentorMemo.querySelector('.memo-display')
const memoEditor = mentorMemo.querySelector('.memo-editor')

const placeholder = memoDisplay.querySelector('.a-placeholder')
const emptyMessage = memoDisplay.querySelector('.o-empty-message')
const memoDisplayContent = memoDisplay.querySelector(
'.user-mentor-memo-content'
)
const memoEditorPreview = memoEditor.querySelector(
'.a-markdown-input__preview'
)
const editorTextarea = memoEditor.querySelector(
'.a-markdown-input__textarea'
)

fetch(`/api/users/${userId}.json`, {
method: 'GET',
headers: {
'X-Requested-With': 'XMLHttpRequest'
},
credentials: 'same-origin',
redirect: 'manual'
})
.then((response) => {
return response.json()
})
.then((json) => {
if (json.mentor_memo) {
savedMemo = json.mentor_memo
}
placeholder.classList.add('is-hidden')
if (savedMemo.length === 0) {
emptyMessage.classList.remove('is-hidden')
} else {
memoDisplayContent.classList.remove('is-hidden')
editorTextarea.value = savedMemo
switchMemoDisplay(memoDisplay, savedMemo)
memoDisplayContent.innerHTML = markdownInitializer.render(savedMemo)
memoEditorPreview.innerHTML = markdownInitializer.render(savedMemo)
}
})
.catch((error) => {
console.warn(error)
})

const editButton = memoDisplay.querySelector('.card-footer-actions__action')
const modalElements = [memoDisplay, memoEditor]
editButton.addEventListener('click', () =>
toggleClass(modalElements, 'is-hidden')
)

const saveButton = memoEditor.querySelector('.is-primary')
saveButton.addEventListener('click', () => {
toggleClass(modalElements, 'is-hidden')
savedMemo = editorTextarea.value
updateMemo(savedMemo, userId)
memoDisplayContent.innerHTML = markdownInitializer.render(savedMemo)
switchMemoDisplay(memoDisplay, savedMemo)
})

const cancelButton = memoEditor.querySelector('.is-secondary')
cancelButton.addEventListener('click', () => {
toggleClass(modalElements, 'is-hidden')
editorTextarea.value = savedMemo
memoEditorPreview.innerHTML = markdownInitializer.render(savedMemo)
})

editorTextarea.addEventListener('change', () => {
memoEditorPreview.innerHTML = markdownInitializer.render(
editorTextarea.value
)
})

const editorTab = memoEditor.querySelector('.editor-tab')
const editorTabContent = memoEditor.querySelector('.is-editor')
const previewTab = memoEditor.querySelector('.preview-tab')
const previewTabContent = memoEditor.querySelector('.is-preview')

const tabElements = [
editorTab,
editorTabContent,
previewTab,
previewTabContent
]
editorTab.addEventListener('click', () =>
toggleClass(tabElements, 'is-active')
)
previewTab.addEventListener('click', () =>
toggleClass(tabElements, 'is-active')
)
}
})

function updateMemo(memo, userId) {
const params = {
user: {
mentor_memo: memo
}
}
fetch(`/api/mentor_memos/${userId}`, {
method: 'PUT',
headers: {
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/json; charset=utf-8',
'X-CSRF-Token': CSRF.getToken()
},
credentials: 'same-origin',
redirect: 'manual',
body: JSON.stringify(params)
})
.then((response) => {
return response
})
.catch((error) => {
console.warn(error)
})
}

function switchMemoDisplay(memoDisplay, memo) {
const memoDisplayContent = memoDisplay.querySelector(
'.user-mentor-memo-content'
)
const emptyMessage = memoDisplay.querySelector('.o-empty-message')
memoDisplayContent.classList.toggle('is-hidden', memo.length === 0)
emptyMessage.classList.toggle('is-hidden', memo.length !== 0)
}

function toggleClass(elements, className) {
elements.forEach((element) => {
element.classList.toggle(className)
})
}
2 changes: 1 addition & 1 deletion app/views/products/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ header.page-header
.user-info
= render 'users/user_secret_attributes', user: @product.user
= render 'users/metas', user: @product.user
div(data-vue="UserMentorMemo" data-vue-user-id:number="#{@product.user.id}" data-vue-products-mode:boolean="#{true}")
= render 'users/user_mentor_memo', user_id: @product.user.id
.side-tabs-contents__item#side-tabs-content-4
div(data-vue="UserProducts" data-vue-user-id:number="#{@product.user.id}" data-vue-is-mentor:boolean="#{false}" data-vue-current-user-id:number="#{current_user.id}" data-vue-display-user-icon:boolean="false")

Expand Down
2 changes: 1 addition & 1 deletion app/views/reports/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
.user-info
= render 'users/user_secret_attributes', user: @report.user
= render 'users/metas', user: @report.user
div(data-vue="UserMentorMemo" data-vue-user-id:number="#{@report.user_id}")
= render 'users/user_mentor_memo', user_id: @report.user_id
.side-tabs-contents__item#side-tabs-content-3.is-only-mentor
.card-list.a-card
- if @products.present?
Expand Down
2 changes: 1 addition & 1 deletion app/views/talks/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,6 @@
method: :patch,
data: { confirm: '本当によろしいですか?' },
class: 'a-button is-sm is-danger is-block'
div(data-vue="UserMentorMemo" data-vue-user-id:number="#{@user.id}" data-vue-products-mode:boolean="#{true}")
= render 'users/user_mentor_memo', user_id: @user.id
.side-tabs-contents__item#side-tabs-content-2
= react_component('Reports', userId: @user.id, displayUserIcon: false, displayPagination: false)
Loading

0 comments on commit 206f5c8

Please sign in to comment.