listing.js 1.8 KB
import { useRouter } from "next/router";
import { useEffect } from "react";
import { useDispatch } from "react-redux";
import Layout from "../components/layout/Layout";
import Listing from "../components/listing/Listing";
import { getActivitiesByFilters } from "../redux/actions/activityAction";
import { getAllCategories, getAllSubCategories, getSubCategoriesByCategoryId } from "../redux/actions/categoriesAction";
import { wrapper } from "../redux/store";

export default function ListingPage() {
  /** Client side rendering, traditional API call. */
  const router = useRouter();
  const dispatch = useDispatch();
  useEffect(() => {
    dispatch(getActivitiesByFilters({ currentPage: router.query.page }));
  }, [router]);

  return (
    <Layout>
      <Listing />
    </Layout>
  );
}

/** For server side rendering */
export const getServerSideProps = wrapper.getServerSideProps(store => async ({ req, query }) => {
  try {
    await store.dispatch(getActivitiesByFilters({ currentPage: query.page }));
    await store.dispatch(getAllCategories());
    await store.dispatch(getSubCategoriesByCategoryId());

    return {
      props: {}
      // Next.js will attempt to re-generate the page:
      // - Any requests to the page after the initial request and before 10 seconds are also cached and instantaneous.
      // - After the 10-second window, the next request will still show the cached (stale) page
      // - Next.js triggers a regeneration of the page in the background.
      // - Once the page generates successfully, Next.js will invalidate the cache and show the updated page. If the background regeneration fails, the old page would still be unaltered.
      // In seconds
      // revalidate: Number(process.env.NEXT_PUBLIC_ISR_REVALIDATE_AFTER)
    };
  } catch (error) {
    console.log("index.js", error);
  }
});