From 790f5b7c8d1ba9d4816760b88b664d30ea818949 Mon Sep 17 00:00:00 2001 From: Serkan Reis Date: Mon, 1 Aug 2022 12:28:12 +0300 Subject: [PATCH] Check matching names and length for assets and metadata (#10) * Name and length check for assets & metadata * Disable update_metadata button on metadata modal if no metadatafile is specified --- components/MetadataModal.tsx | 4 +- pages/collections/upload.tsx | 139 ++++++++++++++++++++++------------- 2 files changed, 89 insertions(+), 54 deletions(-) diff --git a/components/MetadataModal.tsx b/components/MetadataModal.tsx index 74dbf9d..d44f098 100644 --- a/components/MetadataModal.tsx +++ b/components/MetadataModal.tsx @@ -122,7 +122,9 @@ export const MetadataModal = (props: MetadataModalProps) => { subtitle="Enter trait types and values" title="Attributes" /> - + diff --git a/pages/collections/upload.tsx b/pages/collections/upload.tsx index 3963d66..a84ca44 100644 --- a/pages/collections/upload.tsx +++ b/pages/collections/upload.tsx @@ -83,7 +83,7 @@ const UploadPage: NextPage = () => { reader.onload = function (e) { if (!e.target?.result) return toast.error('Error parsing file.') if (!event.target.files) return toast.error('No files selected.') - const metadataFile = new File([e.target.result], event.target.files[i].name, { type: 'image/jpg' }) + const metadataFile = new File([e.target.result], event.target.files[i].name, { type: 'application/json' }) setMetadataFilesArray((prev) => [...prev, metadataFile]) } if (!event.target.files) return toast.error('No file selected.') @@ -95,6 +95,16 @@ const UploadPage: NextPage = () => { } } const updateMetadata = async () => { + const metadataFileNames = metadataFilesArray.map((file) => file.name) + console.log(metadataFileNames) + const imageFileNames = imageFilesArray.map((file) => file.name.substring(0, file.name.lastIndexOf('.'))) + console.log(imageFileNames) + //compare the two arrays to make sure they are the same + const areArraysEqual = metadataFileNames.every((val, index) => val === imageFileNames[index]) + if (!areArraysEqual) { + return toast.error('Asset and metadata file names do not match.') + } + console.log(imageFilesArray) const imageURI = await upload( imageFilesArray, @@ -326,60 +336,83 @@ const UploadPage: NextPage = () => {
- -
- + +
+ > + +
- -
- -
+ + The number of assets and metadata files should match. + +
-
@@ -390,10 +423,9 @@ const UploadPage: NextPage = () => { updatedMetadataFile={updatedMetadataFilesArray[metadataFileArrayIndex]} />
- -
- {imageFilesArray.length > 0 && - imageFilesArray.map((imageSource, index) => ( + {imageFilesArray.length > 0 && ( +
+ {imageFilesArray.map((imageSource, index) => (
4 * index}> @@ -499,7 +531,8 @@ const UploadPage: NextPage = () => {
))} -
+
+ )}