Passed
Push — feature/azure-webapp-pipeline-... ( 271549...3c88ad )
by Grant
07:11 queued 10s
created

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

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 90
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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