1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 12:56:41 +07:00

added toasts

This commit is contained in:
Fedor Katurov 2022-01-04 21:10:05 +07:00
parent ef959af711
commit 39e801f6f3
10 changed files with 92 additions and 7 deletions

View file

@ -1,8 +1,21 @@
const handle = (message: string) => console.warn(message);
import { hideToast, showToastError } from '~/utils/toast';
import { has, path } from 'ramda';
import { ERROR_LITERAL, ERRORS } from '~/constants/errors';
let toastId = '';
const handleUnknown = (message: string) => console.warn(message);
const handleTranslated = (message: string) => {
if (toastId) {
hideToast(toastId);
}
toastId = showToastError(ERROR_LITERAL[message]);
};
export const showErrorToast = (error: unknown) => {
if (typeof error === 'string') {
handle(error);
if (typeof error === 'string' && has(error, ERROR_LITERAL)) {
handleTranslated(error);
return;
}
@ -11,5 +24,17 @@ export const showErrorToast = (error: unknown) => {
return;
}
handle(error.message);
// TODO: Network error
if (error.message === 'Network Error') {
handleTranslated(ERRORS.NETWORK_ERROR);
return;
}
const messageFromBackend = String(path(['response', 'data', 'error'], error));
if (messageFromBackend && has(messageFromBackend, ERROR_LITERAL)) {
handleTranslated(messageFromBackend);
return;
}
handleUnknown(error.message);
};

View file

@ -0,0 +1,11 @@
import { Toaster } from 'react-hot-toast';
import React from 'react';
const containerStyle = {
top: 10,
left: 10,
bottom: 10,
right: 10,
};
export const ToastProvider = () => <Toaster containerStyle={containerStyle} />;

20
src/utils/toast/index.tsx Normal file
View file

@ -0,0 +1,20 @@
import React from 'react';
import toast from 'react-hot-toast';
import styles from './styles.module.scss';
import { ToastOptions } from 'react-hot-toast/dist/core/types';
import classNames from 'classnames';
import { isTablet } from '~/constants/dom';
const defaultOptions: ToastOptions = {
icon: null,
duration: 3000,
position: isTablet() ? 'top-center' : 'bottom-center',
};
export const showToastError = (message: string) =>
toast.error(t => <span onClick={() => toast.dismiss(t.id)}>{message}</span>, {
...defaultOptions,
className: classNames(styles.toast, styles.error),
});
export const hideToast = (id: string) => toast.dismiss(id);

View file

@ -0,0 +1,14 @@
@import "src/styles/variables";
.toast {
@include outer_shadow;
cursor: pointer;
}
.error {
font: $font_14_semibold;
background: $red_gradient_alt;
color: white;
user-select: none;
text-transform: uppercase;
}