From 0f4ea3507cf8d7ae1517b4bbfa50aeca24e9787a Mon Sep 17 00:00:00 2001 From: Defi Boy Date: Tue, 28 Jul 2020 08:29:54 +0100 Subject: [PATCH] added retry mechanism --- src/views/HomeView.tsx | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/src/views/HomeView.tsx b/src/views/HomeView.tsx index 18f052065d..8b67ba1bf8 100644 --- a/src/views/HomeView.tsx +++ b/src/views/HomeView.tsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from "react"; +import React, { useState, useEffect, useRef } from "react"; import { AppContext } from "../AppContext"; import { ContractName, Documentation } from "../types"; import { publish } from "../utils"; @@ -8,10 +8,18 @@ export const HomeView: React.FC = () => { const [activeItem, setActiveItem] = useState(""); const [isPublishing, setIsPublishing] = useState(false); const [htmlDocumentation, setHtmlDocumentation] = useState(""); + const [hasErrorOnPublishing, setHasErrorOnPublishing] = useState(false); + const clearMessageFuncRef = useRef(undefined as any); useEffect(() => { - async function publishDocumentation() { + let maxNumberOfRetries = 1; + let retries = 0; + + const publishDocumentation = async () => { try { + if (clearMessageFuncRef.current) { + clearTimeout(clearMessageFuncRef.current); + } const hash = await publish(htmlDocumentation); console.log("Hash", hash); setIsPublishing(false); @@ -20,11 +28,23 @@ export const HomeView: React.FC = () => { window.open(url); } catch (error) { - setIsPublishing(false); + if (retries < maxNumberOfRetries) { + console.log("Retrying...") + retries++; + publishDocumentation() + } else { + setIsPublishing(false); + setHasErrorOnPublishing(true) + + clearMessageFuncRef.current = setTimeout(() => { + setHasErrorOnPublishing(false) + }, 5000); + } } } if (isPublishing) { + setHasErrorOnPublishing(false) publishDocumentation(); } }, [isPublishing, htmlDocumentation]); @@ -89,7 +109,7 @@ export const HomeView: React.FC = () => { Clear -
+
{activeItem !== "" && ( { /> )}
+ + {hasErrorOnPublishing && +
+ +
}
)} @@ -127,14 +152,14 @@ export const PublishButton: React.FC = ({ {!isPublishing && "Publish"} {isPublishing && ( -
+
)}