refinedev/refine

[FEAT] Make it EASY to code for medusa

Closed this issue · 0 comments

Is your feature request related to a problem? Please describe.

I AM ALWAYS FRUSTRATED WHEN:


import React, { useEffect, useState } from 'react';
import cn from "clsx";
import { Dropdown, DropdownMenuItem, DropdownTrigger, DropdownContent } from "/home/345893490/MYCUSTOMCODE/store/src/components/ui/Dropdown";
import s from "/home/283492408/MYCUSTOMCODE/store/src/components/ui/Dropdown/Dropdown.module.css";
import {
    useCart,
  } from "medusa-react"
const ENDPOINT =
  process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL || "https://SOMESITE.com"
import { useCartContext } from "@lib/context";

const Regions: React.FC = () => {

  const { cart } = useCartContext();

  const [regions, setRegions] = useState<any[]>([]);
  const [isLoading, setIsLoading] = useState<boolean>(true);
  const [selectedRegion, setSelectedRegion] = useState<string | null>(null);

  useEffect(() => {
    fetch(`${ENDPOINT}/store/regions`, {
      credentials: 'include',
    })
      .then((response) => response.json())
      .then(({ regions }) => {
        setRegions(regions);
        setIsLoading(false);
      })
      .catch((error) => {
        console.error('Error fetching regions:', error);
        setIsLoading(false);
      });
  }, []);


const { updateCart } = useCart() THIS LINE WILL GIVE AN ERROR BECAUSE useCart must be used within a CartProvider


  const handleRegionSelect = (regionId: string) => {
    setSelectedRegion(regionId);
    
    let cartid = cart?.id
    updateCart.mutate({
        regionId,
      })
    fetch(`${ENDPOINT}/store/carts/${cartid}`, {
        method: "POST",
        credentials: "include",
        body: JSON.stringify({
          region_id: regionId,
        }),
      })
      .then((response) => response.json())
      .then(({ cart }) => {
        let REGION_LS = "medusa_region"
        //@ts-ignore
        console.log(REGION_LS)
        let retrievedMem = JSON.parse(window.localStorage.getItem(REGION_LS))
        console.warn(retrievedMem)
        retrievedMem['regionId'] = regionId
        //@ts-ignore
        let setMem = window.localStorage.setItem(REGION_LS, JSON.stringify(retrievedMem))
        //window.localStorage.setItem(REGION_LS, regionId)
            console.log("CART UPDATED")
      })


THIS CODE IS MEANT TO UPDATE CART REGION I AM DOING IT 3 different ways IM GETTING ERRORS WHY CANT IT WORK??????????????????????????????????????????????????????????????????????????????????????????????
?
?
?
?
?
?
I HAVE ASKED??!!!!!!!!!!!!!!!!!!!!!!!!!!
WHY DOINT IT WORK??

Describe alternatives you've considered

No response

Additional context

No response

Describe the thing to improve

WHY DONT IT WORK WHY MUST IT BE INSIDE CART PROVIDER YOU MUST MAKE IT EASY TO WORK WITH

(this is a direct order.)