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

233 lines
7.4 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: {
2023-03-09 13:43:15 +01:00
link: "https://data.enseignementsup-recherche.gouv.fr/api/records/1.0/search/?dataset=fr-esr-parcoursup&q=&lang=fr&sort=tri",
2023-03-08 23:21:31 +01:00
facet: {
filiaire: "fili",
formation: "form_lib_voe_acc",
spec: "fil_lib_voe_acc"
}
},
page: {
curIndex: 0, /* section n.0 -> n.2. */
cat: null,
2023-03-08 23:57:59 +01:00
scat: null,
2023-03-09 13:43:15 +01:00
tcat: null,
2023-03-08 23:21:31 +01:00
path: ["", "", ""],
name: [
"formation",
"filière de formation",
"filière de formation détaillée"
],
}
}
log("Selector", "Model 2/3")
}
2023-03-08 23:41:41 +01:00
/**
* Retourne l'index courant de la page.
2023-03-09 11:52:52 +01:00
* @return { number } l'index courant
2023-03-08 23:41:41 +01:00
* */
2023-03-08 23:21:31 +01:00
getCurIndex() {
return this.state.page.curIndex
}
2023-03-08 23:41:41 +01:00
/**
* Passer a la page suivante.
* */
2023-03-08 23:21:31 +01:00
nextPage() {
if(this.state.page.curIndex+1 <= 2) {
this.state.page.curIndex++
}
}
2023-03-08 23:41:41 +01:00
/**
* Passer a la page precedente.
* */
2023-03-08 23:21:31 +01:00
previousPage() {
if(this.state.page.curIndex-1 >= 0) {
this.state.page.curIndex--
}
}
2023-03-08 23:41:41 +01:00
/**
2023-03-09 11:52:52 +01:00
* Recuperer le titre de la page
* @return { string } Le titre.
2023-03-08 23:41:41 +01:00
* */
2023-03-09 11:52:52 +01:00
getTitle() {
return this.state.page.name[this.state.page.curIndex]
2023-03-08 23:21:31 +01:00
}
2023-03-08 23:57:59 +01:00
/**
2023-03-09 11:52:52 +01:00
* @param { string } val La valeur du refine "fili"
2023-03-08 23:57:59 +01:00
* */
2023-03-09 11:52:52 +01:00
setCat(val) {
this.state.page.cat = val
}
/**
* @param { string } val la valeur du refine "form_lib_voe_acc"
* */
setSCat(val) {
this.state.page.scat = val
}
2023-03-09 13:43:15 +01:00
/**
* Recuperer la valeur de TCat
* @return { string | null }
* */
getTCat() {
return this.state.page.tcat
}
/**
* @param { string } val la valeur du refine "fil_lib_voe_acc"
* */
setTCat(val) {
this.state.page.tcat = val
}
2023-03-09 11:52:52 +01:00
/**
* Retourne le chemin filiaires - formations - formations detaillees
* @return { array[] } Le chemin.
* */
getPath() {
return this.state.page.path
}
/**
* Ajouter un chemin
* @param { number } index
* @param { string } choose
* */
setPath(index, choose) {
this.state.page.path[index] = choose
2023-03-08 23:57:59 +01:00
}
2023-03-08 23:41:41 +01:00
/**
* Retourne la section 0 + l'enregistre dans le local storage
2023-03-09 11:52:52 +01:00
* @return { json } Le resultat.
2023-03-08 23:41:41 +01:00
* */
2023-03-08 23:21:31 +01:00
getModelData0() {
if(!localStorage.getItem(`sec0`)) {
2023-03-09 13:43:15 +01:00
const link = `${ this.state.api.link }&rows=0` +
2023-03-08 23:21:31 +01:00
`&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`)))
})
}
}
2023-03-08 23:41:41 +01:00
/**
* Retourne la section 1 + l'enregistre dans le local storage
2023-03-09 11:52:52 +01:00
* @return { json } les donnees demandees en JSON
2023-03-08 23:41:41 +01:00
* */
2023-03-08 23:21:31 +01:00
getModelData1() {
if(!localStorage.getItem(`sec1-${this.state.page.cat}`)) {
2023-03-09 13:43:15 +01:00
const link = `${this.state.api.link}&rows=0` +
2023-03-08 23:21:31 +01:00
`&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}`)))
})
}
}
2023-03-08 23:41:41 +01:00
/**
* Retourne la section 2 + l'enregistre dans le local storage
2023-03-09 11:52:52 +01:00
* PS: Generalement c'est ici que il y a possibilite de ne pas avoir de 3eme section
* @return { json } les donnees demandees en JSON
2023-03-08 23:41:41 +01:00
* */
2023-03-08 23:21:31 +01:00
getModelData2() {
2023-03-08 23:57:59 +01:00
if(!localStorage.getItem(`sec2-${this.state.page.cat}-${this.state.page.scat}`)) {
2023-03-09 13:43:15 +01:00
const link = `${this.state.api.link}&rows=0` +
2023-03-08 23:21:31 +01:00
`&facet=${this.state.api.facet.filiaire}` +
`&facet=${this.state.api.facet.formation}` +
`&facet=${this.state.api.facet.spec}` +
2023-03-08 23:57:59 +01:00
`&refine.${this.state.api.facet.filiaire}=${this.state.page.cat}` +
`&refine.${this.state.api.facet.formation}=${this.state.page.scat}`
2023-03-08 23:21:31 +01:00
return fetch(link)
.then((res) => res.json())
.then((data) => {
2023-03-09 11:52:52 +01:00
try {
if(data) {
localStorage.setItem(`sec2-${this.state.page.cat}-${this.state.page.scat}`, JSON.stringify(data.facet_groups[2].facets))
return data.facet_groups[2].facets
}
} catch(donothing) {}
2023-03-08 23:21:31 +01:00
})
} else {
return new Promise((resolve, reject) => {
2023-03-08 23:57:59 +01:00
resolve(JSON.parse(localStorage.getItem(`sec2-${this.state.page.cat}-${this.state.page.scat}`)))
2023-03-08 23:21:31 +01:00
})
}
}
2023-03-09 13:43:15 +01:00
/**
2023-03-09 14:41:05 +01:00
* Retourne la liste d'etablissement
* @return { json } les donnees demandees en JSON
2023-03-09 13:43:15 +01:00
* */
2023-03-09 14:41:05 +01:00
getModelData3() {
2023-03-09 13:43:15 +01:00
if(!localStorage.getItem(`sec-etab-${this.state.page.cat}-${this.state.page.scat}-${this.state.page.tcat}`)) {
2023-03-09 14:41:05 +01:00
const link = `${this.state.api.link}&rows=10000` +
2023-03-09 13:43:15 +01:00
`&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}` +
`&refine.${this.state.api.facet.formation}=${this.state.page.scat}` +
`&refine.${this.state.api.facet.spec}=${this.state.page.tcat}`
2023-03-09 14:41:05 +01:00
console.log(link)
2023-03-09 13:43:15 +01:00
return fetch(link)
.then((res) => {
return res.json()
})
.then((data) => {
try {
if(data) {
localStorage.setItem(`sec-etab-${this.state.page.cat}-${this.state.page.scat}-${this.state.page.tcat}`, JSON.stringify(data.records))
2023-03-09 14:41:05 +01:00
this.state.page.tcat = null
2023-03-09 13:43:15 +01:00
return data.records
}
} catch(donothing) {}
})
} else {
return new Promise((resolve, reject) => {
resolve(JSON.parse(localStorage.getItem(`sec-etab-${this.state.page.cat}-${this.state.page.scat}-${this.state.page.tcat}`)))
2023-03-09 14:41:05 +01:00
this.state.page.tcat = null
2023-03-09 13:43:15 +01:00
})
}
}
2023-03-08 23:21:31 +01:00
}