Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
1.2 kB
2
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;

    const results = data.acf.content
      ? data.acf.content.map(async (component) => {
          if (component.acf_fc_layout === "slider") {
            // const event = await getEventById("events", data.id);
            const events = component.events.map(async (event) => await getEventById("events", event.ID));
            return Promise.all(events);
          }
          return component;
        })
      : console.log("Component data not loading");
    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",
};