-
Notifications
You must be signed in to change notification settings - Fork 51
/
index.js
141 lines (138 loc) · 4.5 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
// @ts-nocheck
import React from 'react'
import { Component } from 'react'
import * as PropTypes from 'prop-types'
import { connect } from 'react-redux'
import RejectTranslationModal from '../../components/RejectTranslationModal'
import { rejectTranslation } from '../../actions/review-trans-actions'
import update from 'immutability-helper'
import { isUndefined, isEmpty } from 'lodash'
import {
MINOR, MAJOR, CRITICAL, UNSPECIFIED, priorities
} from '../../utils/reject-trans-util'
export const commentTextLimit = 500
/**
* Modal to collect feedback on the reason for rejecting a translation.
*/
export class RejectTranslation 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,
commentRequired: 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: '',
criteriaId: undefined,
reviewComment: ''
},
charsLeft: commentTextLimit,
selectedCriteria: UNSPECIFIED
}
constructor (props) {
super(props)
this.state = this.defaultState
}
onPriorityChange = (selectedPriority) => {
const priorityIdIndex = priorities.indexOf(selectedPriority)
this.setState(prevState => ({
review: update(prevState.review, {
selectedPriority: {$set: selectedPriority},
priorityId: {$set: priorityIdIndex}
})
}))
}
onCriteriaChange = (selectedCriteria) => {
this.setState(prevState => ({
review: update(prevState.review, {
criteriaDescription: {$set: selectedCriteria.description},
criteriaId: {$set: selectedCriteria.id},
selectedPriority: {$set: selectedCriteria.priority}
}),
selectedCriteria: selectedCriteria
}))
}
onUnspecifiedCriteria = () => {
this.setState(prevState => ({
review: update(prevState.review, {
criteriaDescription: {$set: UNSPECIFIED.description},
criteriaId: {$set: undefined},
selectedPriority: {$set: UNSPECIFIED.priority}
}),
selectedCriteria: UNSPECIFIED
}))
}
setReviewComment = (event) => {
const reviewComment = event.target.value
const charCount = event.target.value.length
const charLeft = commentTextLimit - 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, charsLeft } = this.state
return (
<RejectTranslationModal
show={show}
onHide={this.onHideResetState}
onHideResetState={this.onHideResetState}
onCriteriaChange={this.onCriteriaChange}
onUnspecifiedCriteria={this.onUnspecifiedCriteria}
onPriorityChange={this.onPriorityChange}
textLimit={commentTextLimit}
charsLeft={charsLeft}
criteriaList={criteriaList}
saveTransReview={this.saveTransReview}
setReviewComment={this.setReviewComment}
selectedCriteria={selectedCriteria}
review={review} />
)
}
}
/* eslint-enable max-len */
const mapDispatchToProps = dispatch => {
return {
addNewTransReview: (review) => dispatch(rejectTranslation(dispatch, review)
)
}
}
export default connect(null, mapDispatchToProps)(RejectTranslation)