ProjetRIOT/components/search.riot

81 lines
3.1 KiB
Plaintext

<search>
<label>
<input onkeydown={searchF} type="input" placeholder= {state.placeholder}>
<div id="list-formations">
<ul>
<li each={item in this.state.items}>
<span onclick={()=>filter(item.name)}>{item.name}</span>
<span>{item.count}</span>
</li>
</ul>
</div>
</label>
<script>
async function apiLink(whatToSearch, whereToSearch){
let result = await fetch("https://data.enseignementsup-recherche.gouv.fr/api/records/1.0/search/?dataset=fr-esr-parcoursup&q=&sort=tri&facet="+whatToSearch+"&timezone=Europe%2FBerlin")
let resultats = await result.json()
let table = resultats["facet_groups"][whereToSearch]["facets"]
return table
}
export default function search(){
return {
onBeforeMount(props, state) {
// initial state
this.state = {
placeholder: "Formation",
items: null,
allitems: null,
whatToSearch: "fili",
fili: null,
sousfili: null
}
apiLink(this.state.whatToSearch,0).then((response) => {
this.update({
items: response,
allitems: response
})
})
},
searchF(e){
let responseFiltered=[]
this.state.allitems.forEach(formation => {
if (formation.name.toUpperCase().includes(e.target.value.toUpperCase())) {
responseFiltered.push(formation)
}
});
this.update({
items: responseFiltered
})
},
filter(fili){
console.log("filter! "+fili)
let step=0;
if (this.state.placeholder==="Filière de formation"){
this.update({
placeholder: "Filière de formation détaillée",
sousfili: fili,
whatToSearch: "fili&facet=form_lib_voe_acc&facet=fil_lib_voe_acc&refine.form_lib_voe_acc="+this.state.sousfili+"&refine.fili="this.state.fili
})
} else {
this.update({
placeholder: "Filière de formation",
fili: fili,
whatToSearch: "fili&facet=form_lib_voe_acc&refine.fili="+this.state.fili
})
}
apiLink(this.state.whatToSearch).then((response) => {
this.update({
allitems: response,
item: response
})
console.log(this.state.items)
})
}
}
}
</script>
</search>