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

sadがついた日報にコメントを書いて『確認OKにする』ボタンを押した際に『今日の気分は「sad」ですが、コメント無しで確認しますか?』が表示されないようにした #4239

Merged
merged 7 commits into from
Feb 26, 2022
Merged
45 changes: 16 additions & 29 deletions app/javascript/check.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
<script>
import 'whatwg-fetch'
import ProductChecker from './product_checker'
import checkable from './checkable.js'

export default {
components: {
'product-checker': ProductChecker
},
mixins: [checkable],
props: {
checkableId: { type: Number, required: true },
checkableType: { type: String, required: true },
Expand Down Expand Up @@ -76,44 +78,29 @@ export default {
const meta = document.querySelector('meta[name="csrf-token"]')
return meta ? meta.getAttribute('content') : ''
},
check() {
const params = {
checkable_type: this.checkableType,
checkable_id: this.checkableId
}

fetch(this.url, {
method: this.method,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-Token': this.token()
},
credentials: 'same-origin',
redirect: 'manual',
body: JSON.stringify(params)
})
.then(() => {
this.$store.dispatch('setCheckable', {
checkableId: this.checkableId,
checkableType: this.checkableType
})
})
.catch((error) => {
console.warn(error)
})
},
checkSad() {
if (this.checkHasSadEmotion && !this.checkHasComment && !this.checkId) {
if (
window.confirm(
'今日の気分は「sad」ですが、コメント無しで確認しますか?'
)
) {
this.check()
this.check(
this.checkableType,
this.checkableId,
this.url,
this.method,
this.token()
)
}
} else {
this.check()
this.check(
this.checkableType,
this.checkableId,
this.url,
this.method,
this.token()
)
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions app/javascript/checkable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export default {
methods: {
check(checkableType, checkableId, url, method, token) {
const params = {
checkable_type: checkableType,
checkable_id: checkableId
}

fetch(url, {
method: method,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-Token': token
},
credentials: 'same-origin',
redirect: 'manual',
body: JSON.stringify(params)
})
.then(() => {
this.$store.dispatch('setCheckable', {
checkableId: checkableId,
checkableType: checkableType
})
})
.catch((error) => {
console.warn(error)
})
}
}
}
15 changes: 11 additions & 4 deletions app/javascript/comments.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ import CommentPlaceholder from './comment-placeholder'
import confirmUnload from './confirm-unload'
import toast from './toast'
import role from './role'
import checkable from './checkable.js'

export default {
components: {
comment: Comment,
commentPlaceholder: CommentPlaceholder
},
mixins: [toast, confirmUnload, role],
mixins: [toast, confirmUnload, role, checkable],
props: {
commentableId: { type: String, required: true },
commentableType: { type: String, required: true },
Expand Down Expand Up @@ -257,10 +258,16 @@ export default {
!window.confirm('提出物を確認済にしてよろしいですか?')
) {
return null
} else {
this.createComment()
this.check(
this.commentableType,
this.commentableId,
'/api/checks',
'POST',
this.token()
)
}
const check = document.getElementById('js-shortcut-check')
this.createComment()
check.click()
Copy link
Member

Choose a reason for hiding this comment

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

これってなぜこの方法から、自前でPOSTする方法に変更したんでしょう?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

「確認OKにする」ボタンには、「日報を確認」する機能が必要で、ポップアップは必要ない。
「日報を確認」ボタンには、「日報を確認」する機能と、ポップアップの両方必要。

「日報を確認」するというメソッド(機能)はcheck.vueに存在し、comments.vueからは呼べないと思いました。
なぜなら、親子関係にないためです。
なので、「日報を確認」する機能に該当する部分(変更後コードのcomments.vue L138~L164)をcheck.vueから移植して自前でPOSTするようにしました。

自分では思いつかなかったのですが、comments.vueからcheck.vuecheckメソッドを呼ぶ方法がございましたら、実装のヒントや方針などご教示いただけますと幸いです。

Copy link
Member

Choose a reason for hiding this comment

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

@Aseiide なるほどですね〜

Checkする処理は同じだと思うのでcomponentからmixinを使って共通化するのはいかがでしょうか〜

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@komagata
なるほど、そういう方法があるのですね。
mixin調べてやってみます。

},
async fetchUncheckedProducts(page) {
return fetch(`/api/products/unchecked?page=${page}`, {
Expand Down
5 changes: 1 addition & 4 deletions test/system/api/check/reports_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ class Check::ReportsTest < ApplicationSystemTestCase
test 'comment and check report' do
visit_with_auth "/reports/#{reports(:report20).id}", 'komagata'
fill_in 'new_comment[description]', with: '日報でcomment+確認OKにするtest'
accept_alert do
wait_for_vuejs
click_button '確認OKにする'
end
click_button '確認OKにする'
assert_text '確認済'
assert_text '日報でcomment+確認OKにするtest'
end
Expand Down