@@ -13,20 +13,22 @@ import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";
1313import PropTypesSelect from "@/components/propertyTypes/propTypes" ;
1414import Footer from "@/components/footer/footer" ;
1515import AboutUs from "@/components/contents/Aboutus" ;
16- import NoPropertyAvailable from "@/components/property/noProps" ;
1716
1817export default async function Home ( {
19- searchParams = { } ,
18+ searchParams,
2019} : {
21- searchParams ?: { [ key : string ] : string | string [ ] | undefined } ;
20+ searchParams ?: Promise < { [ key : string ] : string | string [ ] | undefined } > ;
2221} ) {
23- // Handle undefined safely
22+ const resolvedParams = await searchParams ;
23+
24+ // Helper to extract query parameters
2425 const getParam = ( key : string ) : string | undefined => {
25- const value = searchParams ?. [ key ] ;
26+ const value = resolvedParams ?. [ key ] ;
2627 if ( Array . isArray ( value ) ) return value [ 0 ] ?. trim ( ) || undefined ;
2728 return value ?. trim ( ) || undefined ;
2829 } ;
2930
31+ // Format date to YYYY-MM-DD or return undefined
3032 const toDateStringOrUndefined = ( value ?: string ) : string | undefined => {
3133 if ( ! value ) return undefined ;
3234 const date = new Date ( value ) ;
@@ -39,84 +41,81 @@ export default async function Home({
3941 const typeParam = getParam ( "type" ) ;
4042 const type = typeParam && typeParam !== "Any" ? typeParam : undefined ;
4143
42- console . log ( "Parsed search params:" , {
43- destination,
44- checkIn,
45- checkOut,
46- type,
47- } ) ;
48-
44+ // Fetch authenticated user (optional)
4945 let kindeUser : TKindeUser | null = null ;
5046 try {
5147 const { getUser } = getKindeServerSession ( ) ;
5248 kindeUser = ( await getUser ( ) ) as TKindeUser ;
5349 console . log ( "User:" , kindeUser ) ;
5450 } catch ( error ) {
55- console . log ( "Finding User Error ", error ) ;
51+ console . warn ( "Failed to fetch user: ", error ) ;
5652 }
5753
58- // Fetch listings
59- const properties =
60- destination || checkIn || checkOut || type
61- ? await getFilteredListings ( destination , checkIn , checkOut , type )
62- : await getAllListedProperties ( ) ;
63-
64- console . log ( "Fetched properties:" , properties ) ;
65-
66- if ( ! properties || properties . length === 0 ) {
67- return < NoPropertyAvailable /> ;
68- }
54+ // Get property listings based on filters (if any)
55+ const properties = destination || checkIn || checkOut || type
56+ ? await getFilteredListings ( destination , checkIn , checkOut , type )
57+ : await getAllListedProperties ( ) ;
6958
59+ // Prepare property cards
7060 const propertyCards = await Promise . all (
7161 properties . map ( async ( property ) => {
7262 if ( ! property ?. id || ! property ?. locationId ) {
73- console . warn ( "Skipping invalid property:" , property ) ;
63+ console . warn ( "Skipping property with missing ID or location :" , property ) ;
7464 return null ;
7565 }
7666
77- const [ reviews , amenities , images , location , user ] = await Promise . all ( [
78- getAllReviewsById ( property . id ) ,
79- getAllAmenitiesForProperty ( property . id ) ,
80- getAllImagesbyId ( property . id ) ,
81- getLocationById ( property . locationId ) ,
82- getUserById ( property . userId ) ,
83- ] ) ;
67+ try {
68+ const [ reviews , amenities , images , location , user ] = await Promise . all ( [
69+ getAllReviewsById ( property . id ) ,
70+ getAllAmenitiesForProperty ( property . id ) ,
71+ getAllImagesbyId ( property . id ) ,
72+ getLocationById ( property . locationId ) ,
73+ getUserById ( property . userId ) ,
74+ ] ) ;
8475
85- if ( ! amenities || ! location || ! user ) {
86- console . error ( "Couldn't fetch all details for property:" , property ) ;
76+ if ( ! location || ! amenities || ! user ) {
77+ console . error ( "Incomplete data for property:" , property . id ) ;
78+ return null ;
79+ }
80+
81+ return (
82+ < PropertyCard
83+ key = { property . id }
84+ property = { property }
85+ location = { location }
86+ reviews = { reviews }
87+ amenities = { amenities }
88+ images = { images }
89+ type = "book"
90+ hostName = { user . name }
91+ hostKindeId = { property . userId }
92+ favorites = ""
93+ status = { null }
94+ />
95+ ) ;
96+ } catch ( error ) {
97+ console . error ( "Failed to fetch property details:" , property . id , error ) ;
8798 return null ;
8899 }
89-
90- return (
91- < PropertyCard
92- key = { property . id }
93- property = { property }
94- location = { location }
95- reviews = { reviews }
96- amenities = { amenities }
97- images = { images }
98- type = "book"
99- hostName = { user . name }
100- hostKindeId = { property . userId }
101- favorites = ""
102- status = { null }
103- />
104- ) ;
105100 } )
106101 ) ;
107102
103+ const validCards = propertyCards . filter ( Boolean ) ;
104+
108105 return (
109106 < >
110107 < PropTypesSelect />
111- < div className = "grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6 p-6" >
112- { propertyCards . filter ( Boolean ) . length > 0 ? (
113- propertyCards . filter ( Boolean )
108+
109+ < div className = "grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6 p-6 min-h-[50vh]" >
110+ { validCards . length > 0 ? (
111+ validCards
114112 ) : (
115- < div className = "text-red-600 text-center font-semibold text-lg w-full mt-8" >
113+ < div className = "text-red-600 text-center font-semibold text-lg w-full mt-8 col-span-full " >
116114 No property available
117115 </ div >
118116 ) }
119117 </ div >
118+
120119 < AboutUs />
121120 < Footer />
122121 </ >
0 commit comments