fetch and display etape data

This commit is contained in:
Valentin
2024-07-29 21:31:08 +02:00
parent 82ccd41d8b
commit 3bd3d32e8a
3 changed files with 113 additions and 11 deletions

View File

@@ -3,7 +3,17 @@ import REST from '../api/rest-axios';
export const useEtapeStore = defineStore('etape', {
state: () => ({
etapeData: null,
href: '',
title: '',
adresse: {},
etape_number: '',
dates: {},
geofield: {},
galeries: [],
parties: [],
saison: {},
thematiques: [],
vignette: {},
loading: false,
error: null,
}),
@@ -12,8 +22,35 @@ export const useEtapeStore = defineStore('etape', {
this.loading = true;
this.error = null;
try {
const response = await REST.get(`/node/${nid}?_format=json`);
this.etapeData = response.data;
// const response = await REST.get(`/node/${nid}?_format=json`);
const response = await REST.get(`/jsonapi/node/etape/`);
for (let etape of response.data.data) {
if (etape.attributes.drupal_internal__nid == nid) {
for (let metatag of etape.attributes.metatag) {
if (metatag.tag === "link") {
this.href = metatag.attributes.href;
}
}
this.title = etape.attributes.title;
this.adresse = etape.attributes.field_adresse;
this.etape_number = etape.attributes.field_arret_numero;
this.dates = etape.attributes.field_dates;
this.geofield = etape.attributes.field_geofield;
this.galeries = await fetchEtapeContent('field_galleries', etape.relationships);
const partiesFetch = await fetchEtapeContent('field_parties', etape.relationships);
let partiesArray = []
for (let partie of partiesFetch) {
partiesArray.push({ title: partie.attributes.field_titre, text: partie.attributes.field_texte.value });
}
this.parties = partiesArray;
this.saison = await fetchEtapeContent('field_saison', etape.relationships);
this.saison = await fetchEtapeContent('field_saison', etape.relationships);
this.thematiques = await fetchEtapeContent('field_thematiques', etape.relationships);
const vignetteFetch = await fetchEtapeContent('field_vignette', etape.relationships);
this.vignette = { url: vignetteFetch.attributes.uri.url, alt: etape.attributes.field_vignette_alt };
break;
}
}
} catch (error) {
this.error = 'Failed to fetch data';
console.error('Issue with getNodeData', error);
@@ -22,4 +59,17 @@ export const useEtapeStore = defineStore('etape', {
}
},
},
});
});
async function fetchEtapeContent(field, relationships) {
if (relationships[field].data) {
try {
const contentLink = relationships[field].links.related.href;
const contentFetch = await REST.get(contentLink);
return contentFetch.data.data;
} catch (error) {
this.error = 'Failed to fetch data';
console.error('Issue with getNodeData', error);
}
}
}