import { useEffect, useMemo, useRef, useState } from "react" import { motion } from "framer-motion" import { useAdminAddPublishableKeySalesChannelsBatch, useAdminRemovePublishableKeySalesChannelsBatch, useAdminPublishableApiKeySalesChannels, useAdminSalesChannels, } from "medusa-react" import { useTranslation } from "react-i18next" import Button from "../../../components/fundamentals/button" import SideModal from "../../../components/molecules/modal/side-modal" import CrossIcon from "../../../components/fundamentals/icons/cross-icon" import useNotification from "../../../hooks/use-notification" import InputField from "../../../components/molecules/input" import SearchIcon from "../../../components/fundamentals/icons/search-icon" import SalesChannelTable from "../tables/sales-channels-table" import UTurnIcon from "../../../components/fundamentals/icons/u-turn-icon" const LIMIT = 12 /* ****************************************** */ /* *************** ADD SCREEN *************** */ /* ****************************************** */ function AddScreen(props: { keyId: string close: () => void goBack: () => void isVisible: boolean }) { const tableRef = useRef() const { t } = useTranslation() const [selectedSalesChannels, setSelectedChannels] = useState({}) const notification = useNotification() const [offset, setOffset] = useState(0) const [search, setSearch] = useState("") const { sales_channels: data = [], isLoading, count, } = useAdminSalesChannels( { q: search, limit: LIMIT, offset }, { keepPreviousData: true, enabled: !!props.keyId } ) const { mutateAsync: addSalesChannelsToKeyScope } = useAdminAddPublishableKeySalesChannelsBatch(props.keyId) useEffect(() => { if (!props.isVisible) { setOffset(0) setSearch("") setSelectedChannels({}) tableRef.current?.toggleAllRowsSelected(false) } }, [props.isVisible]) const onSave = (callback: () => void) => () => { addSalesChannelsToKeyScope({ sales_channel_ids: Object.keys(selectedSalesChannels).map((id) => ({ id, })), }) .then(() => { notification( t("modals-success", "Success"), t( "modals-sales-channels-added-to-the-scope", "Sales channels added to the scope" ), "success" ) }) .catch(() => { notification( t("modals-error", "Error"), t( "modals-error-occurred-while-adding-sales-channels-to-the-scope-of-the-key", "Error occurred while adding sales channels to the scope of the key" ), "success" ) }) .finally(callback) } return (
{/* === HEADER === */}

{t("modals-add-sales-channels", "Add sales channels")}

{/* === DIVIDER === */}
} onChange={(ev) => setSearch(ev.target.value)} />
{/* === DIVIDER === */}
{/* === FOOTER === */}
) } /* ******************************************* */ /* *************** EDIT SCREEN *************** */ /* ******************************************* */ /** * Edit exiting PK SC list */ function EditScreen(props: { keyId: string close: () => void goAdd: () => void isVisible: boolean }) { const { close } = props const tableRef = useRef() const [selectedChannels, setSelectedChannels] = useState({}) const selectedCount = Object.keys(selectedChannels).length const { t } = useTranslation() const notification = useNotification() const [offset, setOffset] = useState(0) const [search, setSearch] = useState("") const { sales_channels: data = [], isLoading } = useAdminPublishableApiKeySalesChannels( props.keyId, { q: search }, { keepPreviousData: true, enabled: !!props.keyId, } ) const { mutateAsync: removeSalesChannelsToKeyScope } = useAdminRemovePublishableKeySalesChannelsBatch(props.keyId) const onDeselect = () => { setSelectedChannels({}) tableRef.current?.toggleAllRowsSelected(false) } const onRemove = () => { removeSalesChannelsToKeyScope({ sales_channel_ids: Object.keys(selectedChannels).map((id) => ({ id, })), }) .then(() => { notification( t("modals-success", "Success"), t( "modals-sales-channels-removed-from-the-scope", "Sales channels removed from the scope" ), "success" ) setSelectedChannels({}) tableRef.current?.toggleAllRowsSelected(false) }) .catch(() => { notification( t("modals-error", "Error"), t( "modals-error-occurred-while-removing-sales-channels-from-the-scope-of-the-key", "Error occurred while removing sales channels from the scope of the key" ), "success" ) }) } useEffect(() => { if (!props.isVisible) { setOffset(0) setSearch("") setSelectedChannels({}) tableRef.current?.toggleAllRowsSelected(false) } }, [props.isVisible]) // virtual pagination const displayData = useMemo( () => data?.slice(offset, offset + LIMIT), [data, offset] ) return (
{/* === HEADER === */}

{t("modals-edit-sales-channels", "Edit sales channels")}

{/* === DIVIDER === */}
} onChange={(ev) => setSearch(ev.target.value)} /> {selectedCount ? (
{t( "publishable-api-keys-modals-manage-sales-channels-selected-with-counts", "{{count}}", { count: selectedCount } )}
) : ( )}
{/* === DIVIDER === */}
{/* === FOOTER === */}
) } /* ***************************************************** */ /* *************** MANAGE CHANNELS MODAL *************** */ /* ***************************************************** */ type ManageSalesChannelsSideModalProps = { keyId?: string close: () => void } /** * Modal for adding/removing existing PKs channels. */ function ManageSalesChannelsSideModal( props: ManageSalesChannelsSideModalProps ) { const { keyId, close } = props const isVisible = !!keyId const [isAddNew, setIsAddNew] = useState(false) useEffect(() => { if (!isVisible) { setIsAddNew(false) } }, [isVisible]) return ( {/* EDIT PANEL */} setIsAddNew(true)} /> {/* ADD PANEL */} setIsAddNew(false)} /> ) } export default ManageSalesChannelsSideModal