challenge-vtex-io-naian-felix/react/ProductContext.tsx

37 lines
1017 B
TypeScript

import React, { useEffect, useState } from "react";
import { useProduct } from "vtex.product-context";
const ProductContext = () => {
const productContext = useProduct();
const [simulation, setSimulation] = useState<any>();
useEffect(() => {
fetch("/api/checkout/pub/orderForms/simulation?sc=1", {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
paymentData: {
payments: [{ paymentSystem: "125", installments: 1 }],
},
items: [
{
id: productContext?.selectedItem?.itemId,
quantity: 1,
seller: "1",
},
],
country: "BRA",
}),
}).then(async (res) => {
setSimulation(await res.json());
});
}, [productContext?.selectedItem]);
console.log("productContext", productContext);
return <> {simulation?.paymentData?.payments[0]?.value}</>;
};
export default ProductContext;