index.js 2.04 KB
import InnerDetails from "@/components/Collection/InnerDetails";
import Breadcrumb from "@/components/Common/Breadcrumb";
import HeadTitle from "@/components/Common/HeadTitle";
import { getCollectionPageData } from "@/services/collectionApi";
import { getCollectionCategoryBySlug } from "@/services/collectionCategoryApi";
import { getCollectionSubCategoryData } from "@/services/collectionSubCategoryApi";
import { getRedisClient } from "@/redis-client";

export default function CategoryPage({ categoryData, categoriesSub,collectionDataa }) {
 
  if (!categoryData) {
    return <h1>Category not found</h1>;
  }

  const breadcrumbData = [
    // { href: "/", label: "Home" },
    { href: "/", label: "Collections" },
    {
      href: `/collections/${categoryData.slug}`,
      label: categoryData.title,
    },
  ];

  // const headTitleData = {
  //   title: categoryData.title,
  //   descrition1: "",
  //   descrition2: "",
  // };

  return (
    <>
      <Breadcrumb breadcrumbData={breadcrumbData} />
      <HeadTitle categoryData={categoryData} />

      {categoriesSub.length > 0 && (
        <InnerDetails subCategories={categoriesSub} />
      )}
    </>
  );
}

export async function getServerSideProps({ params }) {
  const { category } = params;
  const redis = getRedisClient();
  const REDIS_KEY = `${process.env.REDIS_KEY_PREFIX}_category_${category}`;
  const REDIS_EXPIRE = parseInt(process.env.REDIS_KEY_EXPIRE) || 86400;

  try {
    const cachedData = await redis.get(REDIS_KEY);
    if (cachedData) {
      console.log("redis data fetched");
      return { props: JSON.parse(cachedData) };
    }

    const categoryData = await getCollectionCategoryBySlug(category);
    const categoriesSub = await getCollectionSubCategoryData(category);
    const result = { categoryData, categoriesSub };
    await redis.set(REDIS_KEY, JSON.stringify(result), "EX", REDIS_EXPIRE);
    console.log("normal data fetched");
    return { props: result };
  } catch (error) {
    return {
      props: {
        categoryData: null,
        categoriesSub: [],
      },
    };
  }
}