Files
parcoursup/src/components/global/api/Model.js
pro.boooooo 2b1132fdf9 $
2023-03-09 14:41:05 +01:00

233 lines
7.4 KiB
JavaScript

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&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,
scat: null,
tcat: null,
path: ["", "", ""],
name: [
"formation",
"filière de formation",
"filière de formation détaillée"
],
}
}
log("Selector", "Model 2/3")
}
/**
* Retourne l'index courant de la page.
* @return { number } l'index courant
* */
getCurIndex() {
return this.state.page.curIndex
}
/**
* Passer a la page suivante.
* */
nextPage() {
if(this.state.page.curIndex+1 <= 2) {
this.state.page.curIndex++
}
}
/**
* Passer a la page precedente.
* */
previousPage() {
if(this.state.page.curIndex-1 >= 0) {
this.state.page.curIndex--
}
}
/**
* Recuperer le titre de la page
* @return { string } Le titre.
* */
getTitle() {
return this.state.page.name[this.state.page.curIndex]
}
/**
* @param { string } val La valeur du refine "fili"
* */
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
}
/**
* 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
}
/**
* 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
}
/**
* Retourne la section 0 + l'enregistre dans le local storage
* @return { json } Le resultat.
* */
getModelData0() {
if(!localStorage.getItem(`sec0`)) {
const link = `${ this.state.api.link }&rows=0` +
`&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`)))
})
}
}
/**
* Retourne la section 1 + l'enregistre dans le local storage
* @return { json } les donnees demandees en JSON
* */
getModelData1() {
if(!localStorage.getItem(`sec1-${this.state.page.cat}`)) {
const link = `${this.state.api.link}&rows=0` +
`&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}`)))
})
}
}
/**
* Retourne la section 2 + l'enregistre dans le local storage
* PS: Generalement c'est ici que il y a possibilite de ne pas avoir de 3eme section
* @return { json } les donnees demandees en JSON
* */
getModelData2() {
if(!localStorage.getItem(`sec2-${this.state.page.cat}-${this.state.page.scat}`)) {
const link = `${this.state.api.link}&rows=0` +
`&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}`
return fetch(link)
.then((res) => res.json())
.then((data) => {
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) {}
})
} else {
return new Promise((resolve, reject) => {
resolve(JSON.parse(localStorage.getItem(`sec2-${this.state.page.cat}-${this.state.page.scat}`)))
})
}
}
/**
* Retourne la liste d'etablissement
* @return { json } les donnees demandees en JSON
* */
getModelData3() {
if(!localStorage.getItem(`sec-etab-${this.state.page.cat}-${this.state.page.scat}-${this.state.page.tcat}`)) {
const link = `${this.state.api.link}&rows=10000` +
`&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}`
console.log(link)
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))
this.state.page.tcat = null
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}`)))
this.state.page.tcat = null
})
}
}
}