@dvrd/dvr-controls 1.0.3 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dvrd/dvr-controls",
3
- "version": "1.0.3",
3
+ "version": "1.0.6",
4
4
  "description": "Custom web controls",
5
5
  "main": "index.ts",
6
6
  "scripts": {},
@@ -13,24 +13,25 @@ interface Props {
13
13
  className?: string;
14
14
  disabled?: boolean;
15
15
  underline?: boolean;
16
+ target?: string;
16
17
  }
17
18
 
18
19
  export default function Link(props: React.PropsWithChildren<Props>) {
19
- const onClick = (evt: React.MouseEvent) => {
20
+ const {className, disabled, route, children, underline, target, onClick} = props;
21
+
22
+ function _onClick(evt: React.MouseEvent) {
20
23
  evt.preventDefault();
21
- const {disabled, route, onClick} = props;
22
24
  if (!disabled) {
23
25
  if (route) {
24
- if (route.startsWith('http')) window.open(route);
26
+ if (/^(www|http).+/.test(route)) window.open(route, target);
25
27
  else navigate(route);
26
28
  }
27
29
  if (onClick) onClick(evt);
28
30
  }
29
- };
31
+ }
30
32
 
31
- const {className, disabled, route, children, underline} = props;
32
33
  return (
33
- <a href={route} onClick={onClick}
34
+ <a href={route} onClick={_onClick} target={target}
34
35
  className={classNames('app-link', className, disabled && 'disabled',
35
36
  underline && 'underline')}>{children}</a>
36
37
  );
@@ -7,7 +7,7 @@ import React, {MouseEventHandler, ReactNode, useContext, useEffect, useRef, useS
7
7
  import {useLocation} from 'react-router';
8
8
  import {SidebarItem, SideMenuMode} from "../util/interfaces";
9
9
  import classNames from 'classnames';
10
- import {AwesomeIcon, generateComponentId} from "../../../index";
10
+ import {AwesomeIcon, generateComponentId, isAbsoluteLink} from "../../../index";
11
11
  import {ControlContext} from "../util/controlContext";
12
12
  import {defer} from 'lodash';
13
13
 
@@ -67,7 +67,14 @@ export default function SidebarMenu(props: Props) {
67
67
  function _onClickItem(item: SidebarItem) {
68
68
  return function (evt: React.MouseEvent) {
69
69
  evt.stopPropagation();
70
- setActiveItem(item.id);
70
+ const {route} = item;
71
+ let _route: string | null = null;
72
+ if (route) {
73
+ if (Array.isArray(route) && route.length) _route = route[0];
74
+ else _route = route as string;
75
+ }
76
+ if (_route && !isAbsoluteLink(_route))
77
+ setActiveItem(item.id);
71
78
  onClickItem(item)(evt);
72
79
  }
73
80
  }
@@ -9,6 +9,8 @@ import {FetchMethod, ResponseData} from './interfaces';
9
9
  import {getJwt} from './jwtUtil';
10
10
  import {voidFunction} from "./controlUtil";
11
11
 
12
+ type ResponseHandler = (response: Response) => void;
13
+
12
14
  export interface FetchOptions {
13
15
  baseUrl?: string | null;
14
16
  url: string;
@@ -17,29 +19,24 @@ export interface FetchOptions {
17
19
  stringifyData?: boolean;
18
20
  headers?: OutgoingHttpHeaders;
19
21
  callback?: Function;
20
- errorCallback?: Function;
22
+ errorCallback?: (response: Response | string) => void;
21
23
  credentials?: string;
22
- responseParser?: (response: Response) => void;
24
+ responseParser?: ResponseHandler;
25
+ errorHandler?: ResponseHandler;
23
26
  }
24
27
 
25
28
  let abortController: AbortController;
26
29
  let signal: AbortSignal;
27
30
  let defaultHeaders: OutgoingHttpHeaders = {};
28
-
29
- // const getSignal = (): AbortSignal | null => {
30
- // if ("AbortController" in window) {
31
- // if (!abortController)
32
- // abortController = new AbortController;
33
- // if (!signal)
34
- // signal = abortController.signal;
35
- // return signal;
36
- // }
37
- // return null;
38
- // };
31
+ let defaultErrorHandler: ResponseHandler | null = null;
39
32
 
40
33
  function getAbortController(): AbortController | null {
41
- if (!('AbortController' in window)) return null;
42
- return new AbortController();
34
+ try {
35
+ if (!('AbortController' in window)) return null;
36
+ return new AbortController();
37
+ } catch {
38
+ return null;
39
+ }
43
40
  }
44
41
 
45
42
  const responseDataIsSuccess = (data: ResponseData): boolean => data.status === 'success';
@@ -48,6 +45,10 @@ export const setDefaultHeaders = (headers: OutgoingHttpHeaders) => {
48
45
  defaultHeaders = headers;
49
46
  }
50
47
 
48
+ export function setDefaultErrorHandler(errorHandler: ResponseHandler | null) {
49
+ defaultErrorHandler = errorHandler;
50
+ }
51
+
51
52
  /**
52
53
  * Cancel all running requests and refresh the abort controller and signal
53
54
  */
@@ -59,64 +60,61 @@ export const cancelAllFetch = () => {
59
60
  }
60
61
  };
61
62
 
62
- export const sendFetch = (config: FetchOptions, legacySupport: boolean = false): AbortController | null => {
63
- config = createFetchConfig(config, legacySupport);
64
- const {url, method, headers, data} = config, options: { [index: string]: any } = {
63
+ export function sendFetch(config: FetchOptions, legacySupport: boolean = false): AbortController | null {
64
+ const baseUrl = config.baseUrl || window.settings.platformUrl;
65
+ if (!baseUrl) throw new Error('Base url is not set!');
66
+
67
+ config = _prepareConfig(config, legacySupport);
68
+ const {url, method, headers, data} = config;
69
+ const options: { [index: string]: any } = {
65
70
  headers,
66
71
  method,
67
72
  body: config.stringifyData === false ? data : JSON.stringify(data),
68
73
  cache: 'no-store',
69
- }, baseUrl = config.baseUrl || window.settings.platformUrl;
70
- if (!baseUrl) throw new Error('Base url is not set!');
74
+ };
71
75
  const abortController = getAbortController();
72
- if(abortController) options.signal = abortController.signal;
73
- // if (getSignal()) options['signal'] = getSignal();
76
+ if (abortController) options.signal = abortController.signal;
77
+
74
78
  options['mode'] = 'cors';
75
79
  fetch(baseUrl + url, options).then((response: Response) => {
76
- const {status} = response;
77
- if (config.responseParser) {
78
- config.responseParser(response);
79
- if (config.callback) config.callback(response);
80
- } else {
81
- if (status === 200) {
82
- if (config.callback) {
83
- if (response.headers.get('content-type') === 'application/json') {
84
- response.json().then((data: any) => {
85
- if (data.message === 'Invalid rights') {
86
- showDialog('Je hebt niet genoeg rechten voor deze actie.');
87
- data.handled = true;
88
- if (config.callback) config.callback(data);
89
- } else if (config.callback) {
90
- data.success = responseDataIsSuccess(data);
91
- config.callback(data);
92
- }
93
- });
94
- } else config.callback(response);
95
- }
96
- } else if (status === 500) {
97
- if (config.errorCallback) config.errorCallback(response);
98
- else if (config.callback) config.callback({
99
- status: 'error',
100
- message: 'Er is iets misgegaan in ons systeem.'
101
- });
102
- } else {
103
- if (config.errorCallback) config.errorCallback(response);
104
- else if (config.callback) config.callback({
105
- status: 'error',
106
- message: 'Er is iets misgegaan in ons systeem.'
107
- });
108
- }
109
- }
80
+ _handleResponse(response, config);
110
81
  }).catch((reason: any) => {
111
82
  if (reason.code === DOMException.ABORT_ERR) {/*Request aborted*/
112
- } else if (config.errorCallback) {
83
+ } else if (config.errorCallback)
113
84
  config.errorCallback(reason);
114
- }
115
85
  });
116
86
  return abortController;
117
- };
87
+ }
118
88
 
119
- const createFetchConfig = (options: FetchOptions, legacySupport: boolean = true): FetchOptions => {
89
+ function _handleResponse(response: Response, config: FetchOptions) {
90
+ if (config.responseParser)
91
+ return config.responseParser(response);
92
+
93
+ if (response.ok) {
94
+ if (!config.callback) return;
95
+ if (response.headers.get('content-type') === 'application/json') {
96
+ response.json().then((data: any) => {
97
+ if (data.message === 'Invalid rights') {
98
+ showDialog('Je hebt niet genoeg rechten voor deze actie.');
99
+ data.handled = true;
100
+ if (config.callback) config.callback(data);
101
+ } else if (config.callback) {
102
+ data.success = responseDataIsSuccess(data);
103
+ config.callback(data);
104
+ }
105
+ });
106
+ } else config.callback(response);
107
+ } else
108
+ _onErrorResponse(response, config);
109
+ }
110
+
111
+ function _onErrorResponse(response: Response, config: FetchOptions) {
112
+ if (config.errorHandler) config.errorHandler(response);
113
+ else if (config.errorCallback) config.errorCallback(response);
114
+ else if (config.callback) config.callback({status: 'error', message: 'Er is iets misgegaan in het systeem.'});
115
+ }
116
+
117
+ function _prepareConfig(options: FetchOptions, legacySupport: boolean = true): FetchOptions {
120
118
  const fetchOptions: FetchOptions = Object.assign({}, options);
121
119
  if (!fetchOptions.method) fetchOptions.method = FetchMethod.GET;
122
120
  if (!fetchOptions.callback) fetchOptions.callback = voidFunction;
@@ -135,12 +133,13 @@ const createFetchConfig = (options: FetchOptions, legacySupport: boolean = true)
135
133
  }
136
134
  }
137
135
  return setAuthHeader(fetchOptions);
138
- };
136
+ }
139
137
 
140
- const setAuthHeader = (options: FetchOptions): FetchOptions => {
141
- const headers: OutgoingHttpHeaders = Object.assign({}, defaultHeaders, options.headers || {}), jwt = getJwt();
138
+ function setAuthHeader(options: FetchOptions): FetchOptions {
139
+ const headers: OutgoingHttpHeaders = Object.assign({}, defaultHeaders, options.headers || {});
140
+ const jwt = getJwt();
142
141
  if (jwt)
143
142
  headers.Authorization = `JWT ${jwt}`;
144
143
  options.headers = headers;
145
144
  return options;
146
- };
145
+ }