diff --git a/_theme_files/add-property.html b/_theme_files/add-property.html index dffcfe8..dfede2e 100644 --- a/_theme_files/add-property.html +++ b/_theme_files/add-property.html @@ -601,6 +601,7 @@ class="border rounded w-full py-2 px-3" accept="image/*" multiple + required /> diff --git a/_theme_files/images/properties/a2 copy.jpg b/_theme_files/images/properties/a2 copy.jpg deleted file mode 100644 index cda4057..0000000 Binary files a/_theme_files/images/properties/a2 copy.jpg and /dev/null differ diff --git a/app/actions/addProperty.js b/app/actions/addProperty.js index fb1ec81..d925890 100644 --- a/app/actions/addProperty.js +++ b/app/actions/addProperty.js @@ -4,6 +4,7 @@ import Property from '@/models/Property'; import { getSessionUser } from '@/utils/getSessionUser'; import { revalidatePath } from 'next/cache'; import { redirect } from 'next/navigation'; +import cloudinary from '@/config/cloudinary'; async function addProperty(formData) { await connectDB(); @@ -18,10 +19,7 @@ async function addProperty(formData) { // Access all values for amenities and images const amenities = formData.getAll('amenities'); - const images = formData - .getAll('images') - .filter((image) => image.name !== '') // Filter out empty names - .map((image) => image.name); // Extract only the names + const images = formData.getAll('images').filter((image) => image.name !== ''); // Create the propertyData object with embedded seller_info const propertyData = { @@ -48,10 +46,32 @@ async function addProperty(formData) { email: formData.get('seller_info.email'), phone: formData.get('seller_info.phone'), }, - images, owner: userId, }; + const imageUrls = []; + + for (const imageFile of images) { + const imageBuffer = await imageFile.arrayBuffer(); + const imageArray = Array.from(new Uint8Array(imageBuffer)); + const imageData = Buffer.from(imageArray); + + // Convert the image data to base64 + const imageBase64 = imageData.toString('base64'); + + // Make request to upload to Cloudinary + const result = await cloudinary.uploader.upload( + `data:image/png;base64,${imageBase64}`, + { + folder: 'propertypulse', + } + ); + + imageUrls.push(result.secure_url); + } + + propertyData.images = imageUrls; + const newProperty = new Property(propertyData); await newProperty.save(); diff --git a/components/PropertyAddForm.jsx b/components/PropertyAddForm.jsx index da03e2d..f9aa984 100644 --- a/components/PropertyAddForm.jsx +++ b/components/PropertyAddForm.jsx @@ -389,6 +389,7 @@ const PropertyAddForm = () => { className='border rounded w-full py-2 px-3' accept='image/*' multiple + required /> diff --git a/components/PropertyCard.jsx b/components/PropertyCard.jsx index 27e4079..54083e9 100644 --- a/components/PropertyCard.jsx +++ b/components/PropertyCard.jsx @@ -23,7 +23,7 @@ const PropertyCard = ({ property }) => { return (