Skip to content

Commit

Permalink
add toast (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
mazmassa authored Jun 22, 2023
1 parent 48ab97c commit 81347da
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 3 deletions.
42 changes: 39 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"react": "^18.2.0",
"react-currency-input-field": "^3.6.10",
"react-dom": "^18.2.0",
"react-hot-toast": "^2.4.1",
"react-icons": "^4.8.0",
"tw-colors": "^1.2.5",
"vite-plugin-svgr": "^3.2.0"
Expand Down
24 changes: 24 additions & 0 deletions src/components/Toast/Toast.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Toast, toast } from './Toast';
import { Button } from './../Button/Button';

export default { title: 'Components/Toast' };

export const _Toast = {
render: () => (
<>
<Button
onClick={() => {
toast.error('Euhh! This is an error message!');
}}
>
Create ERROR Toast
</Button>
<br />
<br />
<Button onClick={() => toast.success('Uuurraa! This is a success CSS')}>
Create SUCCESS Toast
</Button>
<Toast theme="theme-dark" />
</>
),
};
13 changes: 13 additions & 0 deletions src/components/Toast/Toast.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';
import { Toast } from './Toast';

describe('Components | Toast', () => {
test('it should render', () => {
render(<Toast content="this is the content" error="Oupps!" />);

let input = screen.getByTestId('toast');

expect(input).toBeInTheDocument();
});
});
89 changes: 89 additions & 0 deletions src/components/Toast/Toast.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import React from 'react';

import { useEffect, useState } from 'react';
import { toast as _toast, Toaster } from 'react-hot-toast';
import { ComponentPropsWithoutRef } from 'react';
import { FiX, FiCheckCircle, FiAlertCircle } from 'react-icons/fi';
import { Button } from '../Button';
import { useLocalStorage } from '../../util/useLocalStorage';

interface ToastProps extends ComponentPropsWithoutRef<'div'> {
error?: string;
success?: string;
theme?: string;
}

function Error(props: ToastProps) {
const { error } = props;

return (
<>
<div
className="inline-flex items-center justify-center flex-shrink-0
w-10 h-10 text-s-error bg-s-error bg-opacity-25 rounded-lg"
>
<FiAlertCircle size={24} />
</div>
<div className="ml-3 text-sm font-normal pr-2 text-s-error">{error}</div>
</>
);
}

function Success(props: ToastProps) {
const { success } = props;

return (
<>
<div
className="inline-flex items-center justify-center flex-shrink-0
w-10 h-10 text-s-success bg-s-success bg-opacity-25 rounded-lg"
>
<FiCheckCircle size={24} />
</div>
<div className="ml-3 text-sm font-normal pr-2 text-s-success">
{success}
</div>
</>
);
}

export const toast = _toast;

export function Toast(props: ToastProps) {
const { error, theme: _theme, ...rest } = props;

const [storedTheme] = useLocalStorage<string>(
'massa-station-theme',
'theme-dark',
);
const [theme, setTheme] = useState<string>(storedTheme);
const isError = Boolean(error);

useEffect(() => {
setTheme(_theme ?? storedTheme);
}, [_theme]);

return (
<div data-testid="toast">
<Toaster position="bottom-left">
{(t) => (
<div
className={`${theme} flex items-center w-fit p-4 text-primary bg-secondary rounded-lg shadow`}
{...rest}
>
{t.type === 'error' || isError ? (
<Error error={t.message?.toString()} {...props} />
) : (
<Success success={t.message?.toString()} {...props} />
)}
<Button variant="icon" onClick={() => toast.dismiss(t.id)}>
<FiX />
</Button>
</div>
)}
</Toaster>
</div>
);
}
1 change: 1 addition & 0 deletions src/components/Toast/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Toast';
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ export * from './TextArea';
export * from './DragDrop';
export * from './Vote';
export * from './Spinner';
export * from './Toast';

0 comments on commit 81347da

Please sign in to comment.