Skip to content

Commit

Permalink
fixes an issue in search where data results is parsed incorrectly
Browse files Browse the repository at this point in the history
  • Loading branch information
Chi Kim committed Dec 6, 2020
1 parent 1b344f3 commit e28e54d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 56 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ data
public/assets
logs
public/main.js
node_modules
.git
116 changes: 60 additions & 56 deletions src/search.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,54 @@
let resultContainer;
let searchBox;
let allNotes;
let resultElements = []
let noteMap = {}
let resultElements = [];
let noteMap = {};

const search = () => {
const searchQuery = searchBox.value
const searchQuery = searchBox.value;
if (searchQuery.length < 2) {
return resultContainer.innerHTML = ''
return (resultContainer.innerHTML = "");
}
fetch(`/search/${searchQuery}`).then( r=> r.text() ).then(data => {
// invalidate old requests
if( searchBox.value !== searchQuery) return
const dataArr = data.split('\n').filter( d => d ).map( d => {
const result = d.substr(5) // get rid of data/ prefix
const resultArr = result.split(':')
const title = resultArr.shift()
const match = resultArr.join(':')
return {
title, match
}
})
resultContainer.innerHTML = ''
const newTitleMap = {...noteMap}
resultElements = dataArr.map( (obj, idx) => {
delete newTitleMap[obj.title.toLowerCase()]
return new Result(obj.title, obj.match, idx)
})
Object.keys(newTitleMap).forEach( title => {
if (!title.includes(searchQuery.toLowerCase())) {
return
}
resultElements.push(
new Result(newTitleMap[title], '', resultElements.length)
)
})
})
}
fetch(`/search/${searchQuery}`)
.then((r) => r.text())
.then((data) => {
// invalidate old requests
if (searchBox.value !== searchQuery) return;
const dataArr = data
.split("\n")
.filter((d) => d)
.map((d) => {
const result = d.replace("./data/", ""); // get rid of ./data/ prefix
const resultArr = result.split(":");
const title = resultArr.shift();
const match = resultArr.join(":");
return {
title,
match,
};
});
resultContainer.innerHTML = "";
const newTitleMap = { ...noteMap };
resultElements = dataArr.map((obj, idx) => {
delete newTitleMap[obj.title.toLowerCase()];
return new Result(obj.title, obj.match, idx);
});
Object.keys(newTitleMap).forEach((title) => {
if (!title.includes(searchQuery.toLowerCase())) {
return;
}
resultElements.push(
new Result(newTitleMap[title], "", resultElements.length)
);
});
});
};

function Result(name, value, idx) {
const div = document.createElement('div')
div.className = 'resultContainer'
const div = document.createElement("div");
div.className = "resultContainer";
if (!idx) {
div.className += ' selected'
div.className += " selected";
}
div.innerHTML = `
<p>
Expand All @@ -51,31 +57,29 @@ function Result(name, value, idx) {
</span>
${value}
</p>
`
div.addEventListener('click', () => {
this.select()
})
`;
div.addEventListener("click", () => {
this.select();
});

resultContainer.append(div)
resultContainer.append(div);
this.select = () => {
window.location = `/notes/${name}`
}
window.location = `/notes/${name}`;
};
}

const initSearch = (titleMap) => {
resultContainer = document.querySelector('#resultListContainer')
searchBox = document.querySelector('#searchBox')
allNotes = document.querySelectorAll('.card-columns .card-title')
searchBox.focus()
noteMap = titleMap
searchBox.addEventListener('keyup', (e) => {
console.log(e.key)
if (e.key === 'Enter') {
return resultElements[0].select()
resultContainer = document.querySelector("#resultListContainer");
searchBox = document.querySelector("#searchBox");
allNotes = document.querySelectorAll(".card-columns .card-title");
searchBox.focus();
noteMap = titleMap;
searchBox.addEventListener("keyup", (e) => {
if (e.key === "Enter") {
return resultElements[0].select();
}
search()
})

}
search();
});
};

module.exports = initSearch
module.exports = initSearch;

0 comments on commit e28e54d

Please sign in to comment.