-
Notifications
You must be signed in to change notification settings - Fork 51
/
index.js
202 lines (199 loc) · 6.6 KB
/
index.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// @ts-nocheck
import React from 'react'
import { Component } from 'react'
import * as PropTypes from 'prop-types'
import { connect } from 'react-redux'
import Button from '../../components/Button'
import { Row } from 'react-bootstrap'
import { Modal } from '../../../components'
import PriorityDropdown from './PriorityDropdown'
import CriteriaDropdown from './CriteriaDropdown'
import { rejectTranslation } from '../../actions/review-trans-actions'
import update from 'immutability-helper'
import { isUndefined, isEmpty } from 'lodash'
import {
MINOR, MAJOR, CRITICAL, priorities, textState
} from '../../utils/reject-trans-util'
export const UNSPECIFIED = 'Unspecified Criteria'
const textLimit = 500
/**
* Modal to collect feedback on the reason for rejecting a translation.
*/
export class RejectTranslationModal extends Component {
static propTypes = {
show: PropTypes.bool.isRequired,
onHide: PropTypes.func.isRequired,
transUnitID: PropTypes.number.isRequired,
// Initial flyweight fetch of phrases does not include the revision detail
revision: PropTypes.number,
localeId: PropTypes.string.isRequired,
criteriaList: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.number.isRequired,
editable: PropTypes.bool.isRequired,
description: PropTypes.string.isRequired,
priority: PropTypes.oneOf([MINOR, MAJOR, CRITICAL]).isRequired
})).isRequired,
addNewTransReview: PropTypes.func.isRequired,
selectedPhrase: PropTypes.object
}
defaultState = {
review: {
selectedPriority: MINOR,
priorityId: 0,
criteriaDescription: '-- Select a predefined criteria --',
criteriaId: undefined,
reviewComment: ''
},
charsLeft: textLimit,
selectedCriteria: {
id: undefined,
editable: false,
description: '',
priority: MINOR
}
}
constructor (props) {
super(props)
this.state = this.defaultState
}
onPriorityChange = (event) => {
const selectedPriority = event.target.innerText
const priorityIdIndex = priorities.indexOf(event.target.innerText)
this.setState(prevState => ({
review: update(prevState.review, {
selectedPriority: {$set: selectedPriority},
priorityId: {$set: priorityIdIndex}
})
}))
}
onCriteriaChange = (event) => {
const selectedCriteria = this.props.criteriaList.find(
x => x.description === event.target.innerText)
this.setState(prevState => ({
review: update(prevState.review, {
criteriaDescription: {$set: selectedCriteria.description},
criteriaId: {$set: selectedCriteria.id},
selectedPriority: {$set: selectedCriteria.priority}
}),
selectedCriteria: selectedCriteria
}))
}
onUnspecifiedCriteria = () => {
const unspecifiedCriteria = this.defaultState.selectedCriteria
this.setState(prevState => ({
review: update(prevState.review, {
criteriaDescription: {$set: UNSPECIFIED},
criteriaId: {$set: undefined},
selectedPriority: {$set: unspecifiedCriteria.priority}
}),
selectedCriteria: unspecifiedCriteria
}))
}
setReviewComment = (event) => {
const reviewComment = event.target.value
const charCount = event.target.value.length
const charLeft = textLimit - charCount
this.setState(prevState => ({
review: update(prevState.review, {
reviewComment: {$set: reviewComment}
}),
charsLeft: charLeft
}))
}
saveTransReview = () => {
const review = {
localeId: this.props.localeId,
transUnitId: this.props.transUnitID,
revision: this.props.revision,
criteriaId: this.state.review.criteriaId,
reviewComment: this.state.review.reviewComment,
phrase: this.props.selectedPhrase
}
if (!isUndefined(review.criteriaId) || !isEmpty(review.reviewComment)) {
this.props.addNewTransReview(review)
this.onHideResetState()
} else {
console.error('Must either select a criteria or provide a comment.')
}
}
onHideResetState = () => {
this.setState(this.defaultState)
this.props.onHide()
}
/* eslint-disable max-len */
render () {
const { show, criteriaList } = this.props
const { review, selectedCriteria } = this.state
const priorityTextState = textState(review.selectedPriority)
const criteriaTile = (!isEmpty(criteriaList))
? <div className='flex'>
<span id='CriteriaTitle'>
Criteria
</span>
<CriteriaDropdown
criteriaList={criteriaList}
onCriteriaChange={this.onCriteriaChange}
onUnspecifiedCriteria={this.onUnspecifiedCriteria}
criteriaDescription={review.criteriaDescription} />
<PriorityDropdown
textState={priorityTextState}
priority={review.selectedPriority}
priorityChange={this.onPriorityChange} />
</div>
: undefined
const canReject = (
(!isEmpty(review.reviewComment)) ||
(selectedCriteria.editable === false))
return (
<Modal show={show}
onHide={this.onHideResetState}
key='reject-translation-modal'
id='RejectTranslationModal'>
<Modal.Header>
<Modal.Title>Reject translation</Modal.Title>
</Modal.Header>
<Modal.Body>
{criteriaTile}
<div className='EditorRejection-input'>
<textarea ref='input'
type='comment'
placeholder='Provide a comment for why this translation has been rejected'
cols='50'
onChange={this.setReviewComment}
rows='10'
maxLength={textLimit}
className='EditorInputGroup-input is-focused InputGroup--outlined
Commenting' />
<p>{this.state.charsLeft}</p>
</div>
</Modal.Body>
<Modal.Footer>
<span>
<Row>
<Button
className='EditorButton Button--large u-rounded
Button--secondary'
onClick={this.onHideResetState}>
Cancel
</Button>
<Button
className='EditorButton Button--large u-rounded Button--primary'
onClick={this.saveTransReview}
disabled={!canReject}>
Reject translation
</Button>
</Row>
</span>
</Modal.Footer>
</Modal>
)
}
}
/* eslint-enable max-len */
const mapDispatchToProps = dispatch => {
return {
addNewTransReview: (review) => dispatch(rejectTranslation(dispatch, review)
)
}
}
export default connect(null, mapDispatchToProps)(RejectTranslationModal)