Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checkStampを非Vue化した #8194

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
61 changes: 44 additions & 17 deletions app/javascript/check-stamp.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,48 @@
import Vue from 'vue'
import CheckStamp from 'check-stamp.vue'
import store from 'check-store.js'
import CSRF from 'csrf'
import 'whatwg-fetch'

document.addEventListener('DOMContentLoaded', () => {
const checkStamp = document.getElementById('js-check-stamp')
if (checkStamp) {
const checkableId = checkStamp.getAttribute('data-checkable-id')
const checkableType = checkStamp.getAttribute('data-checkable-type')
new Vue({
store,
render: (h) =>
h(CheckStamp, {
props: {
checkableId: checkableId,
checkableType: checkableType
}
})
}).$mount('#js-check-stamp')
}
setCheckStamp()
})

export const setCheckStamp = () => {
const checkStamp = document.getElementById('js-check-stamp')
if (!checkStamp) return

const checkableType = checkStamp.getAttribute('data-checkable-type')
const checkableId = checkStamp.getAttribute('data-checkable-id')
store.dispatch('setCheckable', {
Copy link
Contributor Author

@mousu-a mousu-a Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この処理(store.jsのcheckIdのセット)が遅いと、commentsテスト(日報にコメントをする処理のあるテスト)でコメントをする時に、日報をチェック済みかどうか?の確認(store.jsのcheckIdのセット)がコメントをするタイミングに間に合わず、日報がチェック済みであるにも関わらずアラートが出てしまうことがあります。それによりテストが落ちやすくなっていたため、コードを上に移動させました。
日報を確認済みにしていませんがよろしいですか?
9a5cc64

checkableId: checkableId,
checkableType: checkableType
})
fetch(
`/api/checks.json/?checkable_type=${checkableType}&checkable_id=${checkableId}`,
{
method: 'GET',
headers: {
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-Token': CSRF.getToken()
},
credentials: 'same-origin'
}
)
.then((response) => {
return response.json()
})
.then((json) => {
const notChecked = !json[0]
const checkStampElement = document.querySelector('.stamp-approve')

checkStampElement.classList.toggle('is-hidden', notChecked)
if (notChecked) return

const checkedUserName = document.querySelector('.stamp__content-inner')
const checkedCreatedAt = document.querySelector('.is-created-at')
checkedUserName.textContent = json[0].user.login_name
checkedCreatedAt.textContent = json[0].created_at
})
.catch((error) => {
console.warn(error)
})
}
37 changes: 0 additions & 37 deletions app/javascript/check-stamp.vue

This file was deleted.

15 changes: 1 addition & 14 deletions app/javascript/check-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ Vue.use(Vuex)
export default new Vuex.Store({
state: {
checkId: null,
userName: null,
createdAt: null,
checkableId: null,
checkableType: null,
productId: null,
Expand All @@ -17,22 +15,15 @@ export default new Vuex.Store({
},
getters: {
checkId: (state) => state.checkId,
userName: (state) => state.userName,
createdAt: (state) => state.createdAt,
checkableId: (state) => state.checkableId,
checkableType: (state) => state.checkableType,
productId: (state) => state.productId,
productCheckerId: (state) => state.productCheckerId,
watchId: (state) => state.watchId
},
mutations: {
setCheckable(
state,
{ checkId, userName, createdAt, checkableId, checkableType }
) {
setCheckable(state, { checkId, checkableId, checkableType }) {
state.checkId = checkId
state.userName = userName
state.createdAt = createdAt
state.checkableId = checkableId
state.checkableType = checkableType
},
Expand Down Expand Up @@ -64,16 +55,12 @@ export default new Vuex.Store({
if (json[0]) {
commit('setCheckable', {
checkId: json[0].id,
createdAt: json[0].created_at,
userName: json[0].user.login_name,
checkableId: checkableId,
checkableType: checkableType
})
} else {
commit('setCheckable', {
checkId: null,
createdAt: null,
userName: null,
checkableId: checkableId,
checkableType: checkableType
})
Expand Down
3 changes: 3 additions & 0 deletions app/javascript/checkable.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { setCheckStamp } from 'check-stamp.js'

export default {
computed: {
isUnassignedAndUnchekedProduct() {
Expand Down Expand Up @@ -41,6 +43,7 @@ export default {
if (json.message) {
this.toast(json.message, 'error')
} else {
setCheckStamp()
if (!this.checkId) {
if (checkableType === 'Product') {
this.toast('提出物を確認済みにしました。')
Expand Down
6 changes: 6 additions & 0 deletions app/views/checks/_check-stamp.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.stamp.stamp-approve.is-hidden
h2.stamp__content.is-title
| 確認済
time.stamp__content.is-created-at
.stamp__content.is-user-name
.stamp__content-inner
Copy link
Contributor

@Ryooo-k Ryooo-k Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

現状のコードから生成されるHTMLは、

<div class="stamp stamp-approve show">
  <h2 class="stamp__content is-title">確認済</h2>
  <time class="stamp__content is-created-at">2024/11/20</time>
  <div class="stamp__content is-user-name">komagata</div>
</div>

となりますが変更前のファイルを見る限り、下記のHTMLを生成するコードが正しいのではないかと思いました。

<div class="stamp stamp-approve show">
  <h2 class="stamp__content is-title">確認済</h2>
  <time class="stamp__content is-created-at">2024/11/20</time>
  <div class="stamp__content is-user-name">
    <div class="stamp__content-inner">komagata</div>
  </div>
</div>

現状だとユーザー名が下よりになっています。

スクリーンショット 2024-11-19 16 33 24

変更すると中央に揃います。

スクリーンショット 2024-11-19 16 33 06

3 changes: 2 additions & 1 deletion app/views/products/_product_header.html.slim
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
header.page-content-header
#js-check-stamp(data-checkable-id="#{product.id}" data-checkable-type='Product')
#js-check-stamp(data-checkable-type='Product' data-checkable-id="#{product.id}")
= render 'checks/check-stamp'
.page-content-header__start
.page-content-header__user
= render 'users/icon', user: product.user, link_class: 'page-content-header__user-link', image_class: 'page-content-header__user-icon'
Expand Down
3 changes: 2 additions & 1 deletion app/views/reports/_report_header.html.slim
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
header.page-content-header
#js-check-stamp(data-checkable-id="#{report.id}" data-checkable-type='Report')
#js-check-stamp(data-checkable-type='Report' data-checkable-id="#{report.id}")
= render 'checks/check-stamp'
.page-content-header__start
.page-content-header__user
= render 'users/icon', user: report.user, link_class: 'page-content-header__user-link', image_class: 'page-content-header__user-icon'
Expand Down