CustomMessages.jsx 2.3 KB

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