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") } /** * Retourne l'index courant de la page. * @return int * */ 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-- } } /** * @param v La valeur du refine * */ setCat(v) { this.state.page.cat = v } /** * Retourne la section 0 + l'enregistre dans le local storage * @return les donnees demandees en JSON * */ 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`))) }) } } /** * Retourne la section 1 + l'enregistre dans le local storage * @return les donnees demandees en JSON * */ 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}`))) }) } } /** * Retourne la section 2 + l'enregistre dans le local storage * @return les donnees demandees en JSON * */ 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}`))) }) } } }