Edit property

This commit is contained in:
Brad Traversy
2024-07-12 07:47:25 -04:00
parent 5457768fac
commit 1c455ca30f
3 changed files with 516 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
'use server';
import connectDB from '@/config/database';
import Property from '@/models/Property';
import { getSessionUser } from '@/utils/getSessionUser';
import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
async function updateProperty(propertyId, formData) {
await connectDB();
const sessionUser = await getSessionUser();
const { userId } = sessionUser;
const existingProperty = await Property.findById(propertyId);
// Verify ownership
if (existingProperty.owner.toString() !== userId) {
throw new Error('Current user does not own this property.');
}
const propertyData = {
type: formData.get('type'),
name: formData.get('name'),
description: formData.get('description'),
location: {
street: formData.get('location.street'),
city: formData.get('location.city'),
state: formData.get('location.state'),
zipcode: formData.get('location.zipcode'),
},
beds: formData.get('beds'),
baths: formData.get('baths'),
square_feet: formData.get('square_feet'),
amenities: formData.getAll('amenities'),
rates: {
weekly: formData.get('rates.weekly'),
monthly: formData.get('rates.monthly'),
nightly: formData.get('rates.nightly.'),
},
seller_info: {
name: formData.get('seller_info.name'),
email: formData.get('seller_info.email'),
phone: formData.get('seller_info.phone'),
},
owner: userId,
};
const updatedProperty = await Property.findByIdAndUpdate(
propertyId,
propertyData
);
revalidatePath('/', 'layout');
redirect(`/properties/${updatedProperty._id}`);
}
export default updateProperty;
+31
View File
@@ -0,0 +1,31 @@
import PropertyEditForm from '@/components/PropertyEditForm';
import connectDB from '@/config/database';
import Property from '@/models/Property';
import { convertToSerializeableObject } from '@/utils/convertToObject';
const PropertyEditPage = async ({ params }) => {
await connectDB();
const propertyDoc = await Property.findById(params.id).lean();
const property = convertToSerializeableObject(propertyDoc);
if (!property) {
return (
<h1 className='text-center text-2xl font-bold mt-10'>
Property Not Found
</h1>
);
}
return (
<section className='bg-blue-50'>
<div className='container m-auto max-w-2xl py-24'>
<div className='bg-white px-6 py-8 mb-4 shadow-md rounded-md border m-4 md:m-0'>
<PropertyEditForm property={property} />
</div>
</div>
</section>
);
};
export default PropertyEditPage;