Skip to content

Commit

Permalink
Refactor javascript shortcode
Browse files Browse the repository at this point in the history
- Switched from XMLHttpRequest to fetch API (more modern replacement).

- Used async syntax (eliminates the need for callbacks and .then()
  chains, marginal here but still good practice).

- Removed `onreadystatechange`: with Fetch API we can simply wait for
  the fetch promise to resolve.

- Simplified error handling by using a `try/catch` block.

- Removed redundant `var` declarations for `const` and `let` variable
  declarations (modern javascript).

- Extracted a helper function `updateModifiedDateElement` to reduce code
  duplication.

- Early `return` in the `if (commits.length === 0)` block to avoid
  executing the rest of the function.

- Removed unnecessary `else` clause since we're returning early in the
  `if (commits.length === 0)` block.
  • Loading branch information
Ziip-dev committed Aug 28, 2024
1 parent 9125c84 commit b721aa8
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions layouts/shortcodes/last_modified_lineage.html
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
<!-- Shortcode for GitHub Last Modified Date -->
<span id="last_modified_github"></span>
<script>
// Function to fetch the latest commit date from the GitHub repository using XMLHttpRequest
function fetchGitHubCommitDate() {
var xmlhttp = new XMLHttpRequest();
var githubApiUrl = "https://api.github.com/repos/MurrellGroup/lineages/commits?path=plots&page=1&per_page=1";

xmlhttp.onreadystatechange = function () {
if (this.readyState == 4) {
if (this.status == 200) {
var commits = JSON.parse(this.responseText);
if (commits.length > 0) {
// Extract the date of the latest commit
var latestCommitDate = new Date(commits[0].commit.committer.date);
// Format the date as YYYY-MM-DD
var formattedDate = latestCommitDate.toISOString().split('T')[0];
// Update the span with the last modified date
document.getElementById("last_modified_github").innerText = formattedDate;
} else {
document.getElementById("last_modified_github").innerText = "Not updated";
}
} else {
console.error("Error fetching GitHub commit date:", this.statusText);
document.getElementById("last_modified_github").innerText = "Error fetching date.";
}
}
};

xmlhttp.open("GET", githubApiUrl, true);
xmlhttp.send();
}
// Function to fetch the latest commit date from the GitHub repository using Fetch API
async function fetchGitHubCommitDate() {
const githubApiUrl = "https://api.github.com/repos/MurrellGroup/lineages/commits?path=plots&page=1&per_page=1";
try {
const response = await fetch(githubApiUrl);
// Check for successful response
if (!response.ok) {
throw new Error(`Error fetching GitHub commit date: ${response.statusText}`);
}
// Parse response data as JSON
const commits = await response.json();

// Check if there are any commits in the response data
if (commits.length === 0) {
updateModifiedDateElement("Not updated");
return;
}
// Extract the date of the latest commit and format as YYYY-MM-DD
const latestCommitDate = new Date(commits[0].commit.committer.date);
const formattedDate = latestCommitDate.toISOString().split('T')[0];
updateModifiedDateElement(formattedDate);

} catch (error) {
console.error(error);
updateModifiedDateElement("Error fetching date");
}
}

// Helper function to update the modified date element
function updateModifiedDateElement(dateText) {
document.getElementById("last_modified_github").innerText = dateText;
}

// Call the function to fetch the GitHub commit date
fetchGitHubCommitDate();

// Call the function to fetch the GitHub commit date
fetchGitHubCommitDate();
</script>

0 comments on commit b721aa8

Please sign in to comment.