Skip to content

Commit

Permalink
chore(ui): add missing toasts (#20)
Browse files Browse the repository at this point in the history
* chore: add toast for token expiry
* including a timeout before redirecting
* chore: add toasts for `project` components
* chore: cleanup
* chore: add toasts for `ProjectData` components
  • Loading branch information
HarshPatel5940 authored May 3, 2024
1 parent 593998c commit 5502f8f
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 13 deletions.
1 change: 0 additions & 1 deletion src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ interface NavbarProps {
}

export default function MyNavbar({ projectName }: NavbarProps) {
// TODO: Try using the new reditect() from react-router-dom
const navigate = useNavigate();
const [token] = useState<string | null>(parseCookies().userToken || null);

Expand Down
12 changes: 8 additions & 4 deletions src/components/projectData/AddImageDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export default function CreateProjectDataDialog({
}

if (!image) return;
const toastId = toast.info('Uploading Data...');

let res: AxiosResponse;

Expand All @@ -91,6 +92,11 @@ export default function CreateProjectDataDialog({
toast.warning(`Unexpected Response Code - ${res.status}`);
return;
}
const data = res.data;

data.key = res.data.title;

setProjectData(prev => [...prev, data]);
} catch (error) {
if (error instanceof AxiosError) {
if (error.response?.status === 400) {
Expand All @@ -112,13 +118,11 @@ export default function CreateProjectDataDialog({
setLoading(false);
return;
}
const data = res.data;

data.key = res.data.title;

setProjectData(prev => [...prev, data]);
setLoading(false);
setOpen(false);
toast.success('Data Added Successfully');
toast.dismiss(toastId);
return;
};

Expand Down
3 changes: 3 additions & 0 deletions src/components/projectData/DeleteImageDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export default function DeleteProjectDataDialog({
?.classList.add(...['border-red-500', 'border-2']);
return;
}
const toastId = toast.loading('Deleting Project Data...');

try {
const res = await server.delete(
Expand Down Expand Up @@ -94,6 +95,8 @@ export default function DeleteProjectDataDialog({

setLoading(false);
setOpen(false);
toast.success('Project Data Deleted Successfully');
toast.dismiss(toastId);
return;
};

Expand Down
5 changes: 4 additions & 1 deletion src/components/projectData/UpdateProjectDataDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default function UpdateProjectDataDialog({
?.classList.add(...['border-red-500', 'border-2']);
return;
}

const toastId = toast.loading('Updating Image Data...');
let res: AxiosResponse;

try {
Expand Down Expand Up @@ -116,6 +116,9 @@ export default function UpdateProjectDataDialog({
}

setOpen(false);
toast.success('Image Data Updated Successfully');
toast.dismiss(toastId);

return;
};

Expand Down
5 changes: 5 additions & 0 deletions src/components/projects/CreateProjectDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default function CreateProjectDialog({
}

try {
const toastId = toast.loading('Creating Project...');
const res = await server.post(
'/api/project/new',
{
Expand All @@ -74,6 +75,10 @@ export default function CreateProjectDialog({
const data = res.data;
data.ProjectData = [];
setProjects(prev => [...prev, data]);
toast.success('Project Created Successfully', {
description: `Project Name: ${projectName}`,
});
toast.dismiss(toastId);
} catch (error) {
if (error instanceof AxiosError) {
if (error.response?.status === 400) {
Expand Down
3 changes: 3 additions & 0 deletions src/components/projects/DeleteProjectDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default function DeleteProjectDialog({

const handleSubmit = async () => {
setLoading(true);
const toastId = toast.loading('Deleting Project...');
if (deleteText !== 'delete') {
setLoading(false);
toast.warning('Please write "delete" to confirm');
Expand All @@ -72,6 +73,8 @@ export default function DeleteProjectDialog({
setProjects?.(prev =>
prev.filter((project: Project) => project.slug !== projectSlug)
);
toast.success('Project Deleted Successfully');
toast.dismiss(toastId);
} catch (error) {
if (error instanceof AxiosError) {
if (error.response?.status === 400) {
Expand Down
4 changes: 3 additions & 1 deletion src/components/projects/TokenResetDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export default function ResetProjectToken({

if (deleteText !== 'confirm') {
setLoading(false);
toast.warning('Please write "confirm" to confirm');
document.getElementById('projectName')?.focus();
document
.getElementById('projectName')
Expand All @@ -55,6 +54,7 @@ export default function ResetProjectToken({
}

try {
const toastId = toast.warning('Please write "confirm" to confirm');
const res = await server.post(
`/api/project/${projectSlug}/token`,
{},
Expand All @@ -73,6 +73,8 @@ export default function ResetProjectToken({

const data = res.data.projectToken;
setProjectToken(data);
toast.success('Token Reset Successfully');
toast.dismiss(toastId);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error) {
Expand Down
8 changes: 6 additions & 2 deletions src/pages/project/manageProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,12 @@ export default function ManageProjectsPage() {
if (error instanceof AxiosError) {
if (error.response?.status === 400) {
destroyCookie(null, 'userToken');
navigate('/login');
toast.warning('Please Login to Continue');

toast.warning('Token Expired. Logging Out!');
setTimeout(() => {
navigate('/login');
}, 3000);

return;
}
if (error.response?.status === 401) {
Expand Down
6 changes: 4 additions & 2 deletions src/pages/project/manageProjectData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ export default function ManageProjectDataPage() {
if (error instanceof AxiosError) {
if (error.response?.status === 400) {
destroyCookie(null, 'userToken');
navigate('/login');
toast.warning('Please Login to Continue');
toast.warning('Token Expired. Logging Out!');
setTimeout(() => {
navigate('/login');
}, 3000);
return;
}
if (error.response?.status === 401) {
Expand Down
6 changes: 4 additions & 2 deletions src/pages/project/projectsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ export default function DashboardPage() {
if (error instanceof AxiosError) {
if (error.response?.status === 400) {
destroyCookie(null, 'userToken');
navigate('/login');
toast.warning('Please Login to Continue');
toast.warning('Token Expired. Logging Out!');
setTimeout(() => {
navigate('/login');
}, 3000);
return;
}
if (error.response?.status === 401) {
Expand Down

0 comments on commit 5502f8f

Please sign in to comment.