tycho-components 0.29.3 → 0.29.4

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.
@@ -9,6 +9,7 @@ type ErrorDispatcher = {
9
9
  t?: TFunction;
10
10
  key?: string;
11
11
  };
12
+ export declare const getErrorMessageValue: ({ err, t, key }: ErrorDispatcher) => string;
12
13
  export declare const useMessageUtils: () => {
13
14
  dispatchLoading: (val: boolean, text?: string) => void;
14
15
  dispatchError: ({ err, t, key }: ErrorDispatcher) => void;
@@ -1,6 +1,37 @@
1
1
  import { useContext } from "react";
2
2
  import { message, silentLoading, toastLoading } from "../configs/store/actions";
3
3
  import CommonContext from "./CommonContext";
4
+ const translateErrorValue = (t, namespace, rawValue) => {
5
+ const entry = `${namespace}:${rawValue}`;
6
+ const translatedValue = t(entry);
7
+ return translatedValue === entry ? rawValue : translatedValue;
8
+ };
9
+ export const getErrorMessageValue = ({ err, t, key }) => {
10
+ if (!t) {
11
+ return err;
12
+ }
13
+ if (!err.response) {
14
+ return translateErrorValue(t, key ?? "message", String(err));
15
+ }
16
+ if (err.response.status === 403) {
17
+ return t("error.access.authorization");
18
+ }
19
+ let errorKey = "";
20
+ if (err.response.data?.message) {
21
+ errorKey = err.response.data.message;
22
+ }
23
+ else if (err.response.data?.description) {
24
+ errorKey = err.response.data.description;
25
+ }
26
+ else if (err.response.data?.errors?.length > 0) {
27
+ errorKey = err.response.data.errors[0].description;
28
+ }
29
+ else {
30
+ key = "common";
31
+ errorKey = "internal.server.error";
32
+ }
33
+ return translateErrorValue(t, key ?? "message", errorKey);
34
+ };
4
35
  export const useMessageUtils = () => {
5
36
  const { state, dispatch } = useContext(CommonContext);
6
37
  const isSilentLoading = () => {
@@ -18,34 +49,7 @@ export const useMessageUtils = () => {
18
49
  : toastLoading(val));
19
50
  };
20
51
  const dispatchError = ({ err, t, key }) => {
21
- if (!t) {
22
- dispatch(message({ value: err, type: "error" }));
23
- return;
24
- }
25
- if (!err.response) {
26
- const value = t(`${key ?? "message"}:${err}`);
27
- dispatch(message({ value, type: "error" }));
28
- return;
29
- }
30
- if (err.response.status === 403) {
31
- dispatch(message({ value: t("error.access.authorization"), type: "error" }));
32
- return;
33
- }
34
- let errorKey = "";
35
- if (err.response.data?.message) {
36
- errorKey = err.response.data.message;
37
- }
38
- else if (err.response.data?.description) {
39
- errorKey = err.response.data.description;
40
- }
41
- else if (err.response.data?.errors?.length > 0) {
42
- errorKey = err.response.data.errors[0].description;
43
- }
44
- else {
45
- key = "common";
46
- errorKey = "internal.server.error";
47
- }
48
- const value = t(`${key ?? "message"}:${errorKey}`);
52
+ const value = getErrorMessageValue({ err, t, key });
49
53
  dispatch(message({ value, type: "error" }));
50
54
  };
51
55
  const dispatchMessage = ({ key, ns, t }) => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tycho-components",
3
3
  "private": false,
4
- "version": "0.29.3",
4
+ "version": "0.29.4",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "exports": {