From 2162230141881f95d4e103b5e475a9d195c35e0f Mon Sep 17 00:00:00 2001 From: Renaud Heluin Date: Fri, 16 Feb 2024 11:59:56 +0000 Subject: [PATCH] Refactor code for FichesFilter and CardView components --- src/components/CardView.astro | 22 +- src/components/fiches/FichesCard.astro | 44 ++-- src/components/fiches/FichesFilter.astro | 1 + .../fiches/FichesMetasDisplay.astro | 224 +++++++++--------- 4 files changed, 140 insertions(+), 151 deletions(-) diff --git a/src/components/CardView.astro b/src/components/CardView.astro index 4fe0555..32c5612 100644 --- a/src/components/CardView.astro +++ b/src/components/CardView.astro @@ -1,30 +1,10 @@ --- -import { getLangFromUrl, useTranslations } from "@i18n/utils"; +import { getLangFromUrl } from "@i18n/utils"; import type { CollectionEntry } from "astro:content"; import Card from "./Card"; import FichesCard from "./fiches/FichesCard.astro"; -const REF_NAME = process.env.PUBLIC_REF_NAME; - const lang = getLangFromUrl(Astro.url); -const t = useTranslations(lang); - -function cleanImpact(meta) { - // TODO: replace with a proper translation - let regexp: RegExp; - switch (getLangFromUrl(Astro.url)) { - case "es": - regexp = /t("Fort")|t("Moyen")|t("Faible")/gi; - break; - case "en": - regexp = /t("High")|t("Medium")|t("Low")/gi; - break; - default: - regexp = /t("Fuerte")|t("Medio")|t("Débil")/gi; - break; - } - return entry.data[`${meta}`].replace(regexp, " "); -} type Props = { entry: CollectionEntry<"fiches" | "lexique" | "personas"> | any; diff --git a/src/components/fiches/FichesCard.astro b/src/components/fiches/FichesCard.astro index d7963b6..96012b2 100644 --- a/src/components/fiches/FichesCard.astro +++ b/src/components/fiches/FichesCard.astro @@ -33,7 +33,7 @@ function cleanImpact(meta) { regexp = /t("Fuerte")|t("Medio")|t("Débil")/gi; break; } - return entry.data[`${meta}`].replace(regexp, " "); + return entry.data[`${meta}`]?.replace(regexp, " ") || "TBD"; } const getScope = () => { @@ -59,19 +59,35 @@ const getScope = () => { {entry.data.title}
- - { - typeof entry.data[`priority_implementation`] === "number" - ? entry.data[`priority_implementation`] - : t(cleanImpact("priority_implementation")) - } - + { + typeof entry.data[`priority_implementation`] === "number" && ( + + {t("Priorité d'implémentation") + + " " + + entry.data[`priority_implementation`]} + + ) + } + { + typeof entry.data[`priority_implementation`] !== "number" && ( + + {t(cleanImpact("priority_implementation"))} + + ) + } | { getRefConfig().featuresEnabled.environmental_impact === MESURE_ON_3 && ( diff --git a/src/components/fiches/FichesFilter.astro b/src/components/fiches/FichesFilter.astro index 2157bcf..0b3b4dc 100644 --- a/src/components/fiches/FichesFilter.astro +++ b/src/components/fiches/FichesFilter.astro @@ -30,6 +30,7 @@ const getDatas = async () => { if (Array.isArray(entry.data[key])) { datas .find((data) => data.name === key) + // @ts-ignore .values.push(...entry.data[key]); } else { datas diff --git a/src/components/fiches/FichesMetasDisplay.astro b/src/components/fiches/FichesMetasDisplay.astro index 96012b2..d3835b9 100644 --- a/src/components/fiches/FichesMetasDisplay.astro +++ b/src/components/fiches/FichesMetasDisplay.astro @@ -1,142 +1,134 @@ --- -import { getLangFromUrl, isStringInUi, useTranslations } from "@i18n/utils"; -import { slugify } from "@js/utils"; -import type { CollectionEntry } from "astro:content"; -import { - MESURE_ON_3, - MESURE_ON_5, - getRefConfig, -} from "../../../referentiel-config"; +import { getLangFromUrl, useTranslations } from "@i18n/utils"; +import { MESURE_ON_5, getRefConfig } from "../../../referentiel-config"; +import MetaItem from "./MetaItem.astro"; type Props = { - entry: CollectionEntry<"fiches">; + className?: string; + frontmatter: any; + tableOfContents: Array<{ depth: number; text: string; slug: string }>; }; +const { className, frontmatter, tableOfContents } = Astro.props; const lang = getLangFromUrl(Astro.url); const t = useTranslations(lang); - -const REF_NAME = import.meta.env.PUBLIC_REF_NAME || process.env.PUBLIC_REF_NAME; - -const { entry } = Astro.props; - -function cleanImpact(meta) { - // TODO: replace with a proper translation - let regexp: RegExp; - switch (getLangFromUrl(Astro.url)) { - case "es": - regexp = /t("Fort")|t("Moyen")|t("Faible")/gi; - break; - case "en": - regexp = /t("High")|t("Medium")|t("Low")/gi; - break; - default: - regexp = /t("Fuerte")|t("Medio")|t("Débil")/gi; - break; - } - return entry.data[`${meta}`]?.replace(regexp, " ") || "TBD"; -} - -const getScope = () => { - if (getRefConfig().featuresEnabled.scope === true) { - return { "data-scope": slugify(entry.data.scope || "") }; - } -}; ---
-

- - {REF_NAME} - {entry.data.refID} - - {entry.data.title} -

-
- { - typeof entry.data[`priority_implementation`] === "number" && ( - - {t("Priorité d'implémentation") + - " " + - entry.data[`priority_implementation`]} - - ) - } +
    + + + + { - typeof entry.data[`priority_implementation`] !== "number" && ( - - {t(cleanImpact("priority_implementation"))} - + getRefConfig().featuresEnabled.moe === true && ( + ) } - | { - getRefConfig().featuresEnabled.environmental_impact === MESURE_ON_3 && ( - - {t(cleanImpact("environmental_impact"))} - + getRefConfig().featuresEnabled.priority_implementation === MESURE_ON_5 && + getRefConfig().featuresEnabled.moe === true ? ( + + ) : ( + ) } { - getRefConfig().featuresEnabled.environmental_impact === MESURE_ON_5 && ( - - {`${t("Impact environnemental")} : ${ - entry.data[`environmental_impact`] - }`} - - ) + getRefConfig().featuresEnabled.perimetre === true ? ( + + ) : null } + +
+
+
    + +
- - {/* @ts-ignore */} - {isStringInUi(lang, entry.data.lifecycle) && t(entry.data.lifecycle)} - +
+
+

{t("Sommaire :")}

{ - getRefConfig().featuresEnabled.scope === true && ( - - {/* @ts-ignore */} - {isStringInUi(lang, entry.data.scope) && t(entry.data.scope)} - - ) + /* + + */ } +