Guest User

Untitled

a guest
Jun 17th, 2024
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export const getGuestCart = () => {
  2.  
  3.     if (readCookie('cart_token')) {
  4.         const cart_token = readCookie('cart_token')
  5.         return fetch(`${baseUrl}/api/cart/user_cart/`, {
  6.             method: 'GET',
  7.             credentials: "include",
  8.             headers: {
  9.                 'Content-Type': 'application/json',
  10.                 'cart_token': `123`,
  11.                 'Cookie': `cart_token=${cart_token}`,
  12.             },
  13.         })
  14.         .then(res => res.json())
  15.         .catch(error => {
  16.             console.error('Error fetching guest cart:', error);
  17.             throw error;
  18.         });
  19.     } else {
  20.         return fetch(`${baseUrl}/api/cart/user_cart/`, {
  21.             method: 'GET',
  22.             headers: {
  23.                 'Content-Type': 'application/json',
  24.             },
  25.         })
  26.         .then(res => {
  27.             return res.json();
  28.         })
  29.         .then(data => {
  30.             if (data['token']) {
  31.                 document.cookie = `cart_token=${data['token']}; path=/`;
  32.             }
  33.         })
  34.         .catch(error => {
  35.             console.error('Error fetching guest cart:', error);
  36.             throw error;
  37.         });
  38.     }
  39.    
  40. };
Advertisement
Add Comment
Please, Sign In to add comment