From 4f9e8874d16b33c63e1997eee86557971e0c8de7 Mon Sep 17 00:00:00 2001 From: Brad Traversy Date: Thu, 11 Jul 2024 16:33:42 -0400 Subject: [PATCH] Profile & property listings --- _theme_files/profile.html | 67 ++++++++++---------------------- app/profile/page.jsx | 65 +++++++++++++++++++++++++++++++ components/ProfileProperties.jsx | 45 +++++++++++++++++++++ utils/convertToObject.js | 14 +++++++ 4 files changed, 145 insertions(+), 46 deletions(-) create mode 100644 app/profile/page.jsx create mode 100644 components/ProfileProperties.jsx create mode 100644 utils/convertToObject.js diff --git a/_theme_files/profile.html b/_theme_files/profile.html index 84edeb3..f92066d 100644 --- a/_theme_files/profile.html +++ b/_theme_files/profile.html @@ -231,57 +231,31 @@ /> -

Name: John Doe

-

Email: john@gmail.com

- - - +

+ Name: John Doe +

+

+ Email: john@gmail.com +

Your Listings

- Property 1 + Property 1

Property Title 1

Address: 123 Main St

- Property 2 - + Property 2 +

Property Title 2

Address: 456 Elm St

- Edit diff --git a/app/profile/page.jsx b/app/profile/page.jsx new file mode 100644 index 0000000..4bd9bf2 --- /dev/null +++ b/app/profile/page.jsx @@ -0,0 +1,65 @@ +import Image from 'next/image'; +import connectDB from '@/config/database'; +import Property from '@/models/Property'; +import { getSessionUser } from '@/utils/getSessionUser'; +import profileDefault from '@/assets/images/profile.png'; +import ProfileProperties from '@/components/ProfileProperties'; +import { convertToSerializeableObject } from '@/utils/convertToObject'; + +const ProfilePage = async () => { + await connectDB(); + + const sessionUser = await getSessionUser(); + + const { userId } = sessionUser; + + if (!userId) { + throw new Error('User ID is required'); + } + + const propertiesDocs = await Property.find({ owner: userId }).lean(); + const properties = propertiesDocs.map(convertToSerializeableObject); + + return ( +
+
+
+

Your Profile

+
+
+
+ User +
+ +

+ Name: {' '} + {sessionUser.user.name} +

+

+ Email: {' '} + {sessionUser.user.email} +

+
+ +
+

Your Listings

+ {properties.length === 0 ? ( +

You have no property listings

+ ) : ( + + )} +
+
+
+
+
+ ); +}; + +export default ProfilePage; diff --git a/components/ProfileProperties.jsx b/components/ProfileProperties.jsx new file mode 100644 index 0000000..ea5d61f --- /dev/null +++ b/components/ProfileProperties.jsx @@ -0,0 +1,45 @@ +'use client'; +import { useState } from 'react'; +import Image from 'next/image'; +import Link from 'next/link'; + +const ProfileProperties = ({ properties: initialProperties }) => { + const [properties, setProperties] = useState(initialProperties); + return properties.map((property) => ( +
+ + + +
+

{property.name}

+

+ Address: {property.location.street} {property.location.city}{' '} + {property.location.state} +

+
+
+ + Edit + + +
+
+ )); +}; + +export default ProfileProperties; diff --git a/utils/convertToObject.js b/utils/convertToObject.js new file mode 100644 index 0000000..bba47b5 --- /dev/null +++ b/utils/convertToObject.js @@ -0,0 +1,14 @@ +/** + * Converts a Mongoose lean document into a serializable plain JavaScript object. + * + * @param {Object} leanDocument - The Mongoose lean document to be converted. + * @returns {Object} A plain JavaScript object that is a serializable representation of the input document. + */ + +export function convertToSerializeableObject(leanDocument) { + for (const key of Object.keys(leanDocument)) { + if (leanDocument[key].toJSON && leanDocument[key].toString) + leanDocument[key] = leanDocument[key].toString(); + } + return leanDocument; +}