tycho-components 0.0.9-SNAPSHOT → 0.0.9-SNAPSHOT-1

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.
@@ -45,7 +45,6 @@ export default function AppToast() {
45
45
  }
46
46
  }, [state.toastLoading]);
47
47
  useEffect(() => {
48
- console.log(state.message);
49
48
  if (state.message && state.message.value !== '') {
50
49
  switch (state.message.type) {
51
50
  case 'error':
@@ -7,12 +7,13 @@ import { TextField } from 'tycho-storybook';
7
7
  import * as yup from 'yup';
8
8
  import AppModal from '../../AppModal';
9
9
  import CommonContext from '../../configs/CommonContext';
10
- import { dispatchError, dispatchLoading } from '../../configs/MessageUtils';
11
10
  import ParticipantService from '../types/ParticipantService';
12
11
  import './style.scss';
12
+ import { useMessageUtils } from '../../configs/useMessageUtils';
13
13
  export default function ParticipantCreate({ document, onClose, onCreate, }) {
14
14
  const { t } = useTranslation('participants');
15
- const { dispatch, state } = useContext(CommonContext);
15
+ const { state } = useContext(CommonContext);
16
+ const { dispatchLoading, dispatchError } = useMessageUtils();
16
17
  const createdForm = useForm({
17
18
  resolver: yupResolver(getFormSchema(t)),
18
19
  mode: 'onChange',
@@ -20,16 +21,16 @@ export default function ParticipantCreate({ document, onClose, onCreate, }) {
20
21
  const handleAdd = () => {
21
22
  if (state.toastLoading)
22
23
  return;
23
- dispatchLoading(true, dispatch);
24
+ dispatchLoading(true);
24
25
  ParticipantService.add(document, createdForm.getValues())
25
26
  .then((r) => {
26
27
  onCreate(r.data);
27
28
  })
28
29
  .catch((err) => {
29
- dispatchError({ err, dispatch, t, key: 'participants' });
30
+ dispatchError({ err, t, key: 'participants' });
30
31
  })
31
32
  .finally(() => {
32
- dispatchLoading(false, dispatch);
33
+ dispatchLoading(false);
33
34
  });
34
35
  };
35
36
  return (_jsx(AppModal, { title: t('modal.add.title'), className: "modal-participant", close: onClose, confirm: handleAdd, disableConfirm: !createdForm.formState.isValid, children: _jsxs("div", { className: "participant-container", children: [_jsx(TextField, { label: t('modal.input.code'), attr: "code",
@@ -3,24 +3,24 @@ import { useContext } from 'react';
3
3
  import { useTranslation } from 'react-i18next';
4
4
  import AppModalRemove from '../../AppModal/AppModalRemove';
5
5
  import CommonContext from '../../configs/CommonContext';
6
- import { dispatchLoading } from '../../configs/MessageUtils';
7
- import { toastLoading } from '../../configs/store/actions';
6
+ import { useMessageUtils } from '../../configs/useMessageUtils';
8
7
  import ParticipantService from '../types/ParticipantService';
9
8
  import './style.scss';
10
9
  export default function ParticipantRemove({ participant, participants, document, onClose, onChange, }) {
11
10
  const { t } = useTranslation('participants');
12
- const { dispatch, state } = useContext(CommonContext);
11
+ const { state } = useContext(CommonContext);
12
+ const { dispatchLoading, dispatchError } = useMessageUtils();
13
13
  const handleRemove = () => {
14
14
  if (state.toastLoading || !participant)
15
15
  return;
16
- dispatchLoading(true, dispatch);
16
+ dispatchLoading(true);
17
17
  ParticipantService.remove(document, participant.code)
18
18
  .then(() => {
19
19
  const list = participants?.filter((p) => p.code !== participant.code) || [];
20
20
  onChange(list);
21
21
  })
22
22
  .finally(() => {
23
- dispatch(toastLoading(false));
23
+ dispatchLoading(false);
24
24
  });
25
25
  };
26
26
  return (_jsx(AppModalRemove, { title: t('modal.remove.title'), subtitle: t('modal.remove.description'), onClose: onClose, onConfirm: handleRemove }));
@@ -6,15 +6,16 @@ import { Button, Form } from 'react-bootstrap';
6
6
  import { useTranslation } from 'react-i18next';
7
7
  import AppEditable from '../AppEditable';
8
8
  import CommonContext from '../configs/CommonContext';
9
- import { dispatchMessage } from '../configs/MessageUtils';
10
9
  import ParticipantCreate from './ParticipantCreate';
11
10
  import ParticipantRemove from './ParticipantRemove';
12
11
  import './style.scss';
13
12
  import { PARTICIPANT_FIELDS } from './types/Participant';
14
13
  import ParticipantService from './types/ParticipantService';
14
+ import { useMessageUtils } from '../configs/useMessageUtils';
15
15
  export default function Participants({ document, participants, onChange, }) {
16
16
  const { t } = useTranslation('participants');
17
17
  const { dispatch } = useContext(CommonContext);
18
+ const { dispatchMessage } = useMessageUtils();
18
19
  const [participant, setParticipant] = useState();
19
20
  const [openRemove, setOpenRemove] = useState(false);
20
21
  const [openCreate, setOpenCreate] = useState(false);
@@ -23,7 +24,7 @@ export default function Participants({ document, participants, onChange, }) {
23
24
  return;
24
25
  ParticipantService.update(field)
25
26
  .then(() => {
26
- dispatchMessage({ key: 'update.success', dispatch, t });
27
+ dispatchMessage({ key: 'update.success', t });
27
28
  const { name, value } = field;
28
29
  const thisParticipants = participants;
29
30
  const idx = participants.findIndex((p) => p.code === field.ref);
@@ -0,0 +1,17 @@
1
+ import { TFunction } from 'i18next';
2
+ type MessageDispatcher = {
3
+ key: string;
4
+ ns?: string;
5
+ t: TFunction;
6
+ };
7
+ type ErrorDispatcher = {
8
+ err: any;
9
+ t: TFunction;
10
+ key?: string;
11
+ };
12
+ export declare const useMessageUtils: () => {
13
+ dispatchLoading: (val: boolean) => void;
14
+ dispatchError: ({ err, t, key }: ErrorDispatcher) => void;
15
+ dispatchMessage: ({ key, ns, t }: MessageDispatcher) => void;
16
+ };
17
+ export {};
@@ -0,0 +1,35 @@
1
+ import { useContext } from 'react';
2
+ import { message, toastLoading } from '../configs/store/actions';
3
+ import CommonContext from './CommonContext';
4
+ export const useMessageUtils = () => {
5
+ const { dispatch } = useContext(CommonContext);
6
+ const dispatchLoading = (val) => {
7
+ dispatch(toastLoading(val));
8
+ };
9
+ const dispatchError = ({ err, t, key }) => {
10
+ if (!err.response) {
11
+ dispatch(message({ value: t(err), type: 'error' }));
12
+ return;
13
+ }
14
+ if (err.response.status === 403) {
15
+ dispatch(message({ value: t('error.access.authorization'), type: 'error' }));
16
+ return;
17
+ }
18
+ let errorKey = 'generic.error.message';
19
+ if (err.response.data?.description)
20
+ errorKey = err.response.data.description;
21
+ if (err.response.data?.errors?.length > 0)
22
+ errorKey = err.response.data.errors[0].description;
23
+ const value = t(`${key ?? 'message'}:${errorKey}`);
24
+ dispatch(message({ value, type: 'error' }));
25
+ };
26
+ const dispatchMessage = ({ key, ns, t }) => {
27
+ const entry = ns ? `${ns}:${key}` : `message:${key}`;
28
+ dispatch(message({ value: t(entry), type: 'success' }));
29
+ };
30
+ return {
31
+ dispatchLoading,
32
+ dispatchError,
33
+ dispatchMessage,
34
+ };
35
+ };
package/dist/index.d.ts CHANGED
@@ -7,4 +7,4 @@ export { default as AppModalConfirm } from './AppModal/AppModalConfirm';
7
7
  export { default as AppModalRemove } from './AppModal/AppModalRemove';
8
8
  export { commonResources } from './configs/Localization';
9
9
  export { CommonProvider } from './configs/CommonContext';
10
- export { default as MessageUtils } from './configs/MessageUtils';
10
+ export { useMessageUtils } from './configs/useMessageUtils';
package/dist/index.js CHANGED
@@ -7,4 +7,4 @@ export { default as AppModalConfirm } from './AppModal/AppModalConfirm';
7
7
  export { default as AppModalRemove } from './AppModal/AppModalRemove';
8
8
  export { commonResources } from './configs/Localization';
9
9
  export { CommonProvider } from './configs/CommonContext';
10
- export { default as MessageUtils } from './configs/MessageUtils';
10
+ export { useMessageUtils } from './configs/useMessageUtils';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tycho-components",
3
3
  "private": false,
4
- "version": "0.0.9-SNAPSHOT",
4
+ "version": "0.0.9-SNAPSHOT-1",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "exports": {
@@ -1,22 +0,0 @@
1
- import { StoreAction } from '../configs/store/types';
2
- import { TFunction } from 'i18next';
3
- type MessageDispatcher = {
4
- key: string;
5
- ns?: string;
6
- dispatch: React.Dispatch<StoreAction>;
7
- t: TFunction;
8
- };
9
- type ErrorDispatcher = {
10
- err: any;
11
- dispatch: React.Dispatch<StoreAction>;
12
- t: TFunction;
13
- key?: string;
14
- };
15
- export declare const dispatchLoading: (val: boolean, dispatch: React.Dispatch<StoreAction>) => void;
16
- export declare const dispatchError: ({ err, dispatch, t, key }: ErrorDispatcher) => void;
17
- export declare const dispatchMessage: ({ key, ns, dispatch, t, }: MessageDispatcher) => void;
18
- declare const MessageUtils: {
19
- dispatchError: ({ err, dispatch, t, key }: ErrorDispatcher) => void;
20
- dispatchMessage: ({ key, ns, dispatch, t, }: MessageDispatcher) => void;
21
- };
22
- export default MessageUtils;
@@ -1,34 +0,0 @@
1
- import { message, toastLoading } from '../configs/store/actions';
2
- export const dispatchLoading = (val, dispatch) => {
3
- dispatch(toastLoading(val));
4
- };
5
- // TODO: improve
6
- export const dispatchError = ({ err, dispatch, t, key }) => {
7
- if (!err.response) {
8
- dispatch(message({ value: t(err), type: 'error' }));
9
- return;
10
- }
11
- if (err.response.status && err.response.status === 403) {
12
- dispatch(message({
13
- value: t('error.access.authorization'),
14
- type: 'error',
15
- }));
16
- return;
17
- }
18
- let errorKey = 'generic.error.message';
19
- if (err.response.data && err.response.data.description)
20
- errorKey = err.response.data.description;
21
- if (err.response.data.errors && err.response.data.errors.length > 0)
22
- errorKey = err.response.data.errors[0].description;
23
- const value = t(`${key ? key : 'message'}:${errorKey}`);
24
- dispatch(message({ value: value, type: 'error' }));
25
- };
26
- export const dispatchMessage = ({ key, ns, dispatch, t, }) => {
27
- const entry = ns ? `${ns}:${key}` : `message:${key}`;
28
- dispatch(message({ value: t(entry), type: 'success' }));
29
- };
30
- const MessageUtils = {
31
- dispatchError,
32
- dispatchMessage,
33
- };
34
- export default MessageUtils;