Skip to content

Commit

Permalink
refactor: snackbar
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramin committed Nov 6, 2022
1 parent a17cd23 commit e0cefac
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
/* eslint-disable unicorn/no-unused-properties */
import React, { useEffect } from 'react';
import { ReactNode, useEffect } from 'react';
import { ToastContainer, toast, Slide, Zoom, Flip, Bounce } from 'react-toastify';

import { SNACKBAR_POSITIONS, SNACKBAR_TYPES } from 'constants/Snackbar';

import 'react-toastify/dist/ReactToastify.min.css';

const TOAST_COLOR_TYPES = {
export type SnackBarType = 'info' | 'error' | 'warn' | 'success' | 'message';

const TOAST_COLOR_TYPES: {
DARK: 'dark';
LIGHT: 'light';
COLORED: 'colored';
} = {
DARK: 'dark',
LIGHT: 'light',
COLORED: 'colored'
Expand All @@ -19,12 +24,27 @@ const TOAST_TRANSITION_TYPES = {
BOUNCE: Bounce
};

interface SnackBarEvent {
detail: {
delay?: number;
message: string;
type?: SnackBarType;
icon?: null | ReactNode;
position?:
| 'top-left'
| 'top-right'
| 'top-center'
| 'bottom-right'
| 'bottom-center'
| 'bottom-left';
};
}

export function SnackbarWrapper() {
// eslint-disable-next-line unicorn/consistent-function-scoping, consistent-return
const createSnackBar = (event) => {
const createSnackBar = (event: SnackBarEvent) => {
const {
icon,
message,
icon = null,
delay = 3000,
type = SNACKBAR_TYPES.MESSAGE,
position = SNACKBAR_POSITIONS.BOTTOM_CENTER
Expand All @@ -33,28 +53,30 @@ export function SnackbarWrapper() {

if (type === SNACKBAR_TYPES.MESSAGE) {
return toast(message, {
icon,
toastId,
position,
autoClose: delay,
icon: () => icon,
theme: TOAST_COLOR_TYPES.DARK,
transition: TOAST_TRANSITION_TYPES.SLIDE
});
}

toast[type]?.(message, {
icon,
toastId,
position,
autoClose: delay,
icon: () => icon,
theme: TOAST_COLOR_TYPES.COLORED,
transition: TOAST_TRANSITION_TYPES.SLIDE
});
};

useEffect(() => {
// @ts-ignore: Unreachable code error
window.addEventListener('GenerateSnackbar', createSnackBar);
return () => {
// @ts-ignore: Unreachable code error
window.removeEventListener('GenerateSnackbar', createSnackBar);
};
}, []);
Expand Down
16 changes: 0 additions & 16 deletions src/constants/Snackbar.js

This file was deleted.

29 changes: 29 additions & 0 deletions src/constants/Snackbar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export const SNACKBAR_TYPES: {
INFO: 'info';
ERROR: 'error';
WARNING: 'warn';
MESSAGE: 'message';
SUCCESS: 'success';
} = {
INFO: 'info',
ERROR: 'error',
WARNING: 'warn',
MESSAGE: 'message',
SUCCESS: 'success'
};

export const SNACKBAR_POSITIONS: {
TOP_LEFT: 'top-left';
TOP_RIGHT: 'top-right';
TOP_CENTER: 'top-center';
BOTTOM_LEFT: 'bottom-left';
BOTTOM_RIGHT: 'bottom-right';
BOTTOM_CENTER: 'bottom-center';
} = {
TOP_LEFT: 'top-left',
TOP_RIGHT: 'top-right',
TOP_CENTER: 'top-center',
BOTTOM_LEFT: 'bottom-left',
BOTTOM_RIGHT: 'bottom-right',
BOTTOM_CENTER: 'bottom-center'
};

0 comments on commit e0cefac

Please sign in to comment.