diff --git a/react/components/CheckoutCustomCep/index.tsx b/react/components/CheckoutCustomCep/index.tsx index c47afd8..43b5f40 100644 --- a/react/components/CheckoutCustomCep/index.tsx +++ b/react/components/CheckoutCustomCep/index.tsx @@ -1,8 +1,10 @@ import React, { FC, useEffect } from 'react' +import { ISellers } from '../../typings/sellers' const CheckoutCustomCep: FC = () => { const [postalCode, setPostalCode] = React.useState('') const [postalCodeFilled, setPostalCodeFilled] = React.useState(false) + const [errorModal, setErrorModal] = React.useState(false) const handleInputCep = (e: React.ChangeEvent) => { let value = e.target.value @@ -51,7 +53,19 @@ const CheckoutCustomCep: FC = () => { const updateShippingData = async (e: React.FormEvent) => { e.preventDefault() + const checkRegionServed = await fetch( + `/api/checkout/pub/regions?country=BRA&postalCode=${postalCode}` + ) + if (!checkRegionServed.ok) { + throw new Error(`HTTP error ${checkRegionServed.status}`) + } + const sellersByRegion: ISellers[] = await checkRegionServed.json() + const hasSellers = sellersByRegion[0].sellers.length > 0 + if (!hasSellers) { + setErrorModal(true) + return + } //@ts-ignore const orderFormId = window?.vtexjs?.checkout?.orderFormId @@ -89,8 +103,6 @@ const CheckoutCustomCep: FC = () => { ) .then(response => response.text()) .then(result => { - console.log(result) - //@ts-ignore vtexjs.checkout.getOrderForm().done(function(orderForm: any) {}) @@ -125,49 +137,78 @@ const CheckoutCustomCep: FC = () => { }, []) return ( -
- {postalCodeFilled ? ( -
-

- Digite o CEP para finalizar a compra* -

-
- {postalCode} - + <> +
+ {postalCodeFilled ? ( +
+

+ Digite o CEP para finalizar a compra* +

+
+ {postalCode} + +
-
- ) : ( -
- -
- - +
+

*Preenchimento obrigatório

+ + Não sei meu CEP + +
+ )} +
+ {errorModal && ( +
+
+ +

Desculpe

+

Ainda não atendemos a sua região

+
-

*Preenchimento obrigatório

- - Não sei meu CEP - - +
)} -
+ ) } diff --git a/react/typings/sellers.ts b/react/typings/sellers.ts new file mode 100644 index 0000000..31ba0b9 --- /dev/null +++ b/react/typings/sellers.ts @@ -0,0 +1,9 @@ +export interface ISellers { + id: string + sellers: ISeller[] +} +interface ISeller { + id: string + name: string + logo: string +}