Skip to content

Commit

Permalink
OV-7: + toast service
Browse files Browse the repository at this point in the history
  • Loading branch information
WilsonBravo committed Aug 20, 2024
1 parent 327e22d commit bda0e54
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { createStandaloneToast } from '@chakra-ui/react';
import {
type Middleware,
isRejected,
isRejectedWithValue,
} from '@reduxjs/toolkit';
import { type ServerValidationErrorResponse } from 'shared';

import { theme } from '~/framework/theme/theme.js';
import { toastService } from '../services/services.js';

import { stringToReactNode } from '../helpers/helpers.js';

const { toast } = createStandaloneToast({ theme: theme });
const toastId = 'redux-store-error';

const errorMiddleware: Middleware = () => {
Expand All @@ -28,16 +24,8 @@ const errorMiddleware: Middleware = () => {
}
}

if (message !== '' && !toast.isActive(toastId)) {
toast({
id: toastId,
title: 'An error occurred.',
description: stringToReactNode(message),
status: 'error',
duration: 7000,
isClosable: true,
position: 'top-right',
});
if (message !== '' && !toastService.isActive(toastId)) {
toastService.error(message, toastId, 'An error occurred.');
}

return next(action);
Expand Down
1 change: 1 addition & 0 deletions frontend/src/bundles/common/services/services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { type ToastService, toastService } from './toast/toast.js';
92 changes: 92 additions & 0 deletions frontend/src/bundles/common/services/toast/toast.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { type createStandaloneToast } from '@chakra-ui/react';

type Constructor = {
toast: ReturnType<typeof createStandaloneToast>['toast'];
};

class ToastService {
private toast: ReturnType<typeof createStandaloneToast>['toast'];

public constructor({ toast }: Constructor) {
this.toast = toast;
}

public warn = (message: string, toastId: string, title: string): void => {
this.toast({
id: toastId,
title: title,
description: message,
status: 'warning',
duration: 7000,
isClosable: true,
position: 'top-right',
variant: 'solid',
});
};

public loading = (
message: string,
toastId: string,
title: string,
): void => {
this.toast({
id: toastId,
title: title,
description: message,
status: 'loading',
duration: 7000,
isClosable: true,
position: 'top-right',
variant: 'solid',
});
};

public error = (message: string, toastId: string, title: string): void => {
this.toast({
id: toastId,
title: title,
description: message,
status: 'error',
duration: 7000,
isClosable: true,
position: 'top-right',
variant: 'solid',
});
};

public success = (
message: string,
toastId: string,
title: string,
): void => {
this.toast({
id: toastId,
title: title,
description: message,
status: 'success',
duration: 7000,
isClosable: true,
position: 'top-right',
variant: 'solid',
});
};

public info = (message: string, toastId: string, title: string): void => {
this.toast({
id: toastId,
title: title,
description: message,
status: 'info',
duration: 7000,
isClosable: true,
position: 'top-right',
variant: 'solid',
});
};

public isActive = (toastId: string): boolean => {
return this.toast.isActive(toastId);
};
}

export { ToastService };
12 changes: 12 additions & 0 deletions frontend/src/bundles/common/services/toast/toast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { createStandaloneToast } from '@chakra-ui/react';

import { theme } from '~/framework/theme/theme.js';

import { ToastService } from './toast.service.js';

const { toast } = createStandaloneToast({ theme: theme });

const toastService = new ToastService({ toast });

export { ToastService } from './toast.service.js';
export { toastService };

0 comments on commit bda0e54

Please sign in to comment.