Passed
Push — task/update-re-reselect ( 601027 )
by
unknown
06:20
created

resources/assets/js/components/ErrorToast.tsx   A

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 98
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 79
mnd 2
bc 2
fnc 0
dl 0
loc 98
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import React, { useEffect } from "react";
2
import { connect } from "react-redux";
3
import {
4
  injectIntl,
5
  FormattedMessage,
6
  WrappedComponentProps,
7
} from "react-intl";
8
import { ApiError } from "redux-api-middleware";
9
import { ErrorEntity } from "../store/Error/errorReducer";
10
import { clearErrors } from "../store/Error/errorActions";
11
import { getRecentError } from "../store/Error/errorSelector";
12
import { RootState } from "../store/store";
13
import { DispatchType } from "../configureStore";
14
15
const constructErrorMessage = (error: ErrorEntity): string => {
16
  if (error.error instanceof ApiError) {
17
    // if error was of type redux-api-middleware/ApiError, we may be able to extract extra info from the response.
18
    const apiError = error.error;
19
    if (apiError.status && apiError.response && apiError.response.message) {
20
      return `${apiError.status} - ${apiError.response.message}`;
21
    }
22
  }
23
  return error.message;
24
};
25
interface ErrorToastProps {
26
  error: ErrorEntity;
27
  dispatchClearErrors: () => void;
28
}
29
const ErrorToast: React.FC<ErrorToastProps & WrappedComponentProps> = ({
30
  error,
31
  dispatchClearErrors,
32
}): React.ReactElement => {
33
  useEffect((): (() => void) => {
34
    const timer = setInterval((): void => {
35
      dispatchClearErrors();
36
      clearInterval(timer);
37
    }, 3500);
38
    return (): void => {
39
      clearInterval(timer);
40
    };
41
  }, [dispatchClearErrors, error]);
42
43
  return (
44
    <>
45
      {error !== undefined && (
46
        <div data-c-alert="error(toast)" data-c-radius="rounded" role="alert">
47
          <div data-c-padding="half">
48
            <span
49
              data-c-margin="bottom(quarter)"
50
              data-c-font-weight="bold"
51
            >
52
              <FormattedMessage
53
                id="errorToast.title"
54
                defaultMessage="Something went wrong!"
55
                description="Title displayed on the Error Toast component."
56
              />
57
            </span>
58
            <p>{constructErrorMessage(error)}</p>
59
          </div>
60
          <button
61
            type="button"
62
            onClick={(): void => {
63
              dispatchClearErrors();
64
            }}
65
            data-c-alert="close-trigger"
66
            data-c-padding="half"
67
          >
68
            <i className="fa fa-times-circle" />
69
          </button>
70
        </div>
71
      )}
72
    </>
73
  );
74
};
75
76
const mapStateToProps = (
77
  state: RootState,
78
): {
79
  error: ErrorEntity;
80
} => ({
81
  error: getRecentError(state),
82
});
83
84
const mapDispatchToProps = (
85
  dispatch: DispatchType,
86
): { dispatchClearErrors: () => void } => ({
87
  dispatchClearErrors: (): void => {
88
    dispatch(clearErrors());
89
  },
90
});
91
92
const ErrorToastContainer = connect(
93
  mapStateToProps,
94
  mapDispatchToProps,
95
)(injectIntl(ErrorToast));
96
97
export default ErrorToastContainer;
98