Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
+62
@@ -0,0 +1,62 @@
|
||||
import { useNavigation, useSubmit } from "react-router";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import clsx from "clsx";
|
||||
|
||||
interface Props {
|
||||
status: string;
|
||||
statusToPoll: string;
|
||||
intervalInSeconds?: number;
|
||||
className?: string;
|
||||
}
|
||||
export default function PollButton({ status, statusToPoll, intervalInSeconds = 10, className }: Props) {
|
||||
const navigation = useNavigation();
|
||||
const submit = useSubmit();
|
||||
const { t } = useTranslation();
|
||||
|
||||
const [countdown, setCountdown] = useState(intervalInSeconds);
|
||||
|
||||
useEffect(() => {
|
||||
let countdownIntervalId: NodeJS.Timeout;
|
||||
let refreshIntervalId: NodeJS.Timeout;
|
||||
|
||||
if (status === statusToPoll) {
|
||||
setCountdown(intervalInSeconds);
|
||||
countdownIntervalId = setInterval(() => {
|
||||
setCountdown((currentCountdown) => (currentCountdown > 1 ? currentCountdown - 1 : intervalInSeconds));
|
||||
}, 1000);
|
||||
|
||||
refreshIntervalId = setInterval(() => {
|
||||
submit(null, {
|
||||
method: "get",
|
||||
});
|
||||
}, intervalInSeconds * 1000);
|
||||
}
|
||||
|
||||
return () => {
|
||||
if (countdownIntervalId) {
|
||||
clearInterval(countdownIntervalId);
|
||||
}
|
||||
if (refreshIntervalId) {
|
||||
clearInterval(refreshIntervalId);
|
||||
}
|
||||
};
|
||||
}, [intervalInSeconds, status, statusToPoll, submit]);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<button
|
||||
type="button"
|
||||
className={clsx("underline", className)}
|
||||
onClick={() => {
|
||||
submit(null, {
|
||||
method: "get",
|
||||
});
|
||||
setCountdown(intervalInSeconds);
|
||||
}}
|
||||
>
|
||||
{navigation.state !== "idle" ? t("shared.refreshing") : <span>{t("shared.refreshingIn", { seconds: countdown })}</span>}
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user