Skip to content

Commit

Permalink
Merge pull request #114 from YotaYota/fix.refresh-and-dry-run-first-c…
Browse files Browse the repository at this point in the history
…lick

Move dry run to useEffect
  • Loading branch information
indy-independence committed Jun 13, 2024
2 parents cd31941 + 4f2c014 commit bb31e8f
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions public/components/ConfigChange/ConfigChangeStep1.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import React, { useEffect, useState } from "react";
import { Popup, Icon } from "semantic-ui-react";
import { Icon, Popup } from "semantic-ui-react";
import { getData } from "../../utils/getData";
import { putData } from "../../utils/sendData";
import permissionsCheck from "../../utils/permissions/permissionsCheck";
import { putData } from "../../utils/sendData";

function ConfigChangeStep1({ setRepoWorking, dryRunJobStatus, onDryRunReady }) {
const [commitInfo, setCommitInfo] = useState({});
const [commitUpdateInfo, setCommitUpdateInfo] = useState({
settings: null,
templates: null,
});
const [triggerDryRun, setTriggerDryRun] = useState(false);
const [expanded, setExpanded] = useState(true);
const [buttonsDisabled, setButtonsDisabled] = useState(false);

Expand All @@ -34,6 +35,21 @@ function ConfigChangeStep1({ setRepoWorking, dryRunJobStatus, onDryRunReady }) {
getRepoStatus("templates");
}, []);

useEffect(() => {
if (!triggerDryRun) {
return;
}
if (commitUpdateInfo.settings === "success") {
onDryRunReady();
} else {
console.log(
`Refresh error occured. Status${JSON.stringify(commitUpdateInfo)}`,
);
}

setTriggerDryRun(false);
}, [commitUpdateInfo, onDryRunReady, triggerDryRun]);

// this request takes some time, perhaps work in a "loading..."
async function refreshRepo(repoName) {
setCommitUpdateInfo((prev) => ({ ...prev, [repoName]: "updating..." }));
Expand Down Expand Up @@ -63,15 +79,8 @@ function ConfigChangeStep1({ setRepoWorking, dryRunJobStatus, onDryRunReady }) {
});
}

async function refreshRepoAndDryRun(repoName) {
await refreshRepo(repoName);
if (commitUpdateInfo.settings === "success") {
onDryRunReady();
} else {
console.log(
`Refresh error occured. Status${JSON.stringify(commitUpdateInfo)}`,
);
}
function handleRefreshAndDryRun(repoName) {
refreshRepo(repoName).then(() => setTriggerDryRun(true));
}

function prettifyCommit(commitStr) {
Expand Down Expand Up @@ -137,7 +146,7 @@ function ConfigChangeStep1({ setRepoWorking, dryRunJobStatus, onDryRunReady }) {
type="button"
hidden={!permissionsCheck("Config change", "write")}
disabled={buttonsDisabled}
onClick={() => refreshRepoAndDryRun("settings")}
onClick={() => handleRefreshAndDryRun("settings")}
>
Refresh settings + dry run
</button>
Expand Down

0 comments on commit bb31e8f

Please sign in to comment.