CustomMessages.jsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. const i18n = require("i18n");
  4. const t = i18n.default.translator.translate;
  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("errors", 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. this.setState({
  74. [type]: this.state[type].concat([message]),
  75. });
  76. };
  77. clearAdd = (type, message) => {
  78. this.setState({
  79. [type]: [message],
  80. });
  81. };
  82. list = (type) => {
  83. let messages = this.state[type];
  84. let key = 0;
  85. if (messages.length > 0) {
  86. messages = messages.map((message) => {
  87. key++;
  88. return (<li key={ key }>{ message }</li>);
  89. });
  90. let text = "";
  91. if (type === "error") text = t("customMessages:somethingWentWrong");
  92. else if (type === "info") text = t("customMessages:info");
  93. else if (type === "success") text = t("customMessages:success");
  94. return (
  95. <div key={ type } className={ type }>
  96. <p>{ text }</p>
  97. <ul>
  98. { messages }
  99. </ul>
  100. </div>
  101. );
  102. } return null;
  103. };
  104. render() {
  105. return (
  106. <div>
  107. { this.list("error") }
  108. { this.list("info") }
  109. { this.list("success") }
  110. </div>
  111. );
  112. }
  113. }