-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
137 lines (114 loc) · 5.1 KB
/
script.js
File metadata and controls
137 lines (114 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
document.addEventListener("DOMContentLoaded", function() {
const resultsDiv = document.getElementById("results");
const loadingDiv = document.getElementById("loading");
const searchInput = document.getElementById("search-input");
const searchButton = document.getElementById("search-button");
const loadMoreButton = document.getElementById("load-more-button");
const startDateInput = document.getElementById("start-date");
const endDateInput = document.getElementById("end-date");
let allResults = [];
let displayedCount = 0;
const resultsPerPage = 5;
function getDefaultDate(daysAgo) {
const date = new Date();
date.setDate(date.getDate() - daysAgo);
return date.toISOString().split("T")[0]; // Format YYYY-MM-DD
}
function fetchResults(searchTerm = "data") {
resultsDiv.innerHTML = "";
loadingDiv.style.display = "block";
loadMoreButton.style.display = "none";
let startDate = startDateInput.value || getDefaultDate(30); // Default: Last 30 days
let endDate = endDateInput.value || getDefaultDate(0); // Default: Today
if (new Date(startDate) > new Date(endDate)) {
alert("⚠️ The start date must be before the end date.");
loadingDiv.style.display = "none";
return;
}
const url = "https://www.federalregister.gov/api/v1/documents.json";
const params = new URLSearchParams({
"conditions[term]": searchTerm,
"conditions[publication_date][gte]": startDate,
"conditions[publication_date][lte]": endDate,
"per_page": 100
});
fetch(`${url}?${params}`)
.then(response => response.json())
.then(data => {
console.log("API Response:", data);
allResults = data.results;
displayedCount = 0;
if (allResults.length === 0) {
resultsDiv.innerHTML = "<p>No results found.</p>";
loadingDiv.style.display = "none";
return;
}
// ✅ Sort results by `publication_date` (newest first)
allResults = data.results.sort((a, b) => new Date(b.publication_date) - new Date(a.publication_date));
displayedCount = 0;
loadingDiv.style.display = "none";
displayNextResults();
if (allResults.length > resultsPerPage) {
loadMoreButton.style.display = "block";
}
})
.catch(error => {
console.error("Error fetching data:", error);
loadingDiv.innerHTML = `<p style="color:red;">⚠️ Error loading data. Try refreshing.</p>`;
});
}
function displayNextResults() {
const slice = allResults.slice(displayedCount, displayedCount + resultsPerPage);
slice.forEach(doc => {
const entry = document.createElement("div");
entry.classList.add("entry");
const title = doc.title || "No Title Available";
const publicationDate = doc.publication_date || "Unknown Date";
let agencies = "Independent Publication";
if (doc.agency_names && doc.agency_names.length > 0) {
agencies = doc.agency_names.join(", ");
} else if (doc.agencies && doc.agencies.length > 0) {
agencies = doc.agencies.map(a => a.name).join(", ");
}
let fullTextUrl = doc.html_url || doc.full_text_xml_url || doc.pdf_url || null;
if (!fullTextUrl && doc.document_number) {
fullTextUrl = `https://www.federalregister.gov/documents/${doc.publication_date}/${doc.document_number}`;
}
const linkDisplay = fullTextUrl
? `<a href="${fullTextUrl}" target="_blank" rel="noopener noreferrer">Read Full Text</a>`
: `<span style="color: gray;">No Link Available</span>`;
entry.innerHTML = `
<div class="title">${title}</div>
<div class="date"><strong>Published:</strong> ${publicationDate}</div>
<div class="agency"><strong>Agency:</strong> ${agencies}</div>
<div class="link">${linkDisplay}</div>
`;
resultsDiv.appendChild(entry);
});
displayedCount += resultsPerPage;
if (displayedCount >= allResults.length) {
loadMoreButton.style.display = "none";
}
}
// Initial load with default term
fetchResults();
// Handle search button click
searchButton.addEventListener("click", function() {
const searchTerm = searchInput.value.trim();
if (searchTerm === "") {
alert("Please enter a search term!");
return;
}
fetchResults(searchTerm);
});
// Enable "Enter" key search
searchInput.addEventListener("keypress", function(event) {
if (event.key === "Enter") {
searchButton.click();
}
});
// Load more results when button is clicked
loadMoreButton.addEventListener("click", function() {
displayNextResults();
});
});