Passed
Pull Request — main (#350)
by Alejandro
25:01
created

src/mercure/reducers/mercureInfo.ts   A

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 55
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 45
mnd 2
bc 2
fnc 0
dl 0
loc 55
ccs 19
cts 19
cp 1
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import { Action, Dispatch } from 'redux';
2
import { ShlinkMercureInfo } from '../../utils/services/types';
3
import { GetState } from '../../container/types';
4
import { buildReducer } from '../../utils/helpers/redux';
5
import { ShlinkApiClientBuilder } from '../../utils/services/ShlinkApiClientBuilder';
6
7
/* eslint-disable padding-line-between-statements */
8 1
export const GET_MERCURE_INFO_START = 'shlink/mercure/GET_MERCURE_INFO_START';
9 1
export const GET_MERCURE_INFO_ERROR = 'shlink/mercure/GET_MERCURE_INFO_ERROR';
10 1
export const GET_MERCURE_INFO = 'shlink/mercure/GET_MERCURE_INFO';
11
/* eslint-enable padding-line-between-statements */
12
13
export interface MercureInfo {
14
  token?: string;
15
  mercureHubUrl?: string;
16
  interval?: number;
17
  loading: boolean;
18
  error: boolean;
19
}
20
21
export type GetMercureInfoAction = Action<string> & ShlinkMercureInfo & { interval?: number };
22
23 1
const initialState: MercureInfo = {
24
  loading: true,
25
  error: false,
26
};
27
28
export default buildReducer<MercureInfo, GetMercureInfoAction>({
29 1
  [GET_MERCURE_INFO_START]: (state) => ({ ...state, loading: true, error: false }),
30 1
  [GET_MERCURE_INFO_ERROR]: (state) => ({ ...state, loading: false, error: true }),
31 1
  [GET_MERCURE_INFO]: (_, action) => ({ ...action, loading: false, error: false }),
32
}, initialState);
33
34 1
export const loadMercureInfo = (buildShlinkApiClient: ShlinkApiClientBuilder) =>
35 3
  () => async (dispatch: Dispatch, getState: GetState) => {
36 3
    dispatch({ type: GET_MERCURE_INFO_START });
37
38 3
    const { settings } = getState();
39 3
    const { mercureInfo } = buildShlinkApiClient(getState);
40
41 3
    if (!settings.realTimeUpdates.enabled) {
42 1
      dispatch({ type: GET_MERCURE_INFO_ERROR });
43
44 1
      return;
45
    }
46
47 2
    try {
48 2
      const info = await mercureInfo();
49
50 1
      dispatch<GetMercureInfoAction>({ type: GET_MERCURE_INFO, interval: settings.realTimeUpdates.interval, ...info });
51
    } catch (e) {
52 1
      dispatch({ type: GET_MERCURE_INFO_ERROR });
53
    }
54
  };
55