From 14e253bbc78e36e07f4aec699fe3ee1316cc826b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ha=C3=AFssous?= Date: Mon, 27 Mar 2023 11:16:06 +0200 Subject: [PATCH 1/4] search etape 2/3 --- components/search.riot | 45 +++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/components/search.riot b/components/search.riot index 43399f4..82a5f7a 100644 --- a/components/search.riot +++ b/components/search.riot @@ -4,7 +4,7 @@
@@ -12,8 +12,8 @@ diff --git a/javascript/search.js b/javascript/search.js deleted file mode 100644 index ad7b098..0000000 --- a/javascript/search.js +++ /dev/null @@ -1,120 +0,0 @@ -let model = { - getFormations(search) { - return new Promise((resolve, reject) => { - let xhr = new XMLHttpRequest(); - xhr.open("GET", "/api/records/1.0/search/?dataset=fr-esr-parcoursup&q=&sort=tri&facet=fili&timezone=Europe%2FBerlin"); - xhr.responseType = "json"; - xhr.onload = ev => { - if (xhr.status == 200) resolve(xhr.response); - }; - xhr.onerror = () => { - reject("error"); - }; - xhr.send(); - }); - } -}; - -class Controller { - constructor(view, model) { - this.view = view; - this.model = model; - this.loading = false; - this.lastSearch = null; - this.error = null; - this.results = []; - this.view.setLoading(false); - this.view.bindSearch(this.search.bind(this)); - } - reset() { - this.loading = false; - this.error = null; - this.results = []; - } - async search(formation) { - this.model.getFormations(formation).then(response => { - let table = response["facet groups"][0]["facets"]; - this.view.renderList(table); - }).catch(error => { - this.view.renderMessage(error); - }); - } -} - -function debounce(fn, wait) { - let timeout; - return (...args) => { - clearTimeout(timeout); - timeout = setTimeout(() => fn(...args), wait); - }; -} -class View { - constructor() { - this.listFormations = document.querySelector("#list-formations"); - this.inputSearch = document.querySelector("input"); - this.message = document.querySelector("p.error"); - } - _getInput() { - return this.inputSearch.value; - } - renderMessage(error) { - this.message.style.display = "block"; - this.message.textContent = error; - } - renderList(formations) { - let ul = document.createElement("ul"); - formations.forEach(formation => { - let li = document.createElement("li"); - let a = document.createElement("a"); - let span = document.createElement("span"); - //a.href = `test` - a.target = "_blank"; - a.textContent = formation.name; - span.textContent = formation.name; - li.appendChild(a); - li.appendChild(span); - ul.appendChild(li); - }); - this.listFormations.replaceChildren(ul); - } - bindSearch(handler) { - this.inputSearch.addEventListener("input", debounce(e => { - handler(this._getInput()); - }, 500)); - } -} - -var search = { - css: null, - exports: function search() { - return { - onBeforeMount(props, state) { - // initial state - this.state = { - formation: props.formation - }; - }, - search() { - console.log("test1"); - const view = new View(); - new Controller(view, model); - } - }; - }, - template: (template, expressionTypes, bindingTypes, getComponent) => template('', [{ - redundantAttribute: 'expr1', - selector: '[expr1]', - expressions: [{ - type: expressionTypes.EVENT, - name: 'oninput', - evaluate: _scope => _scope.search - }, { - type: expressionTypes.ATTRIBUTE, - name: 'placeholder', - evaluate: _scope => _scope.state.formation - }] - }]), - name: 'search' -}; - -export { search as default }; From d4112402c10f3a6321eb47c7b0a6ae72b8035fbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ha=C3=AFssous?= Date: Mon, 27 Mar 2023 20:19:59 +0200 Subject: [PATCH 3/4] search 3/3 --- components/search.riot | 90 ++++++++++++++++----- javascript/components/search.js | 139 ++++++++++++++++++++++++++++++++ javascript/parcoursup-link.js | 2 +- javascript/search.js | 123 ++++++++++++++++++++++++++++ 4 files changed, 332 insertions(+), 22 deletions(-) create mode 100644 javascript/components/search.js create mode 100644 javascript/search.js diff --git a/components/search.riot b/components/search.riot index 5776dab..48e9c91 100644 --- a/components/search.riot +++ b/components/search.riot @@ -1,6 +1,7 @@