@shopgate/engage
Version:
Shopgate's ENGAGE library.
67 lines (66 loc) • 2.37 kB
JavaScript
import React, { Fragment, useCallback, useState, useMemo } from 'react';
import PropTypes from 'prop-types';
import { FulfillmentSheet } from "../FulfillmentSheet";
import { STAGE_SELECT_STORE, MULTI_LINE_RESERVE } from "../../constants";
import GlobalLocationSwitcherDefault from "./GlobalLocationSwitcherDefault";
import GlobalLocationSwitcherBar from "./GlobalLocationSwitcherBar";
import connect from "./GlobalLocationSwitcher.connector";
/**
* @returns {JSX}
*/
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
const GlobalLocationSwitcher = ({
isLocationBasedShopping,
preferredLocation,
renderBar,
standalone,
selectGlobalLocation,
editable
}) => {
const [sheetOpen, setSheetOpen] = useState(false);
const changeLocationHandler = useCallback(() => {
setSheetOpen(true);
}, []);
const closeSheetHandler = useCallback(location => {
setSheetOpen(false);
if (location && location.code !== preferredLocation?.code) {
// Only dispatch selectGlobalLocation when location really changed, since this action
// might clear product data from the resultsByHash product storage.
selectGlobalLocation(location);
}
}, [preferredLocation, selectGlobalLocation]);
const locationName = useMemo(() => preferredLocation?.name || null, [preferredLocation]);
if (!isLocationBasedShopping || !preferredLocation) {
return null;
}
return /*#__PURE__*/_jsxs(_Fragment, {
children: [renderBar ? /*#__PURE__*/_jsx(GlobalLocationSwitcherBar, {
locationName: locationName,
handleChange: changeLocationHandler,
standalone: standalone
}) : /*#__PURE__*/_jsx(GlobalLocationSwitcherDefault, {
locationName: locationName,
handleChange: changeLocationHandler,
standalone: standalone,
editable: editable
}), sheetOpen && /*#__PURE__*/_jsx(FulfillmentSheet, {
stage: STAGE_SELECT_STORE,
fulfillmentPath: MULTI_LINE_RESERVE,
allowClose: true,
onClose: closeSheetHandler,
isInitialized: true,
noProduct: true,
noInventory: true,
changeOnly: true,
open: true
})]
});
};
GlobalLocationSwitcher.defaultProps = {
isLocationBasedShopping: false,
preferredLocation: null,
renderBar: false,
editable: true,
standalone: false
};
export default connect(GlobalLocationSwitcher);