From 340de94a5db005ac8260269e3de9460bfcbecf11 Mon Sep 17 00:00:00 2001 From: aryabenade Date: Fri, 24 Apr 2026 16:37:32 +0530 Subject: [PATCH] add blogs from backend in the homepage after city selected --- src/Redux/Store.tsx | 7 ++- src/Redux/services/blogs.service.ts | 28 +++++++++ src/components/MelbourneBlogs.tsx | 89 +++++++++++++++++++---------- 3 files changed, 93 insertions(+), 31 deletions(-) create mode 100644 src/Redux/services/blogs.service.ts diff --git a/src/Redux/Store.tsx b/src/Redux/Store.tsx index 363baba..2f46957 100644 --- a/src/Redux/Store.tsx +++ b/src/Redux/Store.tsx @@ -5,6 +5,7 @@ import { authApi } from "./services/auth.service"; import { profileApi } from "./services/profile.service"; import { cardsApi } from "./services/cards.service"; import { itineraryApi } from "./services/itinerary.service"; +import { blogsApi } from "./services/blogs.service"; export const store = configureStore({ reducer: { @@ -13,7 +14,8 @@ export const store = configureStore({ [authApi.reducerPath]: authApi.reducer, [profileApi.reducerPath]: profileApi.reducer, [cardsApi.reducerPath]:cardsApi.reducer, - [itineraryApi.reducerPath]:itineraryApi.reducer + [itineraryApi.reducerPath]:itineraryApi.reducer, + [blogsApi.reducerPath]:blogsApi.reducer }, @@ -24,7 +26,8 @@ export const store = configureStore({ authApi.middleware, profileApi.middleware, cardsApi.middleware, - itineraryApi.middleware + itineraryApi.middleware, + blogsApi.middleware ), }); export type RootState = ReturnType; diff --git a/src/Redux/services/blogs.service.ts b/src/Redux/services/blogs.service.ts new file mode 100644 index 0000000..bcc2a40 --- /dev/null +++ b/src/Redux/services/blogs.service.ts @@ -0,0 +1,28 @@ +import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'; +import { baseQuery } from "../baseQuery"; + +export const blogsApi = createApi({ + reducerPath: 'blogsApi', + baseQuery, + endpoints: (builder) => ({ + + getBlogsForCity: builder.query({ + // cityId is required, others optional + query: ({ cityId, categoryId }) => { + const params = new URLSearchParams(); + + // required + params.append('cityXid', cityId); + + // optional + if (categoryId) params.append('categoryXid', categoryId); + + return `/website/list/blogs?${params.toString()}`; + }, + }), + + + }), +}); + +export const { useGetBlogsForCityQuery } = blogsApi; \ No newline at end of file diff --git a/src/components/MelbourneBlogs.tsx b/src/components/MelbourneBlogs.tsx index 1be345a..4f6fff6 100644 --- a/src/components/MelbourneBlogs.tsx +++ b/src/components/MelbourneBlogs.tsx @@ -2,8 +2,9 @@ import { motion } from 'motion/react'; import { ImageWithFallback } from './figma/ImageWithFallback'; import { Calendar, Clock, User, ArrowRight, Coffee, Camera, MapPin, Star } from 'lucide-react'; import { Button } from './ui/button'; -import { useRef } from "react"; +import { useRef, useState } from "react"; import { useNavigate } from 'react-router-dom'; +import { useGetBlogsForCityQuery } from '../Redux/services/blogs.service'; const blogPosts = [ { @@ -93,10 +94,22 @@ export function MelbourneBlogs() { const sectionRef = useRef(null); const navigate = useNavigate(); + const cityId = localStorage.getItem('cityId'); + const [categoryId, setCategoryId] = useState(""); + const { data: blogsData, error, isLoading } = useGetBlogsForCityQuery({ cityId, categoryId }); const featuredPost = blogPosts.find(post => post.featured); const regularPosts = blogPosts.filter(post => !post.featured); const cityName = localStorage.getItem('cityName'); + const baseUrl = import.meta.env.VITE_BASE_URL; + + const blogss = blogsData?.blogs ?? []; + const categoriess = blogsData?.categories ?? [] + + const handleCategoryClick = (id: string) => { + // toggle logic: if already selected, reset to empty + setCategoryId(prev => (prev === id ? "" : id)); + }; return (
- {categories.map((category, index) => ( + setCategoryId("")} + className={`cursor-pointer px-6 py-3 rounded-full font-medium shadow-lg hover:shadow-xl transition-all duration-300 group + ${categoryId === "" ? "bg-gradient-to-r from-primary to-secondary text-white" : "bg-white text-gray-700"}`} + > + All + + + {categoriess.map((category: any, index) => ( handleCategoryClick(category.id)} + className={`cursor-pointer px-6 py-3 rounded-full font-medium shadow-lg hover:shadow-xl transition-all duration-300 group + ${categoryId === category.id ? "bg-gradient-to-r from-primary to-secondary text-white" : "bg-white text-gray-700"}`} > - {category.name} - - {category.count} - + {category.categoryName} ))} + {/* Featured Post */} - {featuredPost && ( + {/* {featuredPost && ( - )} + )} */} {/* Regular Blog Posts Grid */}
- {regularPosts.map((post, index) => ( + {blogss && blogss?.map((blog: any, index) => (
{/* Category Badge */}
- {post.category} + {blog?.category?.categoryName}
{/* Post Content */}
-
+ {/*
- {post.author} -
+ {blog?.author} +
*/}
- {post.date} + {blog?.createdAt && new Date(blog.createdAt).toLocaleDateString( + 'en-US', + { month: 'short', day: 'numeric', year: 'numeric' } + )}
- {post.readTime} + 5 min read

- {post.title} + {blog?.blogTitle}

-

- {post.excerpt} -

+

+ {/* Tags */} -

- {post.tags.slice(0, 2).map((tag, tagIndex) => ( + {/*
+ {blog?.tags?.slice(0, 2).map((tag, tagIndex) => ( ))} - {post.tags.length > 2 && ( + {blog?.tags?.length > 2 && ( - +{post.tags.length - 2} + +{blog?.tags?.length - 2} )} -
+
*/}