import { useEffect, useState } from "react" import { PublishableApiKey, SalesChannel } from "@medusajs/medusa" import { useAdminAddPublishableKeySalesChannelsBatch, useAdminCreatePublishableApiKey, } from "medusa-react" import { useTranslation } from "react-i18next" import BackButton from "../../../components/atoms/back-button" import Fade from "../../../components/atoms/fade-wrapper" import Spacer from "../../../components/atoms/spacer" import Button from "../../../components/fundamentals/button" import ChannelsIcon from "../../../components/fundamentals/icons/channels-icon" import CrossIcon from "../../../components/fundamentals/icons/cross-icon" import InputField from "../../../components/molecules/input" import FocusModal from "../../../components/molecules/modal/focus-modal" import SalesChannelsSummary from "../../../components/molecules/sales-channels-summary" import BodyCard from "../../../components/organisms/body-card" import useNotification from "../../../hooks/use-notification" import useToggleState from "../../../hooks/use-toggle-state" import AddSalesChannelsSideModal from "../modals/add-sales-channels" import DetailsModal from "../modals/details" import ManageSalesChannelsSideModal from "../modals/manage-sales-channels" import PublishableApiKeysTable from "../tables/publishable-api-keys-table" type AddSalesChannelsSectionProps = { setSelectedChannels: (arg: any) => void selectedChannels: Record } /** * Container for adding sales channels to PK scope. */ function AddSalesChannelsSection(props: AddSalesChannelsSectionProps) { const { setSelectedChannels, selectedChannels } = props const { t } = useTranslation() const [isModalVisible, showModal, hideModal] = useToggleState(false) const hasSelectedChannels = !!Object.keys(selectedChannels).length return (
{t("pages-sales-channels", "Sales channels")}

{t( "pages-connect-as-many-sales-channels-to-your-api-key-as-you-need", "Connect as many sales channels to your API key as you need." )}

{!hasSelectedChannels && ( )}
{hasSelectedChannels && (
)}
) } type CreatePublishableKeyProps = { closeModal: () => void } /** * Focus modal container for creating Publishable Keys. */ function CreatePublishableKey(props: CreatePublishableKeyProps) { const { closeModal } = props const notification = useNotification() const { t } = useTranslation() const [name, setName] = useState("") const [keyId, setKeyId] = useState("") const [selectedChannels, setSelectedChannels] = useState({}) const { mutateAsync: createPublishableApiKey } = useAdminCreatePublishableApiKey() const { mutateAsync: addSalesChannelsToKeyScope } = useAdminAddPublishableKeySalesChannelsBatch(keyId) const onSubmit = async () => { try { const res = await createPublishableApiKey({ title: name }) setKeyId(res.publishable_api_key.id) notification( t("pages-success", "Success"), t("pages-created-a-new-api-key", "Created a new API key"), "success" ) } catch (e) { notification( t("pages-error", "Error"), t( "pages-failed-to-create-a-new-api-key", "Failed to create a new API key" ), "error" ) } } useEffect(() => { if (keyId) { addSalesChannelsToKeyScope({ sales_channel_ids: Object.keys(selectedChannels).map((id) => ({ id })), }) .then(() => { notification( t("pages-success", "Success"), t( "pages-sales-channels-added-to-the-scope", "Sales channels added to the scope" ), "success" ) }) .catch(() => { notification( t("pages-error", "Error"), t( "pages-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(closeModal) } }, [keyId, selectedChannels]) return (

{t("pages-create-api-key", "Create API Key")}

General Information

{t( "pages-create-and-manage-api-keys-right-now-this-is-only-related-to-sales-channels", "Create and manage API keys. Right now this is only related to sales channels." )}

setName(ev.target.value)} />
) } /** * Index page container for the "Publishable API keys" page */ function Index() { const { t } = useTranslation() const [selectedKey, setSelectedKey] = useState() const [editKey, setEditKey] = useState() const [isCreateModalVisible, openCreateModal, closeCreateModal] = useToggleState(false) const actions = [ { label: t("pages-create-api-key-label", "Create API key"), onClick: openCreateModal, }, ] const _openChannelsModal = (pubKey: PublishableApiKey) => { setEditKey(pubKey) } const _closeChannelsModal = () => { setEditKey(null) } return (
setSelectedKey(undefined)} />
) } export default Index