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')) { if ($('#noteDate')) {
$('#noteDate').datepicker(); $('#noteDate').datepicker();
} }
if ($('#query')) {
document.querySelector('#query').addEventListener('keyup', function (event) {
if (event.key == "Enter") {
search();
}
});
}
setTimeout(saveNote, saveTimeout); 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() { function newNote() {
notes = document.querySelector('#notes'); notes = document.querySelector('#notes');
notes.text = ''; notes.text = '';
@ -75,7 +116,6 @@ function newNote() {
document.querySelector('#passage').value = ''; document.querySelector('#passage').value = '';
document.querySelector('#noteId').value = uuidv4(); document.querySelector('#noteId').value = uuidv4();
//document.querySelector('#noteSearch').style.display = 'none';
document.querySelector('#ref-list').innerHTML = ''; document.querySelector('#ref-list').innerHTML = '';
document.querySelector('#ref').innerHTML = ''; document.querySelector('#ref').innerHTML = '';
document.querySelector('.toggle').click(); document.querySelector('.toggle').click();

View File

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

View File

@ -29,6 +29,22 @@ class NoteRepository extends ServiceEntityRepository
return $ret; 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 // * @return Note[] Returns an array of Note objects
// */ // */

View File

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