imageHandling.js 2.33 KB
export const cleanImage = originalImage => {
  let imageUrl = "/images/default.svg";

  if (originalImage) {
    /** When the AWS S3 plugin is activated, images are uploaded to S3 rather than local file system. */
    if (originalImage.url.startsWith("http")) {
      imageUrl = originalImage.url;
    } else {
      /** If now S3, then images are stored under the public/uploads directory of Strapi */
      imageUrl = `${process.env.NEXT_PUBLIC_BACKEND_API_URL}${originalImage.url}`;
    }
  }

  return imageUrl;
};

export const renderImage = imagePath => {
  let imageUrl = "/images/default.svg";

  if (imagePath) {
    /** When the AWS S3 plugin is activated, images are uploaded to S3 rather than local file system. */
    if (imagePath.startsWith("http") || imagePath.startsWith("https")) {
      imageUrl = imagePath;
    } else if (process.env.NEXT_PUBLIC_IMAGE_URL) {
      /** If now S3, then images are stored under the public/uploads directory of Strapi */
      imageUrl = `${process.env.NEXT_PUBLIC_IMAGE_URL}${imagePath}`;
    } else {
      imageUrl = `http://localhost:3015${imagePath}`;
    }
  }
  return imageUrl;
};

export const sanitizeTimeFormat = ({ data }) => {
  console.log("here sant 1");

  if (!data.fromHours || !data.fromMins || !data.toHours || !data.toMins) {
    console.log("here sant");
    return null;
  }
  const fromTime = `${data.fromHours}:${data.fromMins}:00`;
  const toTime = `${data.toHours}:${data.toMins}:00`;

  const day = data.day;
  return { fromTime, toTime, day };
  // const day
};

export const sanitizeTimeRange = ({ data }) => {
  // console.log("data >", data[0].$d);
  const date = new Date(data.$d);
  const day = date.getDate();
  const month = date.getMonth() + 1; // Month starts from 0, so add 1
  const year = date.getFullYear();

  const formattedDay = day < 10 ? "0" + day : day;
  const formattedMonth = month < 10 ? "0" + month : month;

  const formattedDate = `${year}-${formattedMonth}-${formattedDay}`;
  // console.log("date", formattedDate);
  return formattedDate;
  // const endDate = new Date(data[1].$d);
};

export const dateFormatFn = rawDate => {
  const date = new Date(rawDate);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0"); // Months are zero-indexed
  const day = String(date.getDate()).padStart(2, "0");
  return `${year}-${month}-${day}`;
};