refactor content store data fetching with promises

This commit is contained in:
2025-03-07 17:46:13 +01:00
parent dffd179bc9
commit 2ca44f2550
8 changed files with 354 additions and 487 deletions

View File

@@ -0,0 +1,134 @@
import REST from '../../api/rest-axios';
import { fetchFromRelationships } from './contentFetchUtils';
export async function getCarteSensible(partie) {
const carteSensiblePromise = fetchFromRelationships('field_image_carte', partie.relationships);
const carteSensibleData = await carteSensiblePromise;
if (carteSensibleData) {
return {
url: {
original: carteSensibleData.attributes.uri.url,
small: carteSensibleData.attributes.image_style_uri.content_small,
medium: carteSensibleData.attributes.image_style_uri.content_medium,
large: carteSensibleData.attributes.image_style_uri.content_large,
xlarge: carteSensibleData.attributes.image_style_uri.content_x_large,
},
alt: partie.relationships.field_image_carte.data.meta.alt,
};
}
}
export async function getTitreTexte(partie) {
let titre = partie.attributes.field_titre;
let texte = partie.attributes.field_texte.value;
// get the resized images from the text
const imgRegex = /<img[^>]+>/g;
const uuidRegex = /data-entity-uuid="([^"]+)"/;
const imgTags = texte.match(imgRegex);
if (imgTags) {
const imagePromises = imgTags.map(imgTag => {
const uuidMatch = imgTag.match(uuidRegex);
if (uuidMatch && uuidMatch[1]) {
return REST.get(`/jsonapi/file/file/${uuidMatch[1]}`)
.then(response => ({
originalTag: imgTag,
imageData: response.data.data
}));
}
});
const images = await Promise.all(imagePromises);
images.forEach(({originalTag, imageData}) => {
const newImgTag = originalTag
.replace(/src="[^"]+"/,`src="${imageData.attributes.image_style_uri.content_medium}"`)
.replace('>',' data-large-src="' + imageData.attributes.image_style_uri.content_large + '">');
texte = texte.replace(originalTag, newImgTag);
});
}
return { titre, texte };
}
export async function getChiffresCles(partie) {
const chiffresClesFetch = await fetchFromRelationships('field_chiffres_clefs', partie.relationships);
if (chiffresClesFetch) {
let chiffresCles = [];
for (let chiffre of chiffresClesFetch) {
chiffresCles.push({
chiffre: chiffre.attributes.field_chiffre,
description: chiffre.attributes.field_description,
});
}
return chiffresCles;
}
}
export async function getDiaporama(partie) {
const diaporamaFetch = await fetchFromRelationships('field_diaporama', partie.relationships);
if (diaporamaFetch) {
const diaporamaPromises = diaporamaFetch.map((image, index) => {
return {
url: {
original: image.attributes.uri.url,
small: image.attributes.image_style_uri.content_small,
medium: image.attributes.image_style_uri.content_medium,
large: image.attributes.image_style_uri.content_large,
},
alt: partie.relationships.field_diaporama.data[index].meta.alt,
};
});
return await Promise.all(diaporamaPromises);
}
}
export async function getEntretien(partie) {
const [personnesFetch, questionsReponsesFetch] = await Promise.all([
fetchFromRelationships('field_personne_s', partie.relationships),
fetchFromRelationships('field_questions_reponses', partie.relationships)
]);
if (personnesFetch && questionsReponsesFetch) {
const personnesPromises = personnesFetch.map(async (personne) => {
const portraitFetch = await fetchFromRelationships('field_portrait', personne.relationships);
if (portraitFetch) {
return {
portrait: {
original: portraitFetch.attributes.uri.url,
small: portraitFetch.attributes.image_style_uri.content_small,
medium: portraitFetch.attributes.image_style_uri.content_medium,
large: portraitFetch.attributes.image_style_uri.content_large,
},
alt: personne.relationships.field_portrait.data.meta.alt,
description: personne.attributes.field_description,
};
}
});
const questionsReponses = questionsReponsesFetch.map(qr => ({
question: qr.attributes.field_question,
reponse: qr.attributes.field_reponse.value,
}));
return {
titre: partie.attributes.field_titre,
personnes: await Promise.all(personnesPromises),
questionsReponses: questionsReponses
}
}
}
export function getVideos(partie) {
let videos = [];
for (let video of partie.attributes.field_videos) {
const videoId = video.split('?v=')[1];
const videoUrl = `https://www.youtube.com/embed/${videoId}`;
videos.push(videoUrl);
}
return videos;
}

View File

