added retry mechanism

pull/3475/head
Defi Boy 5 years ago committed by Aniket
parent af96b3f29c
commit 1d65101716
  1. 37
      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 { AppContext } from "../AppContext";
import { ContractName, Documentation } from "../types"; import { ContractName, Documentation } from "../types";
import { publish } from "../utils"; import { publish } from "../utils";
@ -8,10 +8,18 @@ export const HomeView: React.FC = () => {
const [activeItem, setActiveItem] = useState(""); const [activeItem, setActiveItem] = useState("");
const [isPublishing, setIsPublishing] = useState(false); const [isPublishing, setIsPublishing] = useState(false);
const [htmlDocumentation, setHtmlDocumentation] = useState(""); const [htmlDocumentation, setHtmlDocumentation] = useState("");
const [hasErrorOnPublishing, setHasErrorOnPublishing] = useState(false);
const clearMessageFuncRef = useRef(undefined as any);
useEffect(() => { useEffect(() => {
async function publishDocumentation() { let maxNumberOfRetries = 1;
let retries = 0;
const publishDocumentation = async () => {
try { try {
if (clearMessageFuncRef.current) {
clearTimeout(clearMessageFuncRef.current);
}
const hash = await publish(htmlDocumentation); const hash = await publish(htmlDocumentation);
console.log("Hash", hash); console.log("Hash", hash);
setIsPublishing(false); setIsPublishing(false);
@ -20,11 +28,23 @@ export const HomeView: React.FC = () => {
window.open(url); window.open(url);
} catch (error) { } catch (error) {
if (retries < maxNumberOfRetries) {
console.log("Retrying...")
retries++;
publishDocumentation()
} else {
setIsPublishing(false); setIsPublishing(false);
setHasErrorOnPublishing(true)
clearMessageFuncRef.current = setTimeout(() => {
setHasErrorOnPublishing(false)
}, 5000);
}
} }
} }
if (isPublishing) { if (isPublishing) {
setHasErrorOnPublishing(false)
publishDocumentation(); publishDocumentation();
} }
}, [isPublishing, htmlDocumentation]); }, [isPublishing, htmlDocumentation]);
@ -89,7 +109,7 @@ export const HomeView: React.FC = () => {
Clear Clear
</button> </button>
</div> </div>
<div style={{ width: "15em" }}> <div style={{ width: "16em" }}>
{activeItem !== "" && ( {activeItem !== "" && (
<PublishButton <PublishButton
isPublishing={isPublishing} isPublishing={isPublishing}
@ -100,6 +120,11 @@ export const HomeView: React.FC = () => {
/> />
)} )}
</div> </div>
{hasErrorOnPublishing &&
<div>
<label>Something unexpected happen, Please try again</label>
</div>}
</div> </div>
)} )}
</div> </div>
@ -127,14 +152,14 @@ export const PublishButton: React.FC<PublishButtonProps> = ({
{!isPublishing && "Publish"} {!isPublishing && "Publish"}
{isPublishing && ( {isPublishing && (
<div> <div style={{ display: "flex", justifyContent: "center", alignItems: "center" }}>
<span <span
className="spinner-border spinner-border-sm" className="spinner-border spinner-border-sm"
role="status" role="status"
aria-hidden="true" aria-hidden="true"
style={{ marginRight: "0.3em" }} style={{ marginRight: "0.5em" }}
/> />
Publishing...Please wait <p style={{ margin: "0" }}>Publishing...Please wait</p>
</div> </div>
)} )}
</button> </button>

Loading…
Cancel
Save