ProjetRIOT/components/search.riot

155 lines
4.9 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}>
2023-03-29 22:56:14 +02:00
<button class="button is-fullwidth p-2" disabled={state.updating} onclick={()=>cruiseForward(item.name)}>
<span style="font-size: .75em; max-size: 90%"><strong>{item.name}</strong></span>
<div style="margin-left: auto;"></div>
2023-03-28 13:06:28 +02:00
<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:
2023-03-29 22:01:50 +02:00
promise = PAPI.fetchFiliere(this.state.course.fili)
break
case 2:
2023-03-29 22:01:50 +02:00
promise = PAPI.fetchSpecialites(this.state.course.sousfili)
break
}
2023-03-29 16:50:48 +02:00
promise.then((response) => {
this.state.allItems = response
this.filterSearch()
2023-03-29 22:56:14 +02:00
this.update({
updating: false
})
}, () => {
if (!this.state.currentStep) {
this.cruiseBack()
}
this.update({
updating: false
})
})
},
2023-03-29 16:50:48 +02:00
2023-03-29 22:56:14 +02:00
clearSearch() {
this.$("input").value = ""
},
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) {
switch (this.state.currentStep) {
case 0:
2023-03-29 22:01:50 +02:00
this.state.course.fili = selection
break
case 1:
2023-03-29 22:01:50 +02:00
this.state.course.sousfili = selection
break
case 2:
this.state.course.soussousfili = selection
this.state.updateCourse(this.state.course)
break
2023-03-29 22:56:14 +02:00
case 2:
console.log(this.props)
console.log(this.root)
console.log("end")
return
default:
return
}
this.state.currentStep++
this.updateList()
2023-03-29 22:56:14 +02:00
this.clearSearch()
this.update({
placeholder: PLACEHOLDERS[this.state.currentStep]
})
},
cruiseBack() {
if (!this.state.currentStep) return
this.state.currentStep--
this.updateList()
2023-03-29 22:56:14 +02:00
this.clearSearch()
this.update({
placeholder: PLACEHOLDERS[this.state.currentStep]
})
},
2023-03-29 16:50:48 +02:00
onBeforeMount(props, state) {
2023-03-29 22:01:50 +02:00
console.log("on m'a appelé")
2023-03-29 16:50:48 +02:00
//Initial state
this.state = {
placeholder: PLACEHOLDERS[0],
2023-03-29 16:50:48 +02:00
currentStep: 0,
allItems: null,
items: null,
2023-03-29 22:01:50 +02:00
course: [],
2023-03-29 22:56:14 +02:00
updating: false,
2023-03-29 22:01:50 +02:00
updateCourse: this.props.updateCourse
2023-03-29 16:50:48 +02:00
}
2023-03-29 22:01:50 +02:00
console.log(this.state.updateCourse)
},
2023-03-29 16:50:48 +02:00
2023-03-29 22:56:14 +02:00
onMounted(props, state) {
this.updateList()
2023-03-29 16:50:48 +02:00
}
}
</script>
</search>