From 3225788bd475ff27a537f1eefc8e4da4bbaa14ff Mon Sep 17 00:00:00 2001 From: Regourd Colin Date: Mon, 7 Oct 2024 12:03:28 +0200 Subject: [PATCH] Fix image (#472) --- .changeset/witty-snakes-shout.md | 5 +++++ apps/example/options.tsx | 2 +- .../src/components/inputs/FileWidget.tsx | 18 ++++++++++++++---- packages/next-admin/src/utils/server.ts | 7 ++++--- 4 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 .changeset/witty-snakes-shout.md diff --git a/.changeset/witty-snakes-shout.md b/.changeset/witty-snakes-shout.md new file mode 100644 index 00000000..218d6418 --- /dev/null +++ b/.changeset/witty-snakes-shout.md @@ -0,0 +1,5 @@ +--- +"@premieroctet/next-admin": patch +--- + +Fix image (get async) diff --git a/apps/example/options.tsx b/apps/example/options.tsx index 0b9025a9..3c281835 100644 --- a/apps/example/options.tsx +++ b/apps/example/options.tsx @@ -1,5 +1,5 @@ -import UserDetailsDialog from "@/components/UserDetailsDialogContent"; import AddTagDialog from "@/components/PostAddTagDialogContent"; +import UserDetailsDialog from "@/components/UserDetailsDialogContent"; import { NextAdminOptions } from "@premieroctet/next-admin"; import DatePicker from "./components/DatePicker"; import PasswordInput from "./components/PasswordInput"; diff --git a/packages/next-admin/src/components/inputs/FileWidget.tsx b/packages/next-admin/src/components/inputs/FileWidget.tsx index 35276853..e0fb30e0 100644 --- a/packages/next-admin/src/components/inputs/FileWidget.tsx +++ b/packages/next-admin/src/components/inputs/FileWidget.tsx @@ -9,12 +9,12 @@ import { ChangeEvent, useEffect, useRef, useState } from "react"; import Loader from "../../assets/icons/Loader"; import { useFormState } from "../../context/FormStateContext"; import { useI18n } from "../../context/I18nContext"; -import { getFilenameFromUrl, isImageType } from "../../utils/file"; +import { getFilenameFromUrl } from "../../utils/file"; const FileWidget = (props: WidgetProps) => { const [file, setFile] = useState(); const [errors, setErrors] = useState(props.rawErrors); - const [fileIsImage, setFileIsImage] = useState(false); + const [fileIsImage, setFileIsImage] = useState(true); const [filename, setFilename] = useState(null); const [fileUrl, setFileUrl] = useState(props.value); const [isPending, setIsPending] = useState(false); @@ -35,12 +35,22 @@ const FileWidget = (props: WidgetProps) => { useEffect(() => { if (props.value) { setIsPending(true); - setFileUrl(props.value); + + const image = document.createElement("img"); + image.src = props.value as string; + image.onload = () => { + setFileIsImage(true); + setIsPending(false); + }; + image.onerror = (e) => { + console.error(e); + setFileIsImage(false); + setIsPending(false); + }; const filename = getFilenameFromUrl(props.value); if (filename) { setFilename(filename); } - setFileIsImage(isImageType(props.value)); setIsPending(false); } else { setIsPending(false); diff --git a/packages/next-admin/src/utils/server.ts b/packages/next-admin/src/utils/server.ts index e39729ec..182fb51c 100644 --- a/packages/next-admin/src/utils/server.ts +++ b/packages/next-admin/src/utils/server.ts @@ -317,7 +317,8 @@ export const transformData = ( const model = models.find((model) => model.name === modelName); if (!model) return data; - return Object.keys(data).reduce((acc, key) => { + return Object.keys(data).reduce(async (accP, key) => { + const acc = await accP; const field = model.fields?.find((field) => field.name === key); const fieldKind = field?.kind; const get = editOptions?.fields?.[key as Field]?.handler?.get; @@ -326,7 +327,7 @@ export const transformData = ( editOptions?.fields?.[key as Field]?.relationshipSearchField; if (get) { - acc[key] = get(data[key]); + acc[key] = await get(data[key]); } else if (fieldKind === "enum") { const value = data[key]; if (Array.isArray(value)) { @@ -414,7 +415,7 @@ export const transformData = ( } } return acc; - }, {} as any); + }, Promise.resolve({}) as any); }; /**