Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
890 B
9
Indexable
Never
import PropTypes from "prop-types";
import wordpress from "../../lib/wordpress";
import getEventMediaById from "./getEventMediaById";

const getEventById = async (contentType, id) => {
  try {
    const { data } = await wordpress.get(`${contentType}/${id}`, {
      params: {
        _fields: `title,slug,acf,featured_media,status,excerpt`,
      },
    });
    const eventImage = await getEventMediaById("media", data.featured_media);
    const event = {
      tag: null,
      title: data.title.rendered,
      excerpt: data.excerpt.rendered,
      image: eventImage.media_details.sizes.full,
    };
    return event;
  } catch (err) {
    console.error(`Failed to get ${contentType}: `, err);
  }
};

export default getEventById;

getEventById.propTypes = {
  contentType: PropTypes.string,
  id: PropTypes.string.isRequired,
};

getEventById.defaultProps = {
  contentType: "pages",
};