Skip to content

Commit

Permalink
chore: set the latest sha as initial value of commit input
Browse files Browse the repository at this point in the history
  • Loading branch information
0xExp-po committed Sep 12, 2024
1 parent f83159d commit 6b79544
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
50 changes: 35 additions & 15 deletions dapp/src/components/Commit.astro
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import PrimaryButton from "./utils/PrimaryButton.astro";
import { commitHash } from "../service/WriteContractService";
import { loadProjectInfo } from "../service/StateService";
import { loadedPublicKey } from "./stellar-wallets-kit";
import { latestCommit } from "../utils/store";

document.addEventListener("astro:page-load", () => {
function updateCommitSectionVisibility() {
Expand All @@ -67,6 +68,13 @@ import PrimaryButton from "./utils/PrimaryButton.astro";
"commit_hash",
) as HTMLInputElement;

// Subscribe to latestCommit changes
latestCommit.subscribe((value) => {
if (commit_hash) {
commit_hash.value = value;
}
});

const wrap_loader = document.getElementById(
"wrap-commit-loader",
) as HTMLDivElement;
Expand All @@ -77,24 +85,36 @@ import PrimaryButton from "./utils/PrimaryButton.astro";
"[data-commit-hash]",
) as HTMLButtonElement;

async function handleCommit() {
wrap_loader.style.display = "block";
wrap_button.style.display = "none";
try {
const commit_status = await commitHash(commit_hash.value);

wrap_loader.style.display = "none";
wrap_button.style.display = "block";
if (!commit_status) {
alert("Hash could not be committed! Please retry.");
} else {
wrap_button.innerHTML = "Hash committed!";
}
} catch (e) {
console.error(e);
}
}

if (button) {
button.addEventListener("click", async () => {
wrap_loader.style.display = "block";
wrap_button.style.display = "none";
try {
const commit_status = await commitHash(commit_hash.value);
button.addEventListener("click", handleCommit);
}

wrap_loader.style.display = "none";
wrap_button.style.display = "block";
if (!commit_status) {
alert("Hash could not be committed! Please retry.");
} else {
wrap_button.innerHTML = "Hash committed!";
}
} catch (e) {
console.error(e);
if (commit_hash) {
commit_hash.addEventListener("keypress", (event) => {
if (event.key === "Enter") {
event.preventDefault();
handleCommit();
}
})};
});
}

// Add event listener for wallet connection
window.addEventListener('walletConnected', updateCommitSectionVisibility);
Expand Down
7 changes: 6 additions & 1 deletion dapp/src/components/CommitHistory.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getCommitHistory } from '../service/GithubService';
import CommitRecord from './CommitRecord.jsx';
import { formatDate } from '../service/utils';
import { loadProjectRepoInfo, loadConfigData } from '../service/StateService';
import { projectInfoLoaded } from '../utils/store.js';
import { projectInfoLoaded, latestCommit } from '../utils/store.js';
import { useStore } from '@nanostores/react';

const CommitHistory = () => {
Expand All @@ -17,6 +17,11 @@ const CommitHistory = () => {
if (projectRepoInfo?.author && projectRepoInfo?.repository) {
const history = await getCommitHistory(projectRepoInfo.author, projectRepoInfo.repository);
setCommitHistory(history);

// Set the latest commit
if (history.length > 0 && history[0].commits.length > 0) {
latestCommit.set(history[0].commits[0].sha);
}
}
};

Expand Down
3 changes: 2 additions & 1 deletion dapp/src/utils/store.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { atom } from 'nanostores'

export const projectInfoLoaded = atom(false);
export const projectInfoLoaded = atom(false);
export const latestCommit = atom("");

0 comments on commit 6b79544

Please sign in to comment.