@autobusal/providers 1.0.55 → 1.0.56

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.
@@ -0,0 +1,28 @@
1
+ import React from 'react';
2
+ import Message from './Message';
3
+
4
+ interface Props {
5
+ children: JSX.Element[] | JSX.Element
6
+ }
7
+
8
+ class ErrorBoundary extends React.Component<Props> {
9
+ state = { hasError: false };
10
+
11
+ static getDerivedStateFromError() {
12
+ return { hasError: true };
13
+ }
14
+
15
+ componentDidCatch(error: Error, info: React.ErrorInfo): void {
16
+ console.log(error, info);
17
+ }
18
+
19
+ render() {
20
+ if (this.state.hasError) {
21
+ return <Message type="error" />;
22
+ }
23
+
24
+ return this.props.children;
25
+ }
26
+ }
27
+
28
+ export default ErrorBoundary;
@@ -0,0 +1,37 @@
1
+ import { useNavigate } from 'react-router-dom';
2
+ import { RiAlarmWarningFill, RiFileWarningLine } from 'react-icons/ri';
3
+ import { Button } from '@autobusal/common';
4
+ import { getLanguage } from '../Setup/utilities/languages';
5
+ import { Container, Description } from './styles';
6
+ import errors from '@lang/errors';
7
+
8
+ interface Props {
9
+ type: 'error' | 'not_found'
10
+ }
11
+
12
+ const Message = ({ type }: Props): JSX.Element => {
13
+ const navigate = useNavigate();
14
+
15
+ const language = getLanguage('en');
16
+
17
+ const translations = errors[language as keyof typeof errors];
18
+
19
+ return (
20
+ <Container>
21
+ { type === 'not_found' ? <RiFileWarningLine size={ 100 } /> : <RiAlarmWarningFill size={ 100 } /> }
22
+
23
+ <Description>
24
+ { type === 'not_found' ? translations.not_found : translations.system }
25
+ </Description>
26
+
27
+ <Button
28
+ type="button"
29
+ text={ translations.back }
30
+ noMargin
31
+ onClick={ () => navigate('/') }
32
+ />
33
+ </Container>
34
+ );
35
+ };
36
+
37
+ export default Message;
@@ -0,0 +1,14 @@
1
+ import styled from 'styled-components';
2
+
3
+ export const Container = styled.div`
4
+ height: 100%;
5
+ display: flex;
6
+ flex-direction: column;
7
+ justify-content: center;
8
+ align-items: center;
9
+ gap: 15px;
10
+ `;
11
+
12
+ export const Description = styled.div`
13
+ margin-bottom: 15px;
14
+ `;
@@ -1,6 +1,7 @@
1
1
  import axios from 'axios';
2
- // import i18next from 'i18next';
3
- // import { error } from '@utilities/notify';
2
+ import languages from '@lang/errors';
3
+ import { getLanguage } from '../Setup/utilities/languages';
4
+ import { error as systemError } from '@autobusal/utilities';
4
5
 
5
6
  const apiClient = axios.create({
6
7
  baseURL: import.meta.env.VITE_API_OBT,
@@ -9,11 +10,11 @@ const apiClient = axios.create({
9
10
 
10
11
  apiClient.interceptors.request.use(
11
12
  config => {
12
- // // const token = localStorage.getItem('token');
13
+ const token = localStorage.getItem('token');
13
14
 
14
- // // if (token !== null) {
15
- // // config.headers.Authorization = token;
16
- // // }
15
+ if (token !== null) {
16
+ config.headers.Authorization = `Bearer ${ token }`;
17
+ }
17
18
 
18
19
  return config;
19
20
  }
@@ -21,29 +22,29 @@ apiClient.interceptors.request.use(
21
22
 
22
23
  apiClient.interceptors.response.use(response => (
23
24
  response
24
- ), err => {
25
- console.log(err.response);
26
-
27
- // if (err.response) {
28
- // // if we have a 401, we go to the logout page
29
- // // if (err.response.status === 401) {
30
- // // alert(401);
31
- // // }
32
-
33
- // // @todo: how to handle 404?
34
-
35
- // // if we have a 422 error, we display the message
36
- // if (err.response.status === 422) {
37
- // error(err.response.data.message);
38
- // }
39
- // } else if (err.request) {
40
- // // if we have a 500 error, we display an error message
41
- // error(i18next.t('server_error', { ns: 'general' }))
42
- // } else {
43
- // console.log('ERROR', err.message);
44
- // }
45
-
46
- return err;
25
+ ), error => {
26
+ if (error.response) {
27
+ // if we have a 401, we go to the logout page
28
+ if (error.response.status === 401) {
29
+ alert(401);
30
+ }
31
+
32
+ // if we have a 422 error, we display the message
33
+ if (error.response.status === 422) {
34
+ systemError(error.response.data.message);
35
+ }
36
+
37
+ // if we have a 500 error, we display a generic error message
38
+ if (error.response.status === 500) {
39
+ const language = getLanguage('en');
40
+
41
+ systemError(languages[language as keyof typeof languages].server);
42
+ }
43
+ } else if (error.request) {
44
+ alert('NO REQUEST!!!');
45
+ } else {
46
+ alert('OPAOPA!');
47
+ }
47
48
  });
48
49
 
49
50
  export default apiClient;
@@ -2,9 +2,11 @@ import i18next from 'i18next';
2
2
  import Backend from 'i18next-http-backend';
3
3
  import { initReactI18next } from 'react-i18next';
4
4
 
5
- const languages = (type: string, defaultLanguage: string): void => {
6
- const currentLanguage = localStorage.getItem('language');
5
+ export const getLanguage = (defaultLanguage: string): string => (
6
+ localStorage.getItem('language') ?? defaultLanguage
7
+ );
7
8
 
9
+ const languages = (type: string, defaultLanguage: string): void => {
8
10
  if (i18next.isInitialized) {
9
11
  return;
10
12
  }
@@ -16,7 +18,7 @@ const languages = (type: string, defaultLanguage: string): void => {
16
18
  interpolation: {
17
19
  escapeValue: false
18
20
  },
19
- lng: currentLanguage ?? defaultLanguage,
21
+ lng: getLanguage(defaultLanguage),
20
22
  fallbackLng: defaultLanguage,
21
23
  debug: true,
22
24
  backend: {
package/index.ts CHANGED
@@ -1,11 +1,15 @@
1
1
  import Providers from './Providers';
2
2
  import apiClient from './Queries/apiClient';
3
3
  import useSystemTheme from './Styles/hooks/useSystemTheme';
4
+ import ErrorBoundary from './Errors/ErrorBoundary';
5
+ import Message from './Errors/Message';
4
6
  import useMenuStore from './stores/menu';
5
7
  import useUserStore from './stores/user';
6
8
 
7
9
  export {
8
10
  apiClient,
11
+ ErrorBoundary,
12
+ Message,
9
13
  useMenuStore,
10
14
  useSystemTheme,
11
15
  useUserStore
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autobusal/providers",
3
- "version": "1.0.55",
3
+ "version": "1.0.56",
4
4
  "type": "module",
5
5
  "main": "index.ts"
6
6
  }