Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
1.2 kB
1
Indexable
Never
import Events from "@/components/content/Events";
import PropTypes from "prop-types";
import getEventById from "services/helpers/getEventById";
import wordpress from "..";

const getContentBySlug = async (contentType, slug) => {
  try {
    let { data } = await wordpress.get(`${contentType}`, {
      params: {
        slug: slug,
        _fields: `title,slug,acf,id,type`,
        per_page: 1,
      },
    });
    [data] = data;

    if (!data.acf.content) {
      console.error("Component data not loading");
      return;
    }

    const results = data.acf.content.map((component) => {
      if (component.acf_fc_layout === "slider") {
        // const event = await getEventById("events", data.id);
        const events = component.events.map((event) => getEventById("events", event.ID));
        return Promise.all("slider results", events);
      }
      return component;
    });

    console.log("Results", results);
    return data;
  } catch (err) {
    console.error(`Failed to get ${contentType}: `, err);
  }
};

export default getContentBySlug;

getContentBySlug.propTypes = {
  contentType: PropTypes.string,
  slug: PropTypes.string.isRequired,
};

getContentBySlug.defaultProps = {
  contentType: "pages",
  slug: "home",
};