Files
parcoursup/src/components/global/selector/api/Model.js

120 lines
3.9 KiB
JavaScript
Raw Normal View History

2023-03-08 23:21:31 +01:00
import log from "./log"
export default class Model {
constructor() {
this.state = {
api: {
link: "https://data.enseignementsup-recherche.gouv.fr/api/records/1.0/search/?dataset=fr-esr-parcoursup&q=&lang=fr&rows=0&sort=tri",
facet: {
filiaire: "fili",
formation: "form_lib_voe_acc",
spec: "fil_lib_voe_acc"
}
},
page: {
curIndex: 0, /* section n.0 -> n.2. */
cat: null,
path: ["", "", ""],
name: [
"formation",
"filière de formation",
"filière de formation détaillée"
],
}
}
log("Selector", "Model 2/3")
}
getCurIndex() {
return this.state.page.curIndex
}
nextPage() {
if(this.state.page.curIndex+1 <= 2) {
this.state.page.curIndex++
}
}
previousPage() {
if(this.state.page.curIndex-1 >= 0) {
this.state.page.curIndex--
}
}
setCat(v) {
this.state.page.cat = v
}
getModelData0() {
if(!localStorage.getItem(`sec0`)) {
const link = `${ this.state.api.link }` +
`&facet=${this.state.api.facet.filiaire}`
return fetch(link)
.then((res) => res.json())
.then((data) => {
if(data) {
localStorage.setItem(`sec0`, JSON.stringify(data.facet_groups[0].facets))
return data.facet_groups[0].facets
} else {
return null
}
})
} else {
return new Promise((resolve, reject) => {
resolve(JSON.parse(localStorage.getItem(`sec0`)))
})
}
}
getModelData1() {
if(!localStorage.getItem(`sec1-${this.state.page.cat}`)) {
const link = `${this.state.api.link}` +
`&facet=${this.state.api.facet.filiaire}` +
`&facet=${this.state.api.facet.formation}` +
`&facet=${this.state.api.facet.spec}` +
`&refine.${this.state.api.facet.filiaire}=${this.state.page.cat}`
return fetch(link)
.then((res) => res.json())
.then((data) => {
if(data) {
localStorage.setItem(`sec1-${this.state.page.cat}`, JSON.stringify(data.facet_groups[1].facets))
return data.facet_groups[1].facets
} else {
return null
}
})
} else {
return new Promise((resolve, reject) => {
resolve(JSON.parse(localStorage.getItem(`sec1-${this.state.page.cat}`)))
})
}
}
getModelData2() {
if(!localStorage.getItem(`sec2-${this.state.page.cat}`)) {
const link = `${this.state.api.link}` +
`&facet=${this.state.api.facet.filiaire}` +
`&facet=${this.state.api.facet.formation}` +
`&facet=${this.state.api.facet.spec}` +
`&refine.${this.state.api.facet.filiaire}=${this.state.page.cat}`
return fetch(link)
.then((res) => res.json())
.then((data) => {
if(data) {
localStorage.setItem(`sec2-${this.state.page.cat}`, JSON.stringify(data.facet_groups[2].facets))
return data.facet_groups[2].facets
} else {
return null
}
})
} else {
return new Promise((resolve, reject) => {
resolve(JSON.parse(localStorage.getItem(`sec2-${this.state.page.cat}`)))
})
}
}
}