Skip to content

Commit

Permalink
Added timeout for alert (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
taheeraahmed authored Jan 17, 2023
1 parent 1255640 commit a7ad870
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
16 changes: 15 additions & 1 deletion src/components/Alert/Alert.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC } from "react";
import React, { FC, useEffect } from "react";
import { Collapse, Alert as MUIAlert } from "@mui/material";
import styles from "./Alert.module.css";

Expand All @@ -10,6 +10,20 @@ interface Props {
}

const Alert: FC<Props> = ({ open, setOpen, message, severity }: Props) => {
useEffect(() => {
const timeId = setTimeout(() => {
setOpen(false);
}, 4000);

return () => {
clearTimeout(timeId);
};
}, []);

if (!open) {
return null;
}

return (
<Collapse in={open} className={styles.collapse}>
<div onClick={() => setOpen(false)}>
Expand Down
20 changes: 11 additions & 9 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, FC } from "react";
import React, { useState, FC, useEffect } from "react";
import { render } from "react-dom";
import "./index.css";
import AppRoutes from "./AppRoutes";
Expand All @@ -13,10 +13,11 @@ import { AlertContext, severityEnum } from "./contexts/AlertContext";
import Alert from "./components/Alert/Alert";

const Root: FC = () => {
// Hooks for the Alert component
const [open, setOpen] = useState(false);
const [message, setMessage] = useState("");
const [severity, setSeverity] = useState(severityEnum.INFO);

return (
<>
<Auth0Provider
Expand All @@ -35,13 +36,14 @@ const Root: FC = () => {
severity,
}}
>
<Alert
open={open}
setOpen={setOpen}
message={message}
severity={severity}
/>

{open ? (
<Alert
open={open}
setOpen={setOpen}
message={message}
severity={severity}
/>
) : null}
<div className={guistyles.container}>
<div className={"container"}>
<Router>
Expand Down

0 comments on commit a7ad870

Please sign in to comment.