diff --git a/backend/lib/controllers/user-listings/controller.ts b/backend/lib/controllers/user-listings/controller.ts index 094690d..98e010d 100644 --- a/backend/lib/controllers/user-listings/controller.ts +++ b/backend/lib/controllers/user-listings/controller.ts @@ -32,8 +32,9 @@ router.post("/create", async (req, res) => { }); router.put("/:userListingId", async (req, res) => { + const updatedUserListingId = req.params.userListingId; const updatedUserListing = req.body; - const userListing = await UserListingService().updateUserListing(updatedUserListing); + const userListing = await UserListingService().updateUserListing(updatedUserListingId, updatedUserListing); if (userListing) { res.status(200).json(userListing); } else { diff --git a/backend/lib/services/UserListing/interface.ts b/backend/lib/services/UserListing/interface.ts index e7ed595..4879a84 100644 --- a/backend/lib/services/UserListing/interface.ts +++ b/backend/lib/services/UserListing/interface.ts @@ -12,6 +12,6 @@ export interface IUserListingService { getUserListingById(userListingId: string): Promise getAllUserListings(): Promise createUserListing(newUserListing: Omit): Promise - updateUserListing(updatedUserListing: UserListing): Promise + updateUserListing(updatedUserListingId: string, updatedUserListing: Omit): Promise deleteUserListing(userListingId: string): Promise } diff --git a/backend/lib/services/UserListing/service.ts b/backend/lib/services/UserListing/service.ts index d6de1cd..e5fe823 100644 --- a/backend/lib/services/UserListing/service.ts +++ b/backend/lib/services/UserListing/service.ts @@ -17,7 +17,6 @@ export const UserListingService = (): IUserListingService => ({ }, }); if (!userListing) return null; - return { ...userListing, User: { @@ -26,6 +25,7 @@ export const UserListingService = (): IUserListingService => ({ }, }; }, + getAllUserListings: async () => { const userListings = await prisma.userListing.findMany({ include: { @@ -44,6 +44,7 @@ export const UserListingService = (): IUserListingService => ({ }, })); }, + createUserListing: async (newUserListing) => { if (newUserListing.website) { if (!newUserListing.website.startsWith("https://")) { @@ -55,17 +56,17 @@ export const UserListingService = (): IUserListingService => ({ }); return userListing; }, - updateUserListing: async (updatedUserListing) => { - const { id, ...updateData } = updatedUserListing; + updateUserListing: async (updatedUserListingId, updatedUserListing) => { const userListing = await prisma.userListing.update({ where: { - id, + id: updatedUserListingId, }, - data: updateData, + data: updatedUserListing, }); return userListing; }, + deleteUserListing: async (userListingId) => { const userListing = await prisma.userListing.delete({ where: { diff --git a/frontend/src/components/compound/Banner/ProfileBanner.tsx b/frontend/src/components/compound/Banner/ProfileBanner.tsx index cd02e33..f1dce87 100644 --- a/frontend/src/components/compound/Banner/ProfileBanner.tsx +++ b/frontend/src/components/compound/Banner/ProfileBanner.tsx @@ -1,19 +1,33 @@ import React, { useState } from "react"; import UserListingModal from "../Modal/UserListingModal"; // Adjust the import path +import { UserListingDisplayData } from "../../../lib/services/User-Listing/types"; +import DeleteListingModal from "../Modal/DeleteListingModal"; interface ProfileBannerProps { - onListingAdded: () => void; + handleListingsChanged: () => void; + userListings: Array; } -const ProfileBanner = ({ onListingAdded }: ProfileBannerProps) => { - const [isModalOpen, setIsModalOpen] = useState(false); // State to manage modal visibility +const ProfileBanner = ({ handleListingsChanged, userListings }: ProfileBannerProps) => { + const [isAddEditModalOpen, setAddEditModalOpen] = useState(false); + const [isDeleteModalOpen, setDeleteModalOpen] = useState(false); - const openModal = () => { - setIsModalOpen(true); // Function to open the modal + const listingExists: boolean = userListings.length > 0; + + const openAddEditModal = () => { + setAddEditModalOpen(true); // Function to open the modal + }; + + const closeAddEditModal = () => { + setAddEditModalOpen(false); // Function to close the modal + }; + + const handleDeleteClick = () => { + setDeleteModalOpen(true); }; - const closeModal = () => { - setIsModalOpen(false); // Function to close the modal + const closeDeleteModal = () => { + setDeleteModalOpen(false); }; return ( @@ -24,16 +38,37 @@ const ProfileBanner = ({ onListingAdded }: ProfileBannerProps) => {
Create a profile to be discovered by communities and organizers
- - {isModalOpen && ( + +
+ + + {listingExists && ( + + )} +
+ {isAddEditModalOpen && ( + )} + {isDeleteModalOpen && ( + )} diff --git a/frontend/src/components/compound/Modal/DeleteListingModal.tsx b/frontend/src/components/compound/Modal/DeleteListingModal.tsx index e7f1eca..2c9d9de 100644 --- a/frontend/src/components/compound/Modal/DeleteListingModal.tsx +++ b/frontend/src/components/compound/Modal/DeleteListingModal.tsx @@ -21,11 +21,11 @@ const DeleteListingModal: React.FC = ({ const userListingService = UserListingService(); const spaceListingService = SpaceListingService(); - const handleSubmit = () => { + const handleSubmit = async () => { if (listingCategory === "SpaceListing") { - spaceListingService.delete(listingId); + await spaceListingService.delete(listingId); } else if (listingCategory === "UserListing") { - userListingService.delete(listingId); + await userListingService.delete(listingId); } onSubmitSuccess(); onClose(); diff --git a/frontend/src/components/compound/Modal/UserListingModal.tsx b/frontend/src/components/compound/Modal/UserListingModal.tsx index 8eab5bc..4dbb315 100644 --- a/frontend/src/components/compound/Modal/UserListingModal.tsx +++ b/frontend/src/components/compound/Modal/UserListingModal.tsx @@ -8,13 +8,17 @@ import { UserService } from "../../../lib/services/Users/service"; interface UserListingModalProps { onClose: () => void; onSubmitSuccess: () => void; + listingExists: boolean; } const UserListingModal: React.FC = ({ onClose, onSubmitSuccess, + listingExists, }) => { + const userService = UserService(); + const [editListingId, setEditListingId] = useState(null); const [formData, setFormData] = useState({ user_id: "", leaselength: "", @@ -26,18 +30,34 @@ const UserListingModal: React.FC = ({ email: "", }); - // const userService = useUserService() - const userService = UserService(); useEffect(() => { - const fetchUser = async () => { + const fetchUserAndListing = async () => { const userResponse = await userService.getCurrentUser(); const user = userResponse.data; if (!user) return; - setFormData((prevData) => ({ ...prevData, user_id: user.id || "" })); + + const userListingService = UserListingService(); + const listingsResponse = await userListingService.getAll(); + const editListingId = listingsResponse.data.find(listing => listing.user_id === user.id); + if (editListingId && listingExists) { + setFormData({ + user_id: editListingId.user_id, + leaselength: editListingId.leaselength, + moveInTime: editListingId.moveInTime, + housematesCount: editListingId.housematesCount, + description: editListingId.description, + website: editListingId.website || "", + phone: editListingId.phone || "", + email: editListingId.email || "", + }); + setEditListingId(editListingId.id); + } else { + setFormData(prevData => ({ ...prevData, user_id: user.id || "" })); + } console.log(user, "here"); }; - fetchUser(); + fetchUserAndListing(); }, []); const handleChange = ( @@ -51,8 +71,14 @@ const UserListingModal: React.FC = ({ e.preventDefault(); const userListingService = UserListingService(); try { - await userListingService.create(formData); - console.log("Submitted:", { formData }); + if (editListingId && listingExists) { + await userListingService.update(editListingId, formData); + console.log("Updated:", { formData }); + } + else { + await userListingService.create(formData); + console.log("Submitted:", { formData }); + } onSubmitSuccess(); onClose(); } catch (error) { diff --git a/frontend/src/components/pages/PeopleListingSection.tsx b/frontend/src/components/pages/PeopleListingSection.tsx index 4538420..5f3db8f 100644 --- a/frontend/src/components/pages/PeopleListingSection.tsx +++ b/frontend/src/components/pages/PeopleListingSection.tsx @@ -4,11 +4,12 @@ import { UserListingDisplayData } from "../../lib/services/User-Listing/types"; import UserListingService from "../../lib/services/User-Listing/service"; import { LeaseLength, RoommateCount, MovingTimeline } from "./types"; import ProfileBanner from "../compound/Banner/ProfileBanner"; +import { User } from "../../lib/services/Users/types"; +import { UserService } from "../../lib/services/Users/service"; export default function PeopleListingSection() { - const [userlistings, setuserListings] = useState< - Array - >([]); + const [userlistings, setuserListings] = useState>([]); + const currentDate = new Date(); console.log("people section"); @@ -26,9 +27,11 @@ export default function PeopleListingSection() { fetchListings(); }, [fetchListings]); - const handleListingAdded = () => { + const handleListingsChanged = useCallback(() => { + console.log("listings bananaed"); fetchListings(); - }; + }, [fetchListings]); + const default_values: [string, string, string] = [ "Any lease", @@ -114,7 +117,7 @@ export default function PeopleListingSection() { }; return ( <> - +
*/} @@ -177,8 +180,8 @@ export default function PeopleListingSection() { a.createdAt > b.createdAt ? -1 : b.createdAt > a.createdAt - ? 1 - : 0 + ? 1 + : 0 ) .filter( (f) =>