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
203
204
205
206
207
208
209
210
211
212
213
|
/*
* SonarQube
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import * as React from 'react';
import RemoveExtendedDescriptionModal from './RemoveExtendedDescriptionModal';
import { updateRule } from '../../../api/rules';
import MarkdownTips from '../../../components/common/MarkdownTips';
import { Button, ResetButtonLink } from '../../../components/ui/buttons';
import { translate, translateWithParameters } from '../../../helpers/l10n';
interface Props {
canWrite: boolean | undefined;
onChange: (newRuleDetails: T.RuleDetails) => void;
organization: string | undefined;
ruleDetails: T.RuleDetails;
}
interface State {
description: string;
descriptionForm: boolean;
removeDescriptionModal: boolean;
submitting: boolean;
}
export default class RuleDetailsDescription extends React.PureComponent<Props, State> {
mounted = false;
state: State = {
description: '',
descriptionForm: false,
submitting: false,
removeDescriptionModal: false
};
componentDidMount() {
this.mounted = true;
}
componentWillUnmount() {
this.mounted = false;
}
handleDescriptionChange = (event: React.SyntheticEvent<HTMLTextAreaElement>) =>
this.setState({ description: event.currentTarget.value });
handleCancelClick = () => {
this.setState({ descriptionForm: false });
};
handleSaveClick = () => {
this.updateDescription(this.state.description);
};
handleRemoveDescriptionClick = () => {
this.setState({ removeDescriptionModal: true });
};
handleCancelRemoving = () => this.setState({ removeDescriptionModal: false });
handleConfirmRemoving = () => {
this.setState({ removeDescriptionModal: false });
this.updateDescription('');
};
updateDescription = (text: string) => {
this.setState({ submitting: true });
updateRule({
key: this.props.ruleDetails.key,
markdown_note: text,
organization: this.props.organization
}).then(
ruleDetails => {
this.props.onChange(ruleDetails);
if (this.mounted) {
this.setState({ submitting: false, descriptionForm: false });
}
},
() => {
if (this.mounted) {
this.setState({ submitting: false });
}
}
);
};
handleExtendDescriptionClick = () => {
this.setState({
// set description` to the current `mdNote` each time the form is open
description: this.props.ruleDetails.mdNote || '',
descriptionForm: true
});
};
renderDescription = () => (
<div id="coding-rules-detail-description-extra">
{this.props.ruleDetails.htmlNote !== undefined && (
<div
className="rule-desc spacer-bottom markdown"
dangerouslySetInnerHTML={{ __html: this.props.ruleDetails.htmlNote }}
/>
)}
{this.props.canWrite && (
<Button
id="coding-rules-detail-extend-description"
onClick={this.handleExtendDescriptionClick}>
{translate('coding_rules.extend_description')}
</Button>
)}
</div>
);
renderForm = () => (
<div className="coding-rules-detail-extend-description-form">
<table className="width100">
<tbody>
<tr>
<td className="width100" colSpan={2}>
<textarea
autoFocus={true}
id="coding-rules-detail-extend-description-text"
onChange={this.handleDescriptionChange}
rows={4}
style={{ width: '100%', marginBottom: 4 }}
value={this.state.description}
/>
</td>
</tr>
<tr>
<td>
<Button
disabled={this.state.submitting}
id="coding-rules-detail-extend-description-submit"
onClick={this.handleSaveClick}>
{translate('save')}
</Button>
{this.props.ruleDetails.mdNote !== undefined && (
<>
<Button
className="button-red spacer-left"
disabled={this.state.submitting}
id="coding-rules-detail-extend-description-remove"
onClick={this.handleRemoveDescriptionClick}>
{translate('remove')}
</Button>
{this.state.removeDescriptionModal && (
<RemoveExtendedDescriptionModal
onCancel={this.handleCancelRemoving}
onSubmit={this.handleConfirmRemoving}
/>
)}
</>
)}
<ResetButtonLink
className="spacer-left"
disabled={this.state.submitting}
id="coding-rules-detail-extend-description-cancel"
onClick={this.handleCancelClick}>
{translate('cancel')}
</ResetButtonLink>
{this.state.submitting && <i className="spinner spacer-left" />}
</td>
<td className="text-right">
<MarkdownTips />
</td>
</tr>
</tbody>
</table>
</div>
);
render() {
const { ruleDetails } = this.props;
const hasDescription = !ruleDetails.isExternal || ruleDetails.type !== 'UNKNOWN';
return (
<div className="js-rule-description">
{hasDescription ? (
<div
className="coding-rules-detail-description rule-desc markdown"
dangerouslySetInnerHTML={{ __html: ruleDetails.htmlDesc || '' }}
/>
) : (
<div className="coding-rules-detail-description rule-desc markdown">
{translateWithParameters('issue.external_issue_description', ruleDetails.name)}
</div>
)}
{!ruleDetails.templateKey && (
<div className="coding-rules-detail-description coding-rules-detail-description-extra">
{!this.state.descriptionForm && this.renderDescription()}
{this.state.descriptionForm && this.props.canWrite && this.renderForm()}
</div>
)}
</div>
);
}
}
|