Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { getQueryStringProps } from "qs-props";
- export async function getStaticPathsCategories(locales) {
- let paths = [];
- for (const locale of locales) {
- const res = await fetch(
- `${process.env.NEXT_PUBLIC_API_URL}categories/frontendfind?_locale=${locale}`
- );
- const data = await res.json();
- paths = [
- ...paths,
- ...data.map((category) => {
- return {
- params: {
- query: [
- category.slug.toString().replace(`-${locale.toLowerCase()}`, ""),
- "query.{}",
- ],
- },
- locale: locale,
- };
- }),
- ];
- }
- return {
- paths: paths,
- fallback: "blocking",
- };
- }
- // export const getQuery = (params) => {
- // const paths = params?.query;
- // if (Array.isArray(paths)) {
- // const q = paths.find((p) => p.startsWith("query."));
- // return !q ? q : JSON.parse(q.replace(/^query\./, ""));
- // }
- // return undefined;
- // };
- export async function getStaticPropsCategories(context) {
- // const queryParams = {};
- const queryParams = getQueryStringProps(context, "query");
- console.log(queryParams);
- const { query } = context.params;
- if (query.length === 0) {
- return {
- notFound: true,
- };
- }
- const slug = query[0];
- const { locale, defaultLocale } = context;
- const res = await fetch(
- `${process.env.NEXT_PUBLIC_API_URL}categories/frontendfind?slug_contains=${slug}&_locale=${locale}`
- );
- if (res.status === 404) {
- return {
- notFound: true,
- };
- }
- const data = await res.json();
- if (!data && data.length === 0) {
- return {
- notFound: true,
- };
- }
- if (data.length > 0) {
- let translation =
- data.length >= 1 &&
- data[0].localizations.find((l) => l.locale === locale);
- if (data[0].locale !== locale && translation) {
- const resCategory = await fetch(
- `${process.env.NEXT_PUBLIC_API_URL}categories/findbyid/${translation.id}`
- );
- const dataCategory = await resCategory.json();
- return {
- redirect: {
- destination: `/categories/${dataCategory.slug.replace(
- `-${locale.toLowerCase()}`,
- ""
- )}`,
- permanent: false,
- },
- };
- }
- }
- const resAttributes = await fetch(
- `${process.env.NEXT_PUBLIC_API_URL}attributes/store/find?_locale=${locale}`
- );
- const dataAttributes = await resAttributes.json();
- let searchQuery = ``;
- let orderQuery = `&_sort=order_value:DESC`;
- let pagination = `&_start=0&_limit=${process.env.NEXT_PUBLIC_PER_PAGE}`;
- let haveAttributesToSearch = false;
- if (queryParams) {
- if (Object.keys(queryParams).length !== 0) {
- for (const [key, value] of Object.entries(queryParams)) {
- if (key.includes("attr_")) {
- haveAttributesToSearch = true;
- }
- }
- if (haveAttributesToSearch) {
- searchQuery += `&sellable_items.stock_gte=1`;
- }
- for (const [key, value] of Object.entries(queryParams)) {
- if (key.includes("attr_")) {
- let attr_key = key.replace("attr_", "");
- if (value.length > 0) {
- value.forEach((v) => {
- searchQuery += `&sellable_items.search_string_contains=${attr_key}-${v}`;
- });
- }
- } else if (key === "order_by") {
- if (value.split(":")[0] === "price") {
- orderQuery = `&_sort=sellable_items.price:${value.split(":")[1]}`;
- } else if (value.split(":")[0] === "created_at") {
- orderQuery = `&_sort=created_at:${value.split(":")[1]}`;
- } else if (value.split(":")[0] === "our_choices") {
- orderQuery = `&_sort=order_value:DESC`;
- }
- } else if (key === "page") {
- pagination = `&_start=${
- (value - 1) * process.env.NEXT_PUBLIC_PER_PAGE
- }&_limit=${process.env.NEXT_PUBLIC_PER_PAGE}`;
- } else {
- searchQuery += `&${key}=${value}`;
- }
- }
- }
- }
- searchQuery += `${searchQuery}${orderQuery}`;
- const resProducts = await fetch(
- `${process.env.NEXT_PUBLIC_API_URL}products/store/find?categories_in=${data[0].id}&published_at_null=false${searchQuery}${pagination}&_locale=${locale}`
- );
- const dataProducts = await resProducts.json();
- return {
- props: {
- category: data[0],
- attributes: dataAttributes,
- products: dataProducts,
- },
- revalidate: 86400,
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement