diff --git a/src/components/forms/form-template-form.js b/src/components/forms/form-template-form.js index 3123cdd09..a3a517785 100644 --- a/src/components/forms/form-template-form.js +++ b/src/components/forms/form-template-form.js @@ -22,7 +22,12 @@ import TextEditorV3 from "openstack-uicore-foundation/lib/components/inputs/edit import Swal from "sweetalert2"; import FormRepeater from "../form-repeater"; import FormTemplateMetaFieldForm from "./form-template-meta-field-form"; -import { scrollToError, shallowEqual, hasErrors } from "../../utils/methods"; +import { + scrollToError, + shallowEqual, + hasErrors, + getMediaInputValue +} from "../../utils/methods"; import { MAX_FORM_TEMPLATE_MATERIALS_UPLOAD_SIZE, MAX_FORM_TEMPLATE_MATERIALS_UPLOAD_QTY, @@ -100,14 +105,6 @@ const FormTemplateForm = ({ } }; - const getMediaInputValue = () => - entity.materials.length > 0 - ? entity.materials.map((material) => ({ - ...material, - filename: material.filename ?? material.file_path ?? material.file_url - })) - : []; - const handleSubmit = (ev) => { ev.preventDefault(); entity.meta_fields = getNormalizedMetaFields(); @@ -247,7 +244,7 @@ const FormTemplateForm = ({ - entity.images.length > 0 - ? entity.images.map((img) => ({ - ...img, - filename: img.filename ?? img.file_path ?? img.file_url - })) - : []; - const handleSubmit = (ev) => { ev.preventDefault(); entity.meta_fields = getNormalizedMetaFields(); @@ -323,7 +320,7 @@ const InventoryItemForm = ({ - initialEntity.images?.length > 0 - ? initialEntity.images.map((img) => { - const filename = img.filename ?? img.file_path ?? img.file_url; - return { - ...img, - filename: filename.concat("?t=", Date?.now()) - }; - }) - : []; - const handleClose = () => { formik.resetForm(); onClose(); @@ -283,7 +273,7 @@ const SponsorItemDialog = ({ id="image-upload" name="image" onUploadComplete={handleImageUploadComplete} - value={getMediaInputValue()} + value={getMediaInputValue(initialEntity)} mediaType={mediaType} onRemove={handleRemoveImage} postUrl={`${window.FILE_UPLOAD_API_BASE_URL}/api/v1/files/upload`} diff --git a/src/utils/constants.js b/src/utils/constants.js index 93939c429..d26323a42 100644 --- a/src/utils/constants.js +++ b/src/utils/constants.js @@ -155,9 +155,13 @@ export const EVENT_TYPE_FISHBOWL = "Fishbowl"; export const EVENT_TYPE_GROUP_EVENTS = "Groups Events"; +export const FILENAME_TRUNCATE_SIDE_PERCENT = 0.4; + +export const TRIM_TEXT_LENGTH_20 = 20; + export const TRIM_TEXT_LENGTH_50 = 50; -export const TRIM_TEXT_LENGTH_40 = 50; +export const TRIM_TEXT_LENGTH_40 = 40; export const LANGUAGE_CODE_LENGTH = 2; diff --git a/src/utils/methods.js b/src/utils/methods.js index a558014ca..f6d740484 100644 --- a/src/utils/methods.js +++ b/src/utils/methods.js @@ -26,12 +26,14 @@ import { ERROR_CODE_403, ERROR_CODE_412, ERROR_CODE_500, + FILENAME_TRUNCATE_SIDE_PERCENT, HEX_RADIX, INT_BASE, MARKETING_SETTING_TYPE_HEX_COLOR, MILLISECONDS_TO_SECONDS, ONE_MINUTE, - OR_FILTER + OR_FILTER, + TRIM_TEXT_LENGTH_20 } from "./constants"; const DAY_IN_SECONDS = 86400; // 86400 seconds per day @@ -530,3 +532,41 @@ export const formatBadgeQR = (code, summit) => { return null; }; + +export const getMediaInputValue = ( + entity, + fieldName = "images", + maxLength = TRIM_TEXT_LENGTH_20 +) => { + const mediaFiles = entity?.[fieldName]; + + if (!mediaFiles?.length) return []; + + return mediaFiles.map((img) => { + const fileUrl = img.filename ?? img.file_path ?? img.file_url; + + let filename = fileUrl.includes("/") ? fileUrl.split("/").pop() : fileUrl; + + const parts = filename.split("."); + const ext = parts.pop(); + let nameWithoutExtension = parts.join("."); + + if (nameWithoutExtension.length > maxLength) { + const startChars = Math.floor(maxLength * FILENAME_TRUNCATE_SIDE_PERCENT); + const endChars = Math.floor(maxLength * FILENAME_TRUNCATE_SIDE_PERCENT); + const start = nameWithoutExtension.substring(0, startChars); + const end = nameWithoutExtension.substring( + nameWithoutExtension.length - endChars + ); + nameWithoutExtension = `${start}...${end}`; + } + + filename = `${nameWithoutExtension}.${ext}`; + + return { + ...img, + public_url: img?.public_url || fileUrl, + filename: filename.concat("?t=", Date.now()) + }; + }); +};