forked from inveniosoftware/react-invenio-forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeedbackLabel.js
66 lines (56 loc) · 1.73 KB
/
FeedbackLabel.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
import React, { Component } from "react";
import { Label, Icon } from "semantic-ui-react";
import { flattenAndCategorizeErrors } from "../utils";
import { InvenioPopup } from "../elements/accessibility";
import PropTypes from "prop-types";
export class FeedbackLabel extends Component {
render() {
const { errorMessage } = this.props;
if (!errorMessage) return null;
const { flattenedErrors, severityChecks } =
flattenAndCategorizeErrors(errorMessage);
let errorText = "";
let severityLevel = "";
let severityMessage = "";
if (flattenedErrors) {
const errorKey = Object.keys(flattenedErrors)[0];
errorText = flattenedErrors[errorKey];
}
if (severityChecks) {
const severityKey = Object.keys(severityChecks)[0];
const severityData = severityChecks[severityKey];
if (severityData) {
severityLevel = severityData.severity;
severityMessage = severityData.message;
}
}
if (errorMessage.message && errorMessage.severity) {
severityLevel = errorMessage.severity;
severityMessage = errorMessage.message;
}
const prompt = !severityLevel && !!errorText;
return (
<Label pointing="left" className={severityLevel} prompt={prompt}>
{severityMessage && (
<InvenioPopup
trigger={<Icon name="info circle" />}
content={
<a target="_blank" href="_">
{severityMessage}
</a>
}
position="top center"
hoverable
/>
)}
{errorText || severityMessage}
</Label>
);
}
}
FeedbackLabel.propTypes = {
errorMessage: PropTypes.array,
};
FeedbackLabel.defaultProps = {
errorMessage: undefined,
};