diff --git a/app/actions/addMessage.js b/app/actions/addMessage.js new file mode 100644 index 0000000..7ac74c5 --- /dev/null +++ b/app/actions/addMessage.js @@ -0,0 +1,39 @@ +'use server'; +import connectDB from '@/config/database'; +import Message from '@/models/Message'; +import { getSessionUser } from '@/utils/getSessionUser'; +import { revalidatePath } from 'next/cache'; + +async function addMessage(previousState, formData) { + await connectDB(); + + const sessionUser = await getSessionUser(); + + if (!sessionUser || !sessionUser.user) { + return { error: 'You must be logged in to send a message' }; + } + + const { user } = sessionUser; + + const recipient = formData.get('recipient'); + + if (user.id === recipient) { + return { error: 'You can not send a message to yourself' }; + } + + const newMessage = new Message({ + sender: user.id, + recipient, + property: formData.get('property'), + name: formData.get('name'), + email: formData.get('email'), + phone: formData.get('phone'), + body: formData.get('message'), + }); + + await newMessage.save(); + + return { submitted: true }; +} + +export default addMessage; diff --git a/components/PropertyContactForm.jsx b/components/PropertyContactForm.jsx index 1f2decf..d8de8f7 100644 --- a/components/PropertyContactForm.jsx +++ b/components/PropertyContactForm.jsx @@ -1,81 +1,126 @@ +'use client'; +import { useEffect } from 'react'; +import { useFormState, useFormStatus } from 'react-dom'; +import { useSession } from 'next-auth/react'; +import { toast } from 'react-toastify'; +import addMessage from '@/app/actions/addMessage'; import { FaPaperPlane } from 'react-icons/fa'; -const PropertyContactForm = ({ property }) => { +const SubmitMessageButton = () => { + const status = useFormStatus(); return ( -
-

Contact Property Manager

-
-
- + + ); +}; + +const PropertyContactForm = ({ property }) => { + const { data: session } = useSession(); + + const [state, formAction] = useFormState(addMessage, {}); + + useEffect(() => { + if (state.error) toast.error(state.error); + if (state.submitted) toast.success('Message sent successfully'); + }, [state]); + + if (state.submitted) { + return ( +

+ Your message has been sent successfully +

+ ); + } + + return ( + session && ( +
+

Contact Property Manager

+ -
-
- -
-
- - -
-
- - -
-
- -
- -
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+ +
+ ) ); }; export default PropertyContactForm; diff --git a/models/Message.js b/models/Message.js new file mode 100644 index 0000000..2445e35 --- /dev/null +++ b/models/Message.js @@ -0,0 +1,46 @@ +import { Schema, model, models } from 'mongoose'; + +const MessageSchema = new Schema( + { + sender: { + type: Schema.Types.ObjectId, + ref: 'User', + required: true, + }, + recipient: { + type: Schema.Types.ObjectId, + ref: 'User', + required: true, + }, + property: { + type: Schema.Types.ObjectId, + ref: 'Property', + required: true, + }, + name: { + type: String, + required: [true, 'Name is required'], + }, + email: { + type: String, + required: [true, 'Email is required'], + }, + phone: { + type: String, + }, + body: { + type: String, + }, + read: { + type: Boolean, + default: false, + }, + }, + { + timestamps: true, + } +); + +const Message = models.Message || model('Message', MessageSchema); + +export default Message;