Untitled

mail@pastecode.io avatar
unknown
plain_text
14 days ago
5.1 kB
1
Indexable
Never
import { PARAMS, api, setAuthHeader } from '@/utils/constants';
import { toBase64 } from './apiProducts';

export async function updateFeaturedProducts(data) {
  setAuthHeader();
  const productsData = { products: [] };
  productsData.products = data.map((item) => {
    return item.value;
  });
  try {
    await api.post(`${PARAMS.API_URL}/layouts/addsection1`, productsData);
  } catch (error) {
    throw new Error(error.response?.data.message || "couldn't update featured products at the moment");
  }
}
export async function updatePromotionProducts(data) {
  setAuthHeader();
  const productsData = { products: [] };
  productsData.products = data.map((item) => {
    return item.value;
  });

  try {
    await api.post(`${PARAMS.API_URL}/layouts/addsection2`, productsData);
  } catch (error) {
    throw new Error(error.response?.data.message || "couldn't update promotion products at the moment");
  }
}
export async function updateCategoriesSection(data) {
  setAuthHeader();
  const categoriesData = { categories: [] };
  categoriesData.categories = data.map((item) => {
    return item.value;
  });

  try {
    await api.post(`${PARAMS.API_URL}/layouts/addcategories`, categoriesData);
  } catch (error) {
    throw new Error(error.response?.data.message || "couldn't update categories section at the moment");
  }
}
type updateBannerProps = {
  imageFile: File;
  device: 'mobile' | 'desktop';
};
export async function updateBanner({ imageFile, device }: updateBannerProps) {
  setAuthHeader();

  const base64Image = await toBase64(imageFile);
  //@ts-ignore
  const images = [base64Image.split(',')[1]];

  if (device === 'mobile') {
    try {
      await api.post(`${PARAMS.API_URL}/layouts/addmobilebanner`, { images });
    } catch (error) {
      throw new Error(error.response?.data.message || "couldn't update mobile banner at the moment");
    }
  }
  if (device === 'desktop') {
    try {
      await api.post(`${PARAMS.API_URL}/layouts/adddesktopbanner`, { images });
    } catch (error) {
      throw new Error(error.response?.data.message || "couldn't update desktop banner at the moment");
    }
  }
}

export async function getFeaturedProducts() {
  setAuthHeader();
  try {
    const res = await api.get(`${PARAMS.API_URL}/layouts/products/section1`);

    return res.data.data.Section1;
  } catch (error) {
    throw new Error(error.response?.data.message || "couldn't get featured products at the moment");
  }
}
export async function getPromotionProducts() {
  setAuthHeader();

  try {
    const res = await api.get(`${PARAMS.API_URL}/layouts/products/section2`);

    return res.data.data.Section2;
  } catch (error) {
    throw new Error(error.response?.data.message || "couldn't get promotion products at the moment");
  }
}

export async function getCategoriesSection() {
  setAuthHeader();

  try {
    const res = await api.get(`${PARAMS.API_URL}/layouts/categories`);

    return res.data.data.categories;
  } catch (error) {
    throw new Error(error.response?.data.message || "couldn't get categories section at the moment");
  }
}
export async function getAllLayout() {
  setAuthHeader();

  try {
    const res = await api.get(`${PARAMS.API_URL}/layouts/getAllLayoutData`);
    return res.data.layout;
  } catch (error) {
    throw new Error(error.response?.data.message || "couldn't get layout data section at the moment");
  }
}

export async function updateAllLayoutData({
  mobileBanner = null,
  desktopBanner = null,
  newProducts,
  featuredProducts,
  promotionProducts,
  categories
}) {
  setAuthHeader();

  const featured = { products: [] };
  featured.products = featuredProducts.map((item) => {
    return item.value;
  });

  // Map the categories, productsSection1, and productsSection2 to their respective formats
  const formattedData = {
    categories: categories.map((item) => item.value),
    products_section1: featuredProducts.map((item) => item.value),
    products_section2: promotionProducts.map((item) => item.value),
    products_section3: newProducts.map((item) => item.value)
  };

  if (mobileBanner?.length) {
    const photoBase64 = await Promise.all(mobileBanner.map((image) => toBase64(image)));
    const imagesBase64Stripped = photoBase64.map((image) => image.slice(image.indexOf(',') + 1));
    //@ts-ignore
    formattedData.imagesmobile = [...imagesBase64Stripped];
  }
  if (desktopBanner?.length) {
    const photoBase64 = await Promise.all(desktopBanner.map((image) => toBase64(image)));
    const imagesBase64Stripped = photoBase64.map((image) => image.slice(image.indexOf(',') + 1));
    //@ts-ignore
    formattedData.imagesdesktop = [...imagesBase64Stripped];
  }

  try {
    const res = await api.post(`${PARAMS.API_URL}/layouts/addall`, formattedData);
    return res.data;
  } catch (error) {
    throw new Error(error.response?.data.message || "couldn't update layout data at the moment");
  }
}
Leave a Comment