diff --git a/lang/en/back.php b/lang/en/back.php index eabadd4f..acac7169 100644 --- a/lang/en/back.php +++ b/lang/en/back.php @@ -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", diff --git a/lang/en/validation.php b/lang/en/validation.php index e0dd5237..84368cef 100644 --- a/lang/en/validation.php +++ b/lang/en/validation.php @@ -268,6 +268,7 @@ return [ "latitude" => "latitude", "longitude" => "longitude", "color" => "color", + "value" => "value", "document" => [ "exercice" => "exercice|exercices", "tag" => "etiquette|etiquettes", diff --git a/lang/fr/back.php b/lang/fr/back.php index 51a65b3e..c4e9aa90 100644 --- a/lang/fr/back.php +++ b/lang/fr/back.php @@ -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", diff --git a/lang/fr/validation.php b/lang/fr/validation.php index 967fdbd2..5de03029 100644 --- a/lang/fr/validation.php +++ b/lang/fr/validation.php @@ -266,6 +266,7 @@ return [ "latitude" => "latitude", "longitude" => "longitude", "color" => "couleur", + "value" => "valeur", "document" => [ "exercice" => "exercice|exercices", "tag" => "étiquette|étiquettes", diff --git a/resources/ts/back/Components/DynamicPages/DynamicPageDetails.tsx b/resources/ts/back/Components/DynamicPages/DynamicPageDetails.tsx new file mode 100644 index 00000000..d665266d --- /dev/null +++ b/resources/ts/back/Components/DynamicPages/DynamicPageDetails.tsx @@ -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 ( +

+ {useTrans('back.element', { + params: { count: String(count) }, + choice: count > 1 ? 2 : 1, + })} +

+ ); + })() + : fieldValue !== null && fieldValue !== undefined + ?

+ {(typeof fieldValue === 'boolean') ? useTrans(fieldValue ? 'back.yes' : 'back.no') : fieldValue} +

+ :

{tableNotKnownTrans}

; + + return ( +
+

{field.label}

+ {cellContent} + {!hasChildren &&
} +
+ ); + }; + + return ( + + {hasChildren ? ( + + {rowContent(hasChildren)} + + ) : ( +
{rowContent()}
+ )} + + {hasChildren && Array.isArray(value[field.key as keyof typeof value]) && Object.values(value[field.key as keyof typeof value]).length > 0 && ( + +
+ {Object.values(value[field.key as keyof typeof value]).map((childItem: any, childIndex: number) => ( +
+ {RenderRows(childItem, field.children!, false, `${accordionKey}-${childIndex}`)} +
+ ))} +
+
+ )} +
+ ); + }); + + return ( + + {/* HEADER */} +
+
{transLabel}
+
{transValue}
+
+
+ + {/* ROWS */} + {RenderRows(value, config, true)} + + ); +} diff --git a/resources/ts/back/Components/PageConfigs/ConfigDetails.tsx b/resources/ts/back/Components/PageConfigs/ConfigDetails.tsx new file mode 100644 index 00000000..a1b8b67c --- /dev/null +++ b/resources/ts/back/Components/PageConfigs/ConfigDetails.tsx @@ -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) => ( +
+

{getEnumDynamicFieldType(field.type)?.label}

+

+ {field.label} + {`(${field.key})`} +

+
+ {field.validation || "—"} +
+ {!hasChildren &&
} +
+ ); + + return ( + + {hasChildren ? ( + + {rowContent(hasChildren)} + + ) : ( +
{rowContent()}
+ )} + + {hasChildren && ( + +
+ {RenderRows(field.children!, false)} +
+
+ )} +
+ ); + }); + + return ( + + {/* HEADER */} +
+
{transType}
+
{transLabel}
+
{transValidation}
+
+
+ + {/* ROWS */} + {RenderRows(config, true)} + + ); +} diff --git a/resources/ts/back/Components/PageConfigs/ConfigTable.tsx b/resources/ts/back/Components/PageConfigs/ConfigTable.tsx deleted file mode 100644 index e5bd2e98..00000000 --- a/resources/ts/back/Components/PageConfigs/ConfigTable.tsx +++ /dev/null @@ -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]) => ( - <> - - -
- {depth > 0 && } - {depth > 0 && #{depth}} - {getEnumDynamicFieldType(field.type)?.label} -
-
- - {field.label} - - ({field.key}) - - - - {field.validation} - -
- {field.children && GetChildrenRow(field.children, depth + 1)} - - ) - ) - } - - return ( - - - - - {transType} - - - {transLabel} - - - {transValidation} - - - - - {GetChildrenRow(config)} - -
- ); -} \ No newline at end of file diff --git a/resources/ts/back/Pages/DynamicPages/Form.tsx b/resources/ts/back/Pages/DynamicPages/Form.tsx index c62f63c1..da47d94b 100644 --- a/resources/ts/back/Pages/DynamicPages/Form.tsx +++ b/resources/ts/back/Pages/DynamicPages/Form.tsx @@ -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({
- }) => { - if (typeof value[appEnv.locale] === "object") { - return - - - - {str(useTrans("validation.custom.reference")).ucFirst().value()} - - - {str(useTrans("validation.attributes.content")).ucFirst().value()} - - - - - {Object.entries(value[appEnv.locale]).map( - ([id, field]) => ( - - - {id} - - - {field} - - - ), - )} - -
+ if (typeof value[appEnv.locale] === "object" && dynamicPageModel.config?.config?.[appEnv.locale]) { + return ( + + ); } else if (typeof value[appEnv.locale] === "string" && String(value[appEnv.locale]).trim().length > 0) { - return

{String(value[appEnv.locale])}

- }; + return

{String(value[appEnv.locale])}

; + } return ( {tableNotKnownTrans} ); - } + }, }, { accessorKey: "status", diff --git a/resources/ts/back/Pages/NewsCategory/Show.tsx b/resources/ts/back/Pages/NewsCategory/Show.tsx index e78825d2..39c95b75 100644 --- a/resources/ts/back/Pages/NewsCategory/Show.tsx +++ b/resources/ts/back/Pages/NewsCategory/Show.tsx @@ -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, diff --git a/resources/ts/back/Pages/PageConfigs/Show.tsx b/resources/ts/back/Pages/PageConfigs/Show.tsx index 31838618..146bc8d7 100644 --- a/resources/ts/back/Pages/PageConfigs/Show.tsx +++ b/resources/ts/back/Pages/PageConfigs/Show.tsx @@ -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 }) => value[appEnv.locale] - ? + ? : (

{useTrans('back.table_not_known')}

), }, { diff --git a/resources/ts/back/types/global.d.ts b/resources/ts/back/types/global.d.ts index 03b887da..2f2e5046 100644 --- a/resources/ts/back/types/global.d.ts +++ b/resources/ts/back/types/global.d.ts @@ -67,6 +67,7 @@ type DynamicPageModel = { deleted_at: Date; parent_page: DynamicPageModel | null; children_page: DynamicPageModel[]; + config: PageConfigModel; }; type DynamicFieldTypes = Record< string,