CustomMessages.jsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. const i18next = require("i18next");
  4. const t = i18next.t;
  5. i18next.loadNamespaces("customMessages");
  6. export default class CustomMessages extends Component {
  7. static propTypes = {
  8. onRef: PropTypes.func,
  9. };
  10. static defaultProps = {
  11. onRef: () => {},
  12. };
  13. constructor() {
  14. super();
  15. this.state = {
  16. error: [],
  17. info: [],
  18. success: [],
  19. };
  20. }
  21. componentDidMount() {
  22. this.props.onRef(this);
  23. }
  24. componentWillUnmount() {
  25. this.props.onRef(null);
  26. }
  27. clearError = (cb = () => {}) => {
  28. this.clear("error", cb);
  29. };
  30. addError = (error) => {
  31. this.add("error", error);
  32. };
  33. clearAddError = (error) => {
  34. this.clearAdd("error", error);
  35. };
  36. clearInfo = (cb = () => {}) => {
  37. this.clear("info", cb);
  38. };
  39. addInfo = (info) => {
  40. this.add("info", info);
  41. };
  42. clearAddInfo = (info) => {
  43. this.clearAdd("info", info);
  44. };
  45. clearSuccess = (cb = () => {}) => {
  46. this.clear("success", cb);
  47. };
  48. addSuccess = (success) => {
  49. this.add("success", success);
  50. };
  51. clearAddSuccess = (success) => {
  52. this.clearAdd("success", success);
  53. };
  54. clearErrorSuccess = (cb) => {
  55. this.setState({
  56. error: [],
  57. success: [],
  58. }, cb);
  59. };
  60. clearAll = (cb) => {
  61. this.setState({
  62. error: [],
  63. success: [],
  64. info: [],
  65. }, cb);
  66. };
  67. clear = (type, cb) => {
  68. this.setState({
  69. [type]: [],
  70. }, cb);
  71. };
  72. add = (type, message) => {
  73. // TODO add error parsing, e.g. for arrays/objects
  74. console.log("CM_ADD", type, message, this.state[type]);
  75. this.setState({
  76. [type]: this.state[type].concat([message]),
  77. });
  78. };
  79. clearAdd = (type, message) => {
  80. this.setState({
  81. [type]: [message],
  82. });
  83. };
  84. list = (type) => {
  85. let messages = this.state[type];
  86. let key = 0;
  87. if (messages.length > 0) {
  88. messages = messages.map((message) => {
  89. key++;
  90. return (<li key={ key }>{ message }</li>);
  91. });
  92. let text = "";
  93. if (type === "error") text = t("customMessages:somethingWentWrong");
  94. else if (type === "info") text = t("customMessages:info");
  95. else if (type === "success") text = t("customMessages:success");
  96. return (
  97. <div key={ type } className={ `custom-messages custom-messages-${type}` }>
  98. <p>{ text }</p>
  99. <ul>
  100. { messages }
  101. </ul>
  102. </div>
  103. );
  104. } return null;
  105. };
  106. render() {
  107. if (this.state.error.length > 0 || this.state.info.length > 0 || this.state.success.length) {
  108. return (
  109. <div>
  110. {this.list("error")}
  111. {this.list("info")}
  112. {this.list("success")}
  113. </div>
  114. );
  115. } else return null;
  116. }
  117. }