-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathcomment-checkable.js
More file actions
107 lines (94 loc) · 2.62 KB
/
comment-checkable.js
File metadata and controls
107 lines (94 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { toast } from './vanillaToast.js'
import checkStamp from 'check-stamp.js'
import { FetchRequest } from '@rails/request.js'
async function sendRequest(url, method = 'get', body = null) {
const options = { redirect: 'manual' }
if (body) {
options.body = body
}
const request = new FetchRequest(method, url, options)
const response = await request.perform()
let data
if (response.ok) {
try {
data = await response.json
} catch {
data = response
}
} else {
try {
data = await response.json
} catch {
throw new Error(`HTTP error! status: ${response.status}`)
}
}
return data
}
export default {
async isUnassignedAndUncheckedProduct(checkableType, checkableId, isMentor) {
if (!isMentor) return
if (checkableType === 'Product') {
const hasCheckerResult = await this.hasChecker(checkableId)
const isCheckedResult = await this.isChecked(checkableType, checkableId)
return hasCheckerResult === false && isCheckedResult === false
}
},
async check(checkableType, checkableId, url, method) {
const params = {
checkable_type: checkableType,
checkable_id: checkableId
}
try {
const data = await sendRequest(url, method, params)
if (data.message) {
toast(data.message, 'error')
} else {
checkStamp()
const message =
checkableType === 'Product'
? '提出物を合格にしました。'
: '日報を確認済みにしました。'
sessionStorage.setItem('showToast', message)
}
} catch (error) {
console.warn(error)
}
},
async assignChecker(productId, currentUserId) {
const params = {
product_id: productId,
current_user_id: currentUserId
}
try {
const json = await sendRequest('/api/products/checker', 'patch', params)
if (json.message) {
alert(json.message)
} else {
alert('提出物の担当になりました。')
sessionStorage.setItem('showToast', '担当になりました。')
}
} catch (error) {
console.warn(error)
}
},
async isChecked(checkableType, checkableId) {
try {
const json = await sendRequest(
`/api/checks.json/?checkable_type=${checkableType}&checkable_id=${checkableId}`
)
return !!json[0]
} catch (error) {
console.warn(error)
}
},
async hasChecker(productId) {
try {
const json = await sendRequest(
`/api/products/checker.json?product_id=${productId}`
)
return !!json.checker_name
} catch (error) {
console.warn(error)
}
}
}