Update search

This commit is contained in:
Ryan Prather 2024-05-16 01:25:08 -04:00
parent 15f486205d
commit f4d17bb7dc
4 changed files with 64 additions and 8 deletions

View File

@ -52,9 +52,50 @@ function setHeight() {
if ($('#noteDate')) {
$('#noteDate').datepicker();
}
if ($('#query')) {
document.querySelector('#query').addEventListener('keyup', function (event) {
if (event.key == "Enter") {
search();
}
});
}
setTimeout(saveNote, saveTimeout);
}
function search() {
query = document.querySelector('#query').value;
fetch('/index.php/search', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'query': query
})
})
.then(response => response.json())
.then(results => {
var oldNotes = document.querySelector('#old-notes');
oldNotes.innerHTML = '';
for (var n in results) {
var link = document.createElement('a');
link.href = '#';
link.setAttribute('onclick', "retrieveNote('" + results[n].id + "');openNote();");
link.innerHTML = results[n].title;
var p = document.createElement('p');
p.innerHTML = results[n].passage;
var article = document.createElement('article');
article.appendChild(link);
article.appendChild(p);
oldNotes.append(article);
}
});
}
function newNote() {
notes = document.querySelector('#notes');
notes.text = '';
@ -75,7 +116,6 @@ function newNote() {
document.querySelector('#passage').value = '';
document.querySelector('#noteId').value = uuidv4();
//document.querySelector('#noteSearch').style.display = 'none';
document.querySelector('#ref-list').innerHTML = '';
document.querySelector('#ref').innerHTML = '';
document.querySelector('.toggle').click();

View File

@ -239,12 +239,14 @@ class AjaxController extends AbstractController
return $res;
}
#[Route('/search-note', name: 'app_open_note')]
public function openNote(Request $req, EntityManagerInterface $emi): Response
#[Route('/search', name: 'app_search_note')]
public function searchNote(Request $req, EntityManagerInterface $emi): Response
{
/** @var User $user */
$user = $this->getUser();
$res = new Response();
$data = json_decode($req->getContent());
$note = $emi->getRepository(Note::class)->findNote($data->search);
$note = $emi->getRepository(Note::class)->findNote($data->query, $user->getId()->toBinary());
$res->setContent(json_encode($note));
return $res;

View File

@ -29,6 +29,22 @@ class NoteRepository extends ServiceEntityRepository
return $ret;
}
public function findNote(string $query, string $userId): array
{
$ret = $this->createQueryBuilder('n')
->orderBy('n.date', 'DESC')
->where('n.title LIKE :query')
->orWhere('n.passage LIKE :query')
->andWhere('n.user = :user')
->setParameter('query', "%{$query}%")
->setParameter('user', $userId)
->setMaxResults(3)
->getQuery()
->getResult();
return $ret;
}
// /**
// * @return Note[] Returns an array of Note objects
// */

View File

@ -5,9 +5,7 @@
<!-- Search -->
{% if app.user %}
<section id="search" class="alt">
<form method="post" action="#">
<input type="text" name="query" id="query" placeholder="Search" />
</form>
<input type="text" name="query" id="query" placeholder="Search" />
</section>
{% endif %}
@ -48,7 +46,7 @@
<header class="major">
<h2>Recent Notes</h2>
</header>
<div class="mini-posts">
<div class="mini-posts" id='old-notes'>
{% for n in app.user.notes %}
<article>
<a href="#" onclick="retrieveNote('{{ n.id }}');openNote();">{{ n.title }}</a>