ProjetRIOT/components/search.riot

214 lines
7.3 KiB
Plaintext
Raw Normal View History

<search>
2023-03-28 13:06:28 +02:00
<div class="box p-1 m-2">
<div class="columns m-1">
<input onkeyup={filterSearch} class="input" type="input" placeholder={state.placeholder}>
<button class="button ml-1" disabled={!state.currentStep || state.updating} onclick={cruiseBack}>&lt;</button>
2023-03-28 13:06:28 +02:00
</div>
<div id="list-formations">
2023-03-26 15:03:06 +02:00
<ul>
2023-03-28 13:06:28 +02:00
<li class="m-1" each={item in this.state.items}>
<button class="button is-fullwidth" disabled={state.updating} onclick={()=>cruiseForward(item.name)}>
2023-03-28 13:42:53 +02:00
<span style="font-size: .75em;"><strong>{item.name}</strong></span>
2023-03-28 13:06:28 +02:00
<div style="margin-left: auto;"></div>
<span class="tag is-primary">{item.count}</span>
</button>
2023-03-26 15:03:06 +02:00
</li>
</ul>
</div>
2023-03-28 13:06:28 +02:00
</div>
<script>
import PAPI from '../javascript/parcoursup-link.js'
2023-03-26 15:03:06 +02:00
const PLACEHOLDERS = [
"Formation",
"Filière",
"Spécialité"
]
2023-03-29 16:50:48 +02:00
export default {
2023-03-29 16:50:48 +02:00
updateList() {
let promise
this.update({
updating: true
})
2023-03-29 16:50:48 +02:00
switch (this.state.currentStep) {
case 0:
promise = PAPI.fetchFilieres()
break
case 1:
promise = PAPI.fetchFiliere(this.state.filiere)
break
case 2:
promise = PAPI.fetchSpecialites(this.state.specialite)
break
}
2023-03-29 16:50:48 +02:00
promise.then((response) => {
this.state.allItems = response
this.filterSearch()
this.update({
updating: false
})
})
},
2023-03-29 16:50:48 +02:00
filterSearch() {
let input = this.$("input")
if (!input) return
let finalArray = []
//On évite de trier avant d'avoir plus de 1 lettres.
if (input.value.length > 1) {
finalArray = this.state.allItems.filter((item) => {
return item.name.toLowerCase().includes(input.value.toLowerCase())
})
} else {
finalArray = this.state.allItems
}
this.update({
items: finalArray
})
},
cruiseForward(selection) {
if (this.state.currentStep >= 2) return
switch (this.state.currentStep) {
case 0:
this.state.filiere = selection
break
case 1:
this.state.specialite = selection
break
}
this.state.currentStep++
this.updateList()
this.update({
placeholder: PLACEHOLDERS[this.state.currentStep]
})
},
cruiseBack() {
if (!this.state.currentStep) return
this.state.currentStep--
this.updateList()
this.update({
placeholder: PLACEHOLDERS[this.state.currentStep]
})
},
2023-03-29 16:50:48 +02:00
onBeforeMount(props, state) {
//Initial state
this.state = {
placeholder: PLACEHOLDERS[0],
2023-03-29 16:50:48 +02:00
currentStep: 0,
allItems: null,
filter: null,
items: null,
filiere: null,
specialite: null,
updating: false
2023-03-29 16:50:48 +02:00
}
},
2023-03-29 16:50:48 +02:00
onMounted() {
this.updateList()
2023-03-29 16:50:48 +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 20:19:59 +02:00
2023-03-27 21:22:11 +02:00
async function fetchFiliere(state) {
if (state.sousfili){
return PAPI.fetchSpecialites(state.sousfili)
2023-03-29 16:50:48 +02:00
} else if (state.fili && !state.sousfili){
return PAPI.fetchFiliere(state.fili)
2023-03-29 16:50:48 +02:00
} else if (!state.fili && !state.sousfili){
return PAPI.fetchFilieres()
2023-03-27 21:22:11 +02:00
}
2023-03-26 15:03:06 +02:00
}
2023-03-29 16:50:48 +02:00
2023-03-26 15:03:06 +02:00
export default function search(){
2023-03-26 15:03:06 +02:00
return {
onBeforeMount(props, state) {
2023-03-29 16:50:48 +02:00
// initial state
2023-03-26 15:03:06 +02:00
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-29 16:50:48 +02:00
}*/
</script>
</search>