Skip to content

Commit

Permalink
fixed issue with error timer
Browse files Browse the repository at this point in the history
  • Loading branch information
Megha-Dev-19 committed Jan 23, 2025
1 parent 332cbc5 commit b515e1d
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ useEffect(() => {
setShowErrorToast(true);
setTxnCreated(false);
clearTimeout(checkTxnTimeout);
}, 20000);
}, 25_000);

return () => {
clearTimeout(checkTxnTimeout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ useEffect(() => {

const checkForNewProposal = () => {
getLastProposalId().then((id) => {
if (lastProposalId !== id) {
if (typeof lastProposalId === "number" && lastProposalId !== id) {
cleanInputs();
onCloseCanvas();
clearTimeout(errorTimeout);
Expand All @@ -180,14 +180,14 @@ useEffect(() => {
setShowErrorToast(true);
setTxnCreated(false);
clearTimeout(checkTxnTimeout);
}, 20000);
}, 25_000);

return () => {
clearTimeout(checkTxnTimeout);
clearTimeout(errorTimeout);
};
}
}, [isTxnCreated]);
}, [isTxnCreated, lastProposalId]);

useEffect(() => {
const handler = setTimeout(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ useEffect(() => {

const checkForNewProposal = () => {
getLastProposalId().then((id) => {
if (lastProposalId !== id) {
if (typeof lastProposalId === "number" && lastProposalId !== id) {
setToastStatus(true);
setTxnCreated(false);
clearTimeout(errorTimeout);
Expand All @@ -63,7 +63,7 @@ useEffect(() => {
setShowErrorToast(true);
setTxnCreated(false);
clearTimeout(checkTxnTimeout);
}, 20000);
}, 25_000);

return () => {
clearTimeout(checkTxnTimeout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ useEffect(() => {

const checkForNewProposal = () => {
getLastProposalId().then((id) => {
if (lastProposalId !== id) {
if (typeof lastProposalId === "number" && lastProposalId !== id) {
setToastStatus(true);
onCloseCanvas();
clearTimeout(errorTimeout);
Expand All @@ -74,14 +74,14 @@ useEffect(() => {
setShowErrorToast(true);
setTxnCreated(false);
clearTimeout(checkTxnTimeout);
}, 20000);
}, 25_000);

return () => {
clearTimeout(checkTxnTimeout);
clearTimeout(errorTimeout);
};
}
}, [isTxnCreated]);
}, [isTxnCreated, lastProposalId]);

function updateDaoPolicy(rolesMap) {
const updatedPolicy = { ...daoPolicy };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ useEffect(() => {

const checkForNewProposal = () => {
getLastProposalId().then((id) => {
if (lastProposalId !== id) {
if (typeof lastProposalId === "number" && lastProposalId !== id) {
setToastStatus(true);
setTxnCreated(false);
clearTimeout(errorTimeout);
Expand All @@ -284,14 +284,14 @@ useEffect(() => {
setShowErrorToast(true);
setTxnCreated(false);
clearTimeout(checkTxnTimeout);
}, 20000);
}, 25_000);

return () => {
clearTimeout(checkTxnTimeout);
clearTimeout(errorTimeout);
};
}
}, [isTxnCreated]);
}, [isTxnCreated, lastProposalId]);

function toBase64(json) {
return Buffer.from(JSON.stringify(json)).toString("base64");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ useEffect(() => {

const checkForNewProposal = () => {
getLastProposalId().then((id) => {
if (lastProposalId !== id) {
if (typeof lastProposalId === "number" && lastProposalId !== id) {
setToastStatus(true);
setTxnCreated(false);
clearTimeout(errorTimeout);
Expand All @@ -112,14 +112,14 @@ useEffect(() => {
setShowErrorToast(true);
setTxnCreated(false);
clearTimeout(checkTxnTimeout);
}, 20000);
}, 25_000);

return () => {
clearTimeout(checkTxnTimeout);
clearTimeout(errorTimeout);
};
}
}, [isTxnCreated]);
}, [isTxnCreated, lastProposalId]);

function resetForm() {
setSelectedVoteOption(selectedGroup.isRatio ? options[1] : options[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,25 @@ const daoPolicy = treasuryDaoID
? Near.view(treasuryDaoID, "get_policy", {})
: null;

const lastProposalId = Near.view(treasuryDaoID, "get_last_proposal_id");

const hasCreatePermission = hasPermission(
treasuryDaoID,
context.accountId,
"policy_update_parameters",
"AddProposal"
);

const [lastProposalId, setLastProposalId] = useState(null);

function getLastProposalId() {
return Near.asyncView(treasuryDaoID, "get_last_proposal_id").then(
(result) => result
);
}

useEffect(() => {
getLastProposalId().then((i) => setLastProposalId(i));
}, []);

if (!daoPolicy || lastProposalId === null) {
return (
<div className="card d-flex justify-content-center align-items-center w-100 h-100">
Expand All @@ -61,7 +71,7 @@ const [proposalsThatWillExpire, setProposalsThatWillExpire] = useState([]);
const [proposalsThatWillBeActive, setProposalsThatWillBeActive] = useState([]);
const [otherPendingRequests, setOtherPendingRequests] = useState([]);
const [showToastStatus, setToastStatus] = useState(null);
const [isSubmittingChangeRequest, setSubmittingChangeRequest] = useState(false);
const [isTxnCreated, setTxnCreated] = useState(false);
const [showAffectedProposalsModal, setShowAffectedProposalsModal] =
useState(false);

Expand Down Expand Up @@ -223,8 +233,9 @@ const findAffectedProposals = (callback) => {
};

function submitVotePolicyChangeTxn() {
setLoading(false);
setShowAffectedProposalsModal(false);
setSubmittingChangeRequest(true);
setTxnCreated(true);
const description = {
title: "Update policy - Voting Duration",
summary: `${context.accountId} requested to change voting duration from ${currentDurationDays} to ${durationDays}.`,
Expand Down Expand Up @@ -265,35 +276,36 @@ const submitChangeRequest = () => {
};

useEffect(() => {
if (isSubmittingChangeRequest) {
if (isTxnCreated) {
let checkTxnTimeout = null;
let errorTimeout = null;
Near.asyncView(treasuryDaoID, "get_proposal", {
id: lastProposalId - 1,
}).then((proposal) => {
const proposal_period =
proposal?.kind?.ChangePolicyUpdateParameters?.parameters
?.proposal_period;

if (
proposal_period &&
isSubmittingChangeRequest &&
Number(proposal_period.substring(0, proposal_period.length - 9)) /
(24 * 60 * 60) ===
Number(durationDays)
) {
setToastStatus(true);
setSubmittingChangeRequest(false);
clearTimeout(errorTimeout);
}
});
const checkForNewProposal = () => {
getLastProposalId().then((id) => {
if (typeof lastProposalId === "number" && lastProposalId === id) {
setToastStatus(true);
setTxnCreated(false);
clearTimeout(errorTimeout);
} else {
checkTxnTimeout = setTimeout(() => checkForNewProposal(), 1000);
}
});
};
checkForNewProposal();

// if in 20 seconds there is no change, show error condition
errorTimeout = setTimeout(() => {
setShowErrorToast(true);
setSubmittingChangeRequest(false);
}, 20000);
setTxnCreated(false);
clearTimeout(checkTxnTimeout);
}, 25_000);

return () => {
clearTimeout(checkTxnTimeout);
clearTimeout(errorTimeout);
};
}
}, [isSubmittingChangeRequest, lastProposalId]);
}, [isTxnCreated, lastProposalId]);

const changeDurationDays = (newDurationDays) => {
setDurationDays(newDurationDays);
Expand All @@ -305,7 +317,7 @@ const showImpactedRequests =
return (
<Container>
<TransactionLoader
showInProgress={isSubmittingChangeRequest}
showInProgress={isTxnCreated}
showError={showErrorToast}
toggleToast={() => setShowErrorToast(false)}
/>
Expand Down Expand Up @@ -492,12 +504,12 @@ return (
props={{
classNames: { root: "theme-btn" },
label: "Submit Request",
loading: showLoader || isSubmittingChangeRequest,
loading: showLoader || isTxnCreated,
disabled:
durationDays === currentDurationDays ||
showLoader ||
!hasCreatePermission ||
isSubmittingChangeRequest,
isTxnCreated,
}}
/>
),
Expand All @@ -506,7 +518,7 @@ return (
durationDays === currentDurationDays ||
showLoader ||
!hasCreatePermission ||
isSubmittingChangeRequest,
isTxnCreated,
treasuryDaoID,
callbackAction: submitChangeRequest,
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ useEffect(() => {

const checkForNewProposal = () => {
getLastProposalId().then((id) => {
if (lastProposalId !== id) {
if (typeof lastProposalId === "number" && lastProposalId !== id) {
cleanInputs();
onCloseCanvas();
refreshData();
Expand All @@ -153,14 +153,14 @@ useEffect(() => {
setShowErrorToast(true);
setTxnCreated(false);
clearTimeout(checkTxnTimeout);
}, 20000);
}, 25_000);

return () => {
clearTimeout(checkTxnTimeout);
clearTimeout(errorTimeout);
};
}
}, [isTxnCreated]);
}, [isTxnCreated, lastProposalId]);

const BalanceDisplay = ({ label, balance, tooltipInfo, noBorder }) => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ useEffect(() => {

const checkForNewProposal = () => {
getLastProposalId().then((id) => {
if (lastProposalId !== id) {
if (typeof lastProposalId === "number" && lastProposalId !== id) {
cleanInputs();
onCloseCanvas();
refreshData();
Expand All @@ -211,14 +211,14 @@ useEffect(() => {
setShowErrorToast(true);
setTxnCreated(false);
clearTimeout(checkTxnTimeout);
}, 20000);
}, 25_000);

return () => {
clearTimeout(checkTxnTimeout);
clearTimeout(errorTimeout);
};
}
}, [isTxnCreated]);
}, [isTxnCreated, lastProposalId]);

const BalanceDisplay = ({ label, balance, tooltipInfo, noBorder }) => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ useEffect(() => {

const checkForNewProposal = () => {
getLastProposalId().then((id) => {
if (lastProposalId !== id) {
if (typeof lastProposalId === "number" && lastProposalId !== id) {
cleanInputs();
onCloseCanvas();
refreshData();
Expand All @@ -141,14 +141,14 @@ useEffect(() => {
setShowErrorToast(true);
setTxnCreated(false);
clearTimeout(checkTxnTimeout);
}, 20000);
}, 25_000);

return () => {
clearTimeout(checkTxnTimeout);
clearTimeout(errorTimeout);
};
}
}, [isTxnCreated]);
}, [isTxnCreated, lastProposalId]);

const BalanceDisplay = ({ label, balance, tooltipInfo, noBorder }) => {
return (
Expand Down
5 changes: 3 additions & 2 deletions playwright-tests/tests/settings/voting-duration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ test.describe("User is logged in", function () {
.fill(newDurationDays.toString());

await page.waitForTimeout(500);
await page.locator("button", { hasText: "Submit" }).click();
const submitBtn = page.locator("button", { hasText: "Submit" });
await submitBtn.click();

await page
.locator(".modalfooter button", { hasText: "Yes, proceed" })
Expand Down Expand Up @@ -227,7 +228,7 @@ test.describe("User is logged in", function () {
await expect(
page.getByText("Voting duration change request submitted")
).toBeVisible();

await expect(submitBtn).toBeEnabled();
await sandbox.quitSandbox();
});

Expand Down

0 comments on commit b515e1d

Please sign in to comment.