ProjetRIOT/components/search.riot

105 lines
4.0 KiB
Plaintext
Raw Normal View History

<search>
<label>
2023-03-27 16:15:49 +02:00
<input onkeydown={searchF} type="input" placeholder= {state.placeholder}>
2023-03-27 20:19:59 +02:00
<button onclick={back}><</button>
<div id="list-formations">
2023-03-26 15:03:06 +02:00
<ul>
<li each={item in this.state.items}>
2023-03-27 11:16:06 +02:00
<span onclick={()=>filter(item.name)}>{item.name}</span>
2023-03-26 15:03:06 +02:00
<span>{item.count}</span>
</li>
</ul>
</div>
</label>
<script>
2023-03-26 15:03:06 +02:00
2023-03-27 20:19:59 +02:00
const searchURL = `https://data.enseignementsup-recherche.gouv.fr/api/records/1.0/search/?dataset=fr-esr-parcoursup&timezone=Europe%2FBerlin`
2023-03-27 21:22:11 +02:00
async function fetchFiliere(state) {
if (state.sousfili){
var request = await fetch(`${searchURL}&rows=0&sort=tri&facet=fil_lib_voe_acc&refine.form_lib_voe_acc=${state.sousfili}`)
} else if (state.fili&&!state.sousfili){
var request = await fetch(`${searchURL}&rows=0&sort=tri&facet=form_lib_voe_acc&refine.fili=${state.fili}`)
} else if (!state.fili&&!state.sousfili){
var request = await fetch(`${searchURL}&rows=0&sort=tri&facet=fili`)
}
2023-03-27 20:19:59 +02:00
let result = await request.json()
return result["facet_groups"][0]["facets"]
2023-03-26 15:03:06 +02:00
}
export default function search(){
2023-03-26 15:03:06 +02:00
return {
onBeforeMount(props, state) {
// initial state
this.state = {
2023-03-27 16:15:49 +02:00
placeholder: "Formation",
2023-03-26 22:12:11 +02:00
items: null,
2023-03-27 11:16:06 +02:00
allitems: null,
2023-03-27 16:15:49 +02:00
fili: null,
sousfili: null
2023-03-26 15:03:06 +02:00
}
2023-03-27 21:22:11 +02:00
fetchFiliere(this.state).then((response) => {
2023-03-26 15:03:06 +02:00
this.update({
2023-03-26 22:12:11 +02:00
items: response,
allitems: response
2023-03-26 15:03:06 +02:00
})
})
},
2023-03-26 22:12:11 +02:00
searchF(e){
let responseFiltered=[]
this.state.allitems.forEach(formation => {
2023-03-27 11:16:06 +02:00
if (formation.name.toUpperCase().includes(e.target.value.toUpperCase())) {
2023-03-26 22:12:11 +02:00
responseFiltered.push(formation)
}
});
this.update({
items: responseFiltered
})
2023-03-27 11:16:06 +02:00
},
filter(fili){
console.log("filter! "+fili)
2023-03-27 16:15:49 +02:00
if (this.state.placeholder==="Filière de formation"){
2023-03-27 11:16:06 +02:00
this.update({
2023-03-27 16:15:49 +02:00
placeholder: "Filière de formation détaillée",
sousfili: fili,
})
2023-03-27 20:19:59 +02:00
} else if (this.state.placeholder="Formation"){
2023-03-27 11:16:06 +02:00
this.update({
2023-03-27 16:15:49 +02:00
placeholder: "Filière de formation",
2023-03-27 20:19:59 +02:00
fili: fili
})
2023-03-27 11:16:06 +02:00
}
2023-03-27 21:22:11 +02:00
fetchFiliere(this.state).then((response) => {
this.update({
allitems: response,
items: response
})
console.log(this.state.items)
})
2023-03-27 20:19:59 +02:00
},
back(){
console.log("back")
if (this.state.placeholder==="Filière de formation"){
2023-03-27 11:16:06 +02:00
this.update({
2023-03-27 20:19:59 +02:00
placeholder: "Formation",
fili: null,
})
} else if (this.state.placeholder="Filière de formation détaillée"){
this.update({
placeholder: "Filière de formation",
sousfili: null
})
}
2023-03-27 21:22:11 +02:00
fetchFiliere(this.state).then((response) => {
this.update({
allitems: response,
items: response
})
})
}
}
}
2023-03-26 15:03:06 +02:00
</script>
</search>