fix: 🐛 update data recursion in configuration_pages.show and dynamic_pages.show

This commit is contained in:
Alexis Gousseau 2025-12-18 14:45:18 +01:00
parent ebf16a916a
commit 075a7f6ea6
12 changed files with 209 additions and 127 deletions

View file

@ -134,6 +134,7 @@ return [
"crop" => "crop",
"slugify" => "slugify",
"filter" => "filter|filters",
"element" => ":count element|:count elements",
"image_cropper" => [
"state_file_uploaded" => "a file is uploaded with success",
"modale_title" => "Image modification",

View file

@ -268,6 +268,7 @@ return [
"latitude" => "latitude",
"longitude" => "longitude",
"color" => "color",
"value" => "value",
"document" => [
"exercice" => "exercice|exercices",
"tag" => "etiquette|etiquettes",

View file

@ -134,6 +134,7 @@ return [
"crop" => "recadrer",
"slugify" => "slugifier",
"filter" => "filtre|filtres",
"element" => ":count élément|:count éléments",
"image_cropper" => [
"state_file_uploaded" => "un fichier à bien été chargé",
"modale_title" => "Modification de l'image",

View file

@ -266,6 +266,7 @@ return [
"latitude" => "latitude",
"longitude" => "longitude",
"color" => "couleur",
"value" => "valeur",
"document" => [
"exercice" => "exercice|exercices",
"tag" => "étiquette|étiquettes",

View file

@ -0,0 +1,104 @@
// * Packages
import { ReactNode } from "react";
// * Components shadcn
import {
Accordion,
AccordionContent,
AccordionItem,
AccordionTrigger,
} from "@/components/ui/accordion";
// * Other
import str from "@/hooks/use-string";
import useTrans from "@/hooks/use-translations";
import { ConfigField } from "@/back/types/global";
import { cn } from "@/lib/utils";
type DynamicPageDetailsProps = {
value: string | object;
config: ConfigField[];
};
export default function DynamicPageDetails({ value, config }: DynamicPageDetailsProps) {
const transLabel = str(useTrans("validation.custom.label")).ucFirst().value();
const transValue = str(useTrans("validation.custom.value")).ucFirst().value();
const tableNotKnownTrans = str(useTrans("back.table_not_known")).ucFirst().value();
const RenderRows = (value: string | number | object, config: ConfigField[], hasBorders: boolean = false, path: string = ""): ReactNode =>
config.map((field, index) => {
const hasChildren = !!field.children?.length;
const accordionKey = path ? `${path}.${field.key}-${index}` : `${field.key}-${index}`;
const rowContent = (hasChildren: boolean = false) => {
const cellClasses = "break-words whitespace-normal";
const placeholderClasses = "text-sm text-muted-foreground break-words whitespace-normal";
const fieldValue = value[field.key as keyof typeof value];
const cellContent = hasChildren
? (() => {
const count = Array.isArray(fieldValue)
? Object.values(fieldValue).length
: 0;
return (
<p className={placeholderClasses}>
{useTrans('back.element', {
params: { count: String(count) },
choice: count > 1 ? 2 : 1,
})}
</p>
);
})()
: fieldValue !== null && fieldValue !== undefined
? <p className={cellClasses}>
{(typeof fieldValue === 'boolean') ? useTrans(fieldValue ? 'back.yes' : 'back.no') : fieldValue}
</p>
: <p className={placeholderClasses}>{tableNotKnownTrans}</p>;
return (
<div className="grid grid-cols-[1fr_1fr_auto] items-center w-full gap-4">
<p className={cellClasses}>{field.label}</p>
{cellContent}
{!hasChildren && <div className="w-[16px]" />}
</div>
);
};
return (
<AccordionItem key={accordionKey} value={accordionKey} className="border-none">
{hasChildren ? (
<AccordionTrigger className={cn("gap-0 px-4", { "rounded-none border-b mb-2": hasBorders }, { "py-2": !hasBorders })}>
{rowContent(hasChildren)}
</AccordionTrigger>
) : (
<div className="px-4 py-2">{rowContent()}</div>
)}
{hasChildren && Array.isArray(value[field.key as keyof typeof value]) && Object.values(value[field.key as keyof typeof value]).length > 0 && (
<AccordionContent className="px-4">
<div className="space-y-1">
{Object.values(value[field.key as keyof typeof value]).map((childItem: any, childIndex: number) => (
<div key={childIndex} className="border rounded-md">
{RenderRows(childItem, field.children!, false, `${accordionKey}-${childIndex}`)}
</div>
))}
</div>
</AccordionContent>
)}
</AccordionItem>
);
});
return (
<Accordion type="multiple" className="w-full divide-y">
{/* HEADER */}
<div className="grid grid-cols-[1fr_1fr_auto] text-sm font-medium border-b gap-4 px-4 py-2">
<div>{transLabel}</div>
<div>{transValue}</div>
<div className="w-[16px]"/>
</div>
{/* ROWS */}
{RenderRows(value, config, true)}
</Accordion>
);
}

View file

@ -0,0 +1,85 @@
// * Packages
import { usePage } from "@inertiajs/react";
import { ReactNode } from "react";
// * Components shadcn
import {
Accordion,
AccordionContent,
AccordionItem,
AccordionTrigger,
} from "@/components/ui/accordion";
// * Other
import str from "@/hooks/use-string";
import useTrans from "@/hooks/use-translations";
import { ConfigField, DynamicFieldTypes } from "@/back/types/global";
import { cn } from "@/lib/utils";
type ConfigDetailsProps = {
config: ConfigField[];
};
export default function ConfigDetails({ config }: ConfigDetailsProps) {
const enumDynamicFieldTypes = usePage().props.enumDynamicFieldTypes as DynamicFieldTypes;
const getEnumDynamicFieldType = (value: number) => {
return Object.values(enumDynamicFieldTypes).find((def) => def.value === value);
};
const transType = str(useTrans("validation.custom.type")).ucFirst().value();
const transLabel = str(useTrans("validation.custom.label")).ucFirst().value();
const transValidation = str(useTrans("validation.custom.validation")).ucFirst().value();
const RenderRows = (fields: ConfigField[], hasBorders: boolean = false): ReactNode =>
fields.map((field, index) => {
const hasChildren = !!field.children?.length;
const accordionKey = `${field.key}-${index}`;
const rowContent = (hasChildren: boolean = false) => (
<div className="grid grid-cols-[1fr_1fr_1fr_auto] items-center w-full gap-4">
<p className="break-words">{getEnumDynamicFieldType(field.type)?.label}</p>
<p className="break-words whitespace-normal">
{field.label}
<code className="text-xs ms-1 text-muted-foreground break-words break-all whitespace-normal">{`(${field.key})`}</code>
</p>
<div className="text-sm text-muted-foreground break-words">
{field.validation || "—"}
</div>
{!hasChildren && <div className="w-[16px]" />}
</div>
);
return (
<AccordionItem key={accordionKey} value={accordionKey} className="border-none">
{hasChildren ? (
<AccordionTrigger className={cn("gap-0 px-4", { "rounded-none border-b mb-2": hasBorders }, { "py-2": !hasBorders })}>
{rowContent(hasChildren)}
</AccordionTrigger>
) : (
<div className="px-4 py-2">{rowContent()}</div>
)}
{hasChildren && (
<AccordionContent className="pl-4">
<div className="border rounded-md space-y-1">
{RenderRows(field.children!, false)}
</div>
</AccordionContent>
)}
</AccordionItem>
);
});
return (
<Accordion type="multiple" className="w-full divide-y">
{/* HEADER */}
<div className="grid grid-cols-[1fr_1fr_1fr_auto] text-sm font-medium border-b gap-4 px-4 py-2">
<div>{transType}</div>
<div>{transLabel}</div>
<div>{transValidation}</div>
<div className="w-[16px]"/>
</div>
{/* ROWS */}
{RenderRows(config, true)}
</Accordion>
);
}

View file

@ -1,84 +0,0 @@
// * Packages
import { usePage } from "@inertiajs/react";
import { CornerDownRightIcon } from "lucide-react";
import { ReactNode } from "react";
// * Components shadcn
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
// * Other
import str from "@/hooks/use-string";
import useTrans from "@/hooks/use-translations";
import { ConfigField, DynamicFieldTypes } from "@/back/types/global";
type ConfigTableProps = {
config: ConfigField[];
};
export default function ConfigTable({ config }: ConfigTableProps) {
// Enums.
const enumDynamicFieldTypes = usePage().props.enumDynamicFieldTypes as DynamicFieldTypes;
const getEnumDynamicFieldType = (value: number) => {
return Object.values(enumDynamicFieldTypes).find((def) => def.value === value);
};
// Translations.
const transType = str(useTrans("validation.custom.type")).ucFirst().value();
const transLabel = str(useTrans("validation.custom.label")).ucFirst().value()
const transValidation = str(useTrans("validation.custom.validation")).ucFirst().value();
// Recursive component.
const GetChildrenRow = (fields: ConfigField[], depth: number = 0): ReactNode => {
return Object.entries(fields).map(
([id, field]) => (
<>
<TableRow key={id}>
<TableCell>
<div className="flex justify-start items-center gap-1.5">
{depth > 0 && <CornerDownRightIcon size={18} />}
{depth > 0 && <span className="text-muted-foreground">#{depth}</span>}
{getEnumDynamicFieldType(field.type)?.label}
</div>
</TableCell>
<TableCell>
{field.label}
<code className="text-xs ms-1">
({field.key})
</code>
</TableCell>
<TableCell>
{field.validation}
</TableCell>
</TableRow>
{field.children && GetChildrenRow(field.children, depth + 1)}
</>
)
)
}
return (
<Table>
<TableHeader>
<TableRow>
<TableHead>
{transType}
</TableHead>
<TableHead>
{transLabel}
</TableHead>
<TableHead>
{transValidation}
</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{GetChildrenRow(config)}
</TableBody>
</Table>
);
}

View file

@ -23,7 +23,7 @@ import EnumSelect from "@/back/Components/Inputs/EnumSelect";
import ModelSelect from "@/back/Components/Inputs/ModelSelect";
import DynamicInputs from "@/back/Components/DynamicPages/DynamicInputs";
import FieldsetWrapper from "@/back/Components/FieldsetWrapper";
import ConfigTable from "@/back/Components/PageConfigs/ConfigTable";
import ConfigDetails from "@/back/Components/PageConfigs/ConfigDetails";
import BtnOptionnalLinkWithTooltip from "@/back/Components/BtnOptionnalLinkWithTooltip";
import GoToFrontView from "@/back/Components/GoToFrontView";
import useTrans from "@/hooks/use-translations";
@ -312,7 +312,7 @@ export default function Form({
</DialogDescription>
</DialogHeader>
<div className="max-h-[25rem] overflow-y-auto">
<ConfigTable
<ConfigDetails
config={getConfig(
Number(data.config_id),
)}

View file

@ -4,19 +4,12 @@ import { usePage } from "@inertiajs/react";
import { route } from "ziggy-js";
// * Components shadcn
import { Badge } from "@/components/ui/badge";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
// * Other
import AuthenticatedLayout from "@/back/Layouts/AuthenticatedLayout";
import DetailsTable from "@/back/Components/Table/DetailsTable";
import ModelActions from "@/back/Components/ModelActions";
import DynamicStatusBadge from "@/back/Components/DynamicPages/DynamicStatusBadge";
import RecursiveContent from "@/back/Components/DynamicPages/DynamicPageDetails";
import { Views } from "@/lib/utils";
import { useFormatedDate } from "@/hooks/use-formated-value";
import {
@ -105,42 +98,22 @@ export default function Show({ data, modelName }: { data: SingleResource, modelN
accessorKey: "content",
header: str(useTrans("validation.attributes.content")).ucFirst().value(),
cell: ({ value }: { value: TranslationField<object | string> }) => {
if (typeof value[appEnv.locale] === "object") {
return <Table>
<TableHeader>
<TableRow>
<TableHead>
{str(useTrans("validation.custom.reference")).ucFirst().value()}
</TableHead>
<TableHead>
{str(useTrans("validation.attributes.content")).ucFirst().value()}
</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{Object.entries(value[appEnv.locale]).map(
([id, field]) => (
<TableRow key={id}>
<TableCell>
{id}
</TableCell>
<TableCell>
{field}
</TableCell>
</TableRow>
),
)}
</TableBody>
</Table>
if (typeof value[appEnv.locale] === "object" && dynamicPageModel.config?.config?.[appEnv.locale]) {
return (
<RecursiveContent
value={value[appEnv.locale]}
config={dynamicPageModel.config.config[appEnv.locale]}
/>
);
} else if (typeof value[appEnv.locale] === "string" && String(value[appEnv.locale]).trim().length > 0) {
return <p>{String(value[appEnv.locale])}</p>
};
return <p>{String(value[appEnv.locale])}</p>;
}
return (
<span className="text-sm italic text-muted-foreground">
{tableNotKnownTrans}
</span>
);
}
},
},
{
accessorKey: "status",

View file

@ -32,7 +32,6 @@ export default function Show({ data, modelName, news }: { data: SingleResource,
};
const { viewAny } = auth.policies.newsCategories;
const model = data.data as NewsCategoryModel;
console.log({news, model})
const actionModels = [
Views.EDITION,
Views.DELETION,

View file

@ -7,7 +7,7 @@ import { Badge } from "@/components/ui/badge";
import AuthenticatedLayout from "@/back/Layouts/AuthenticatedLayout";
import DetailsTable from "@/back/Components/Table/DetailsTable";
import ModelActions from "@/back/Components/ModelActions";
import ConfigTable from "@/back/Components/PageConfigs/ConfigTable";
import ConfigDetails from "@/back/Components/PageConfigs/ConfigDetails";
import { Views } from "@/lib/utils";
import { useFormatedDate } from "@/hooks/use-formated-value";
import {
@ -51,7 +51,7 @@ export default function Show({ data, modelName }: { data: SingleResource, modelN
accessorKey: "config",
header: str(useTrans("validation.custom.config")).ucFirst().value(),
cell: ({ value }: { value: TranslationField<ConfigField[]> }) => value[appEnv.locale]
? <ConfigTable config={value[appEnv.locale]} />
? <ConfigDetails config={value[appEnv.locale]} />
: (<p className="text-muted-foreground italic m-0">{useTrans('back.table_not_known')}</p>),
},
{

View file

@ -67,6 +67,7 @@ type DynamicPageModel = {
deleted_at: Date;
parent_page: DynamicPageModel | null;
children_page: DynamicPageModel[];
config: PageConfigModel;
};
type DynamicFieldTypes = Record<
string,