Skip to content

Commit

Permalink
Update Toast component to use custom container and improve styling
Browse files Browse the repository at this point in the history
  • Loading branch information
tomtitherington committed Oct 28, 2023
1 parent f1665c4 commit 7947fb2
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 35 deletions.
4 changes: 2 additions & 2 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState } from 'react';
// import { setOpenApiBase } from 'api/configOpenApi';
import Router from './routes';
import { TokenContext } from 'api';
import { Toaster } from 'react-hot-toast';
import { ToastContainer } from 'components/toasts';

function App() {
const [accessToken, setAccessToken] = useState<string>('');
Expand All @@ -16,7 +16,7 @@ function App() {
return (
<TokenContext.Provider value={{ accessToken, setAccessToken, refreshToken, setRefreshToken }}>
<Router />
<Toaster position="bottom-right" />
<ToastContainer />
</TokenContext.Provider>
);
}
Expand Down
27 changes: 0 additions & 27 deletions frontend/src/components/Toast/index.tsx

This file was deleted.

65 changes: 65 additions & 0 deletions frontend/src/components/toasts/Toast/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { useEffect, useRef, useState } from 'react';
import toast from 'react-hot-toast';
import { MdInfoOutline } from 'react-icons/md';

interface ToastProps {
id?: string;
message: string;
type?: 'info' | 'success' | 'error';
duration?: number;
}

const Toast = ({ id, message, type = 'info', duration = 4000 }: ToastProps) => {
const [progress, setProgress] = useState(100);
const intervalRef = useRef<number | null>(null);
const color = type == 'info' ? 'bg-radial-ultra-light' : type == 'success' ? 'bg-green-600' : 'bg-red-600';
const progressColor =
type == 'info' ? 'bg-gray-200' : type == 'success' ? 'bg-green-200' : 'bg-red-200';

const startProgress = () => {
if (!intervalRef.current) {
intervalRef.current = window.setInterval(() => {
setProgress(prev => Math.max(prev - 100 / (duration / 10), 0));
}, 10);
}
};

const stopProgress = () => {
if (intervalRef.current) {
clearInterval(intervalRef.current);
intervalRef.current = null;
}
};

useEffect(() => {
startProgress();
return stopProgress;
}, []);

return (
<div
onMouseEnter={stopProgress}
onMouseLeave={startProgress}
className="w-80 rounded flex-col justify-start items-start inline-flex">
<div className={`self-stretch h-1 rounded-t ${color}`}>
<div style={{ width: `${progress}%` }} className={`h-1 ${progressColor} `}></div>
</div>
<div
className={`w-full py-2 px-3 shadow justify-center items-center gap-2 inline-flex rounded-b ${color}`}>
<div className="w-4 h-4 relative">
<MdInfoOutline className="text-white" />
</div>
<div className="grow shrink basis-0 flex-col justify-start items-start gap-1 inline-flex">
<p className="self-stretch text-white text-sm w-full">{message}</p>
</div>
<button
className="text-center text-neutral-300 hover:text-neutral-200 text-sm"
onClick={() => toast.remove(id)}>
Close
</button>
</div>
</div>
);
};

export default Toast;
2 changes: 2 additions & 0 deletions frontend/src/components/toasts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {default as ToastContainer } from './ToastContainer';
export {default as Toast } from './Toast';
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Dialog, Transition } from '@headlessui/react';
import EmphasisButton from 'components/buttons/EmphasisButton';
import { TextInput } from 'components/inputs';
import Toast from 'components/Toast';
import Toast from 'components/toasts/Toast';
import { Fragment } from 'react';
import toast from 'react-hot-toast';
import { useNavigate } from 'react-router-dom';
Expand All @@ -21,7 +21,8 @@ export const CreateProjectModal = ({ isOpen, onClose }: CreateProjectModalProps)

const onSubmit = () => {
close();
toast.custom(t => <Toast customMessage="Project created successfully" toast={t} />);
toast.success("Project created successfully");
//toast.custom(t => <Toast customMessage="Project created successfully" toast={t} />);
};

const { formik } = useCreateProject({ onSubmit: onSubmit });
Expand Down
6 changes: 2 additions & 4 deletions frontend/src/features/projects/pages/ProjectSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useProjectsStore } from 'features/projects/stores/useProjectsStore';
import { MdAdd } from 'react-icons/md';
import useSettings from '../hooks/useProjectSettings';
import toast from 'react-hot-toast';
import Toast from 'components/Toast';
import Toast from 'components/toasts/Toast';

const headers = [
{ propertyName: 'email', displayName: 'Email' },
Expand Down Expand Up @@ -101,9 +101,7 @@ const Settings = () => {
className="self-start"
onClick={async () =>
await deleteProject(selectedProject.data.id?.toString() ?? '', () =>
toast.custom(t => (
<Toast toast={t} customMessage="Project deleted successfully" />
)),
toast.success('Project deleted successfully'),
)
}
/>
Expand Down

0 comments on commit 7947fb2

Please sign in to comment.