Skip to content

Commit

Permalink
store state in url
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchelljkotler committed Aug 29, 2023
1 parent ecd6c88 commit e990f74
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ var userId;
var prevUrls = [];
const searchUrl = "https://api.www.documentcloud.org/api/documents/search/";

function loadHash() {
if (window.location.hash) {
var state = JSON.parse(decodeURIComponent(window.location.hash.slice(1)));
document.getElementById("query").value = state.query;
document.getElementById("attrs").value = state.attrs.join(", ");
document.getElementById("data").value = state.metadata.join(", ");
document.getElementById("sort").value = state.sort;
document.getElementById("group").checked = state.group;
document.getElementById("notes").checked = state.notes;
}
}

function getUserId() {
const url = "https://api.www.documentcloud.org/api/users/me/";
fetch(url, { credentials: "include"})
Expand All @@ -14,8 +26,11 @@ function getUserId() {
data.username;
userId = data.id;
var query = `user:${userId}`;
document.getElementById("query").value = query;
if (!document.getElementById("query").value) {
document.getElementById("query").value = query;
}
update();

}
});
} else {
Expand Down Expand Up @@ -143,7 +158,16 @@ function update() {
var sort = document.getElementById("sort").value.trim();
var group = document.getElementById("group").checked;
var notes = document.getElementById("notes").checked;
console.log("group", group);
var state = {
"query": query,
"attrs": attrs,
"metadata": metadata,
"sort": sort,
"group": group,
"notes": notes,
}
window.location = "#" + encodeURIComponent(JSON.stringify(state));

var dataUrl;
if (sort) {
dataUrl = `${searchUrl}?q=${query}&sort=data_${sort}`;
Expand All @@ -162,4 +186,5 @@ function update() {

document.getElementById("update").addEventListener("click", update);

loadHash();
getUserId();

0 comments on commit e990f74

Please sign in to comment.