diff --git a/src/configs/ManageProducts.config.js b/src/configs/ManageProducts.config.js index 013ec71a..38d81dc6 100644 --- a/src/configs/ManageProducts.config.js +++ b/src/configs/ManageProducts.config.js @@ -1,9 +1,10 @@ import { MultipleActionsCard } from "../components/MultipleActionsCard"; import { StandardCard } from "../components/StandardCard"; import { ReactComponent as AddProducts } from "../icons/add-products.svg"; -import { ReactComponent as GiftCard } from "../icons/gift.svg"; import { ReactComponent as Booking } from "../icons/booking.svg"; +import { ReactComponent as GiftCard } from "../icons/gift.svg"; import { ReactComponent as ImportProducts } from "../icons/import-products.svg"; +import { createProduct } from "../services"; import { wcBookings, wcGiftCardsSelector, @@ -95,8 +96,14 @@ const ManageProducts = (plugins) => [ isDisabled: () => plugins.status?.woocommerce !== "Active", }, actions: { - buttonClick: (state, setShowModal) => { - window.location.href = getUrl("post-new.php?post_type=gift_card"); + buttonClick: async (state, setShowModal) => { + let product = await createProduct({ + type: "gift-card", + status: "draft", + }); + window.location.href = product + ? `post.php?post=${product.id}&action=edit` + : "post-new.php?post_type=product"; }, }, dataDependencies: [ @@ -126,14 +133,21 @@ const ManageProducts = (plugins) => [ isDisabled: () => plugins.status?.woocommerce !== "Active", }, actions: { - onSelectAction: (state, action, opts) => { - let url = - action === "create_gift_card" - ? "post-new.php?post_type=gift_card" - : action === "view_gift_card" - ? "admin.php?page=yith_woocommerce_gift_cards_panel" - : "admin.php?page=yith_woocommerce_gift_cards_panel&tab=general"; - window.location.href = url; + onSelectAction: async (state, action, opts) => { + if (action === "create_gift_card") { + let product = await createProduct({ + type: "gift-card", + status: "draft", + }); + window.location.href = product + ? `post.php?post=${product.id}&action=edit` + : "post-new.php?post_type=product"; + } else if (action === "view_gift_card") { + window.location.href = "edit.php?post_type=product"; + } else { + window.location.href = + "admin.php?page=yith_woocommerce_gift_cards_panel"; + } }, }, dataDependencies: [ @@ -146,30 +160,38 @@ const ManageProducts = (plugins) => [ }, { Card: StandardCard, - shouldRender: () => - plugins?.status.nfd_slug_yith_woocommerce_booking === "Active", - title: "nfd_slug_yith_woocommerce_booking", + shouldRender: () => plugins?.status.yith_wcbk_panel === "Active", + title: "booking", assets: (state) => ({ Icon: BookingIcon }), text: (state) => ({ title: state.hasUsedPlugin ? "Manage Bookings" : "Setup Bookings", actionName: state.hasUsedPlugin ? "Manage" : "Create now", }), state: { - hasUsedPlugin: (data) => - data.nfd_slug_yith_woocommerce_booking.length > 0, + hasUsedPlugin: (data) => data.yith_wcbk_panel.length > 0, showCompletedIcon: (data) => false, isDisabled: () => plugins.status?.woocommerce !== "Active", }, actions: { - buttonClick: (state, setShowModal) => { - window.location.href = "admin.php?page=yith_wcbk_panel"; + buttonClick: async (state, setShowModal) => { + if (state.hasUsedPlugin) { + window.location.href = "admin.php?page=yith_wcbk_panel"; + } else { + let product = await createProduct({ + type: "booking", + status: "draft", + }); + window.location.href = product + ? `post.php?post=${product.id}&action=edit` + : "post-new.php?post_type=product"; + } }, }, dataDependencies: [ { endpoint: "/wc/v3/products", selector: wcBookings, - refresh: "nfd_slug_yith_woocommerce_booking", + refresh: "yith_wcbk_panel", }, ], }, diff --git a/src/services.js b/src/services.js index e7b8a514..a2e6ac01 100644 --- a/src/services.js +++ b/src/services.js @@ -6,6 +6,7 @@ export const Endpoints = { PAGE_STATUS: '/newfold-ecommerce/v1/user/page-status', PLUGIN_STATUS: '/newfold-ecommerce/v1/plugins/status', PLUGIN_INSTALL: '/newfold-onboarding/v1/plugins/install', + WC_PRODUCTS: '/wc/v3/products', }; export async function fetchWPSettings() { return apiFetch({ path: Endpoints.WP_SETTINGS }); @@ -42,3 +43,11 @@ export async function queuePluginInstall(plugin, token, priority = 10) { data: { plugin, activate: true, queue: true, priority }, }).catch((error) => 'failed'); } + +export async function createProduct(data) { + return apiFetch({ + path: Endpoints.WC_PRODUCTS, + method: 'POST', + data + }).catch((error) => null); +}