@shopgate/engage
Version:
Shopgate's ENGAGE library.
50 lines (49 loc) • 1.97 kB
JavaScript
import React, { useContext, useCallback } from 'react';
import { RippleButton } from '@shopgate/engage/components';
import PropTypes from 'prop-types';
import { isProductAvailable } from "../../helpers";
import { StoreContext } from "./Store.context";
import { i18n, useWidgetSettings } from "../../../core";
import { FulfillmentContext } from "../../locations.context";
import { selectLocationButton, selectLocationButtonWrapper } from "./Store.style";
import connect from "./StoreListSearch.connector";
/**
* The StoreSelectLocationButton component.
* @returns {JSX.Element}
*/
import { jsx as _jsx } from "react/jsx-runtime";
const StoreSelectLocationButton = ({
setPostalCode
}) => {
const store = useContext(StoreContext);
const {
setUserSearchZipLocationFromSelection = true
} = useWidgetSettings('@shopgate/engage/locations') || {};
const {
selectLocation,
noInventory,
isLoading,
product
} = useContext(FulfillmentContext);
const isAvailable = isProductAvailable(store, store?.inventory);
const handleClick = useCallback(e => {
e.stopPropagation();
if (noInventory || isAvailable) {
if (setUserSearchZipLocationFromSelection) {
setPostalCode(store.address.postalCode, product?.id);
}
selectLocation(store);
}
}, [isAvailable, noInventory, product, selectLocation, setPostalCode, setUserSearchZipLocationFromSelection, store]);
return /*#__PURE__*/_jsx("div", {
className: selectLocationButtonWrapper,
children: /*#__PURE__*/_jsx(RippleButton, {
onClick: handleClick,
className: selectLocationButton.toString(),
disabled: isLoading || store?.isComingSoon || !noInventory && !isAvailable,
children: i18n.text(store?.isComingSoon ? 'location.comingSoon' : 'locations.select_location')
})
});
};
const connectedStoreSelectLocationButton = connect(StoreSelectLocationButton);
export { connectedStoreSelectLocationButton as StoreSelectLocationButton };