UNPKG

@finos/legend-application-marketplace

Version:
78 lines 5.63 kB
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; /** * Copyright (c) 2025-present, Goldman Sachs * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { clsx, PlusIcon, CheckIcon, CheckCircleIcon } from '@finos/legend-art'; import { RecommendationSource, } from '@finos/legend-server-marketplace'; import { Box, Button, CircularProgress, Tooltip, Typography, } from '@mui/material'; import { flowResult } from 'mobx'; import { observer } from 'mobx-react-lite'; import { useState } from 'react'; import { assertErrorThrown } from '@finos/legend-shared'; import { toastManager } from '../Toast/CartToast.js'; import { useLegendMarketplaceBaseStore } from '../../application/providers/LegendMarketplaceFrameworkProvider.js'; export const RecommendedItemsCard = observer((props) => { const { recommendedItem, onSelect, isSelecting, selectedItemId } = props; const legendMarketplaceBaseStore = useLegendMarketplaceBaseStore(); const [isAddingToCart, setIsAddingToCart] = useState(false); const inCart = legendMarketplaceBaseStore.cartStore.isItemInCart(recommendedItem.id); const isAssociationFlow = onSelect !== undefined; const isCurrentlySelecting = isAssociationFlow && Boolean(isSelecting) && selectedItemId === recommendedItem.id; const isMarketplaceItem = recommendedItem.source === RecommendationSource.MARKETPLACE; const handleAddAddonToCart = (addon) => { setIsAddingToCart(true); const cartItemRequest = legendMarketplaceBaseStore.cartStore.providerToCartRequest(addon); flowResult(legendMarketplaceBaseStore.cartStore.addToCartWithAPI(cartItemRequest)) .catch((error) => { assertErrorThrown(error); toastManager.error(`Failed to add ${addon.productName} to cart: ${error.message}`); }) .finally(() => { setIsAddingToCart(false); }); }; const renderAction = () => { if (isAssociationFlow) { if (recommendedItem.isOwned) { return (_jsxs(Box, { className: "recommended-addons-modal__owned-badge", children: [_jsx(CheckCircleIcon, {}), _jsx(Typography, { variant: "body2", children: "Owned" })] })); } if (isMarketplaceItem) { if (inCart) { return (_jsxs(Box, { className: "recommended-addons-modal__in-cart-badge", children: [_jsx(Typography, { variant: "body2", children: "In Cart" }), _jsx(CheckCircleIcon, {})] })); } return (_jsx(Button, { variant: "outlined", onClick: () => onSelect(recommendedItem), disabled: Boolean(isSelecting), size: "small", className: "recommended-addons-modal__add-btn", children: isCurrentlySelecting ? (_jsxs(_Fragment, { children: ["Adding... \u00A0", _jsx(CircularProgress, { size: 14 })] })) : (_jsxs(_Fragment, { children: ["Add to Cart \u00A0", _jsx(PlusIcon, {})] })) })); } return (_jsx(Button, { variant: "outlined", onClick: () => onSelect(recommendedItem), disabled: Boolean(isSelecting), size: "small", className: "recommended-addons-modal__select-btn", children: isCurrentlySelecting ? (_jsxs(_Fragment, { children: ["Selecting... \u00A0", _jsx(CircularProgress, { size: 14 })] })) : (_jsxs(_Fragment, { children: ["Select \u00A0", _jsx(CheckIcon, {})] })) })); } const button = (_jsx(Button, { variant: "outlined", onClick: () => handleAddAddonToCart(recommendedItem), disabled: inCart || isAddingToCart, size: "small", className: clsx('recommended-addons-modal__add-btn', { 'recommended-addons-modal__add-btn--added': inCart, }), children: isAddingToCart ? (_jsxs(_Fragment, { children: ["Adding... \u00A0", _jsx(CircularProgress, { size: 14 })] })) : inCart ? ('Added to Cart') : (_jsxs(_Fragment, { children: ["Add to Cart \u00A0", _jsx(PlusIcon, {})] })) })); if (inCart) { return (_jsx(Tooltip, { title: recommendedItem.isMandatory ? 'This is a mandatory item which needs to be associated with this order.' : 'This item is already in your cart.', arrow: true, placement: "top", children: _jsx("span", { children: button }) })); } return button; }; return (_jsxs(Box, { className: "recommended-addons-modal__list-item", children: [_jsx(Typography, { variant: "body1", className: "recommended-addons-modal__item-name", children: recommendedItem.productName }), _jsx(Typography, { variant: "body2", className: "recommended-addons-modal__item-provider", children: recommendedItem.providerName }), _jsx(Typography, { variant: "body2", className: "recommended-addons-modal__item-price", children: recommendedItem.price.toLocaleString('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 2, maximumFractionDigits: 2, }) }), _jsx(Box, { className: "recommended-addons-modal__item-action", children: renderAction() })] })); }); //# sourceMappingURL=RecommendedItemsCard.js.map