@@ -0,0 +1,68 @@
import REST from '../../api/rest-axios';
export async function fetchFromRelationships(field, relationships) {
field = relationships[field] ? field : `${field}_static`;
if (relationships[field].links) {
const contentLink = relationships[field].links.related.href;
return REST.get(contentLink)
.then(contentFetch => contentFetch.data.data)
.catch(error => {
this.error = 'Failed to fetch data';
console.error('Issue with getNodeData', error);
});
}
return null;
}
export function getCleanDate(date) {
return {
d: date.split('-')[2],
m: new Intl.DateTimeFormat('fr-FR', { month: 'long' }).format(new Date(date)),
y: date.split('-')[0],
}
}
export async function getRelatedEtape(direction, path) {
const getRelatedEtapeContent = (relatedEtapeData) => {
if (relatedEtapeData) {
return fetchFromRelationships('field_vignette', relatedEtapeData.relationships)
.then(vignetteFetch => {
if (vignetteFetch) {
return {
url: relatedEtapeData.attributes.metatag.find(tag => tag.tag === "link")?.attributes.href,
couleur: relatedEtapeData.attributes.field_couleur,
title: relatedEtapeData.attributes.title,
postalCode: relatedEtapeData.attributes.field_adresse.postal_code,
dates: {
start: getCleanDate(relatedEtapeData.attributes.field_dates.value),
end: getCleanDate(relatedEtapeData.attributes.field_dates.end_value),
},
vignette: {
url: {
original: vignetteFetch.attributes.uri.url,
small: vignetteFetch.attributes.image_style_uri.content_small,
medium: vignetteFetch.attributes.image_style_uri.content_medium,
large: vignetteFetch.attributes.image_style_uri.content_large,
},
alt: relatedEtapeData.relationships.field_vignette.data.meta.alt,
},
}
}
});
}
}
const allEtapesPromise = REST.get('/jsonapi/views/etapes/block_1/');
return allEtapesPromise.then(allEtapesData => {
for (let [index, etape] of allEtapesData.data.data.entries()) {
if (etape.attributes.metatag.some(tag =>
tag.tag === "link" && tag.attributes.href === path
)) {
const relatedEtapeIndex = direction === 'next' ? index + 1 : index - 1;
return getRelatedEtapeContent(allEtapesData.data.data[relatedEtapeIndex]);
}
}
});
}

View File

@@ -0,0 +1,32 @@
import REST from '../../api/rest-axios';
export async function findContentByPath(contentTypes, path) {
for (let type of contentTypes) {
const response = await REST.get(`/jsonapi/node/${type}/`);
const content = response.data.data.find(content =>
content.attributes.metatag.some(tag =>
tag.tag === "link" && tag.attributes.href === path
)
);
if (content) {
return {
contentType: type,
rawContent: content,
};
}
// Handle special case for governance/partners (multiple items per page)
const pageRequested = window.location.href.split('/').pop().replace(/s?$/, '');
if (type === pageRequested
|| (type === 'gouvernance' && pageRequested === 'contact')
) {
return {
contentType: type,
rawContent: response.data.data,
};
}
}
return null;
}

View File

@@ -0,0 +1,54 @@
import REST from '../../api/rest-axios';
export async function getPartenaires(rawContent) {
const logoPromises = rawContent.map(item =>
REST.get(item.relationships.field_logo.links.related.href)
.then(logoFetch => ({
title: item.attributes.title,
description: item.attributes.body.value,
weight: item.attributes.field_poid,
link_url: item.attributes.field_lien.uri,
logo_alt: item.relationships.field_logo.data.meta.alt,
logo_url: {
original: logoFetch.data.data.attributes.uri.url,
small: logoFetch.data.data.attributes.image_style_uri.content_small,
medium: logoFetch.data.data.attributes.image_style_uri.content_medium,
large: logoFetch.data.data.attributes.image_style_uri.content_large,
}
}))
);
return await Promise.all(logoPromises);
}
export async function getGouvernance(rawContent) {
const itemPromises = rawContent.map(item =>
REST.get(item.relationships.field_personne_s.links.related.href)
.then(async personnesFetch => {
const portraitPromises = personnesFetch.data.data.map(personne =>
REST.get(personne.relationships.field_portrait.links.related.href)
.then(portraitFetch => ({
nom: personne.attributes.field_nom,
prenom: personne.attributes.field_prenom,
description: personne.attributes.field_description,
photo_meta: personne.relationships.field_portrait.data?.meta.alt,
photo_url: portraitFetch.data.data ? {
original: portraitFetch.data.data.attributes.uri.url,
small: portraitFetch.data.data.attributes.image_style_uri.content_small,
medium: portraitFetch.data.data.attributes.image_style_uri.content_medium,
large: portraitFetch.data.data.attributes.image_style_uri.content_large,
} : null
}))
);
return Promise.all(portraitPromises)
.then(personnes => ({
title: item.attributes.title,
weight: item.attributes.field_poid,
personnes
}));
})
);
return await Promise.all(itemPromises);
}