Skip to content

Commit 6a6415d

Browse files
committed
Working add search events
1 parent c73cd2a commit 6a6415d

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

pgml-dashboard/src/api/cms.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,13 @@ impl Collection {
650650

651651
#[post("/search_event", data = "<search_event>")]
652652
async fn search_event(search_event: Form<crate::forms::SearchEvent>, site_search: &State<crate::utils::markdown::SiteSearch>) -> ResponseOk {
653-
eprintln!("WE GOT IT: {:?}", search_event.clicked);
654-
ResponseOk("".to_string())
653+
match site_search.add_search_event(search_event.search_id, search_event.clicked).await {
654+
Ok(_) => ResponseOk("ok".to_string()),
655+
Err(e) => {
656+
eprintln!("{:?}", e);
657+
ResponseOk("error".to_string())
658+
}
659+
}
655660
}
656661

657662
#[get("/search?<query>", rank = 20)]

pgml-dashboard/src/utils/markdown.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,13 @@ impl SiteSearch {
12861286
.collect()
12871287
}
12881288

1289+
pub async fn add_search_event(&self, search_id: i64, search_result: i64) -> anyhow::Result<()> {
1290+
self.collection.add_search_event(search_id, search_result + 1, serde_json::json!({
1291+
"clicked": true
1292+
}).into(), &self.pipeline).await?;
1293+
Ok(())
1294+
}
1295+
12891296
pub async fn search(
12901297
&self,
12911298
query: &str,

pgml-dashboard/static/js/search.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,22 @@ export default class extends Controller {
1818

1919
this.timer;
2020

21-
// Listen to click events and store clicked results
22-
document.addEventListener("click", function(e) {
23-
const target = e.target.closest(".search-result");
24-
if (target) {
25-
const resultIndex = target.getAttribute("data-result-index");
26-
const searchId = target.getAttribute("data-search-id");
27-
fetch('/search_event', {
28-
method: 'POST',
29-
headers: {
30-
'Content-Type': 'application/json',
31-
},
32-
body: JSON.stringify({
33-
search_id: searchId,
34-
clicked: resultIndex,
35-
}),
36-
});
37-
}
38-
});
21+
document.addEventListener("click", this.handle_search_click);
22+
}
23+
24+
handle_search_click(e) {
25+
const target = e.target.closest(".search-result");
26+
if (target) {
27+
const resultIndex = target.getAttribute("data-result-index");
28+
const searchId = target.getAttribute("data-search-id");
29+
const formData = new FormData();
30+
formData.append("search_id", searchId);
31+
formData.append("clicked", resultIndex);
32+
fetch('/search_event', {
33+
method: 'POST',
34+
body: formData,
35+
});
36+
}
3937
}
4038

4139
search(e) {
@@ -46,7 +44,7 @@ export default class extends Controller {
4644
}, 250);
4745
}
4846

49-
focusSearchInput = (e) => {
47+
focusSearchInput = () => {
5048
this.searchInput.focus()
5149
this.searchTriggerTarget.blur()
5250
}
@@ -63,5 +61,6 @@ export default class extends Controller {
6361
disconnect() {
6462
this.searchTriggerTarget.removeEventListener('shown.bs.modal', this.focusSearchInput)
6563
this.searchTriggerTarget.removeEventListener('hidden.bs.modal', this.updateSearch)
64+
document.removeEventListener("click", this.handle_search_click);
6665
}
6766
}

0 commit comments

Comments
 (0)