| 1 |
import { useEffect, useState } from "@wordpress/element"; |
| 2 |
import { Toaster, toast } from "react-hot-toast"; |
| 3 |
import Header from "./template-parts/header"; |
| 4 |
import "./style.scss"; |
| 5 |
import useGetOptions from "./hooks/getOptions"; |
| 6 |
import BlockPage from "./pages/blocks"; |
| 7 |
import QuickStart from "./pages/quick-start"; |
| 8 |
import Modules from "./pages/modules"; |
| 9 |
import SavedTemplate from "./pages/savedTemplate"; |
| 10 |
import "./editor.scss"; |
| 11 |
import LightToPro from "./pages/liteToPro"; |
| 12 |
import AboutUs from "./pages/aboutUs"; |
| 13 |
import SetupWizard from "./setup-wizard"; |
| 14 |
import "./setup-wizard/setup-wizard.scss"; |
| 15 |
import Settings from "./pages/Settings/Settings"; |
| 16 |
import Integrations from "./pages/integrations"; |
| 17 |
|
| 18 |
const options = sp_pcp_block_settings.getOptions; |
| 19 |
const modules = sp_pcp_block_settings?.modulesOptions; |
| 20 |
const integrations = sp_pcp_block_settings?.integrationOptions; |
| 21 |
|
| 22 |
const Render = () => { |
| 23 |
const hashValue = window.location.hash.replace("#", "").split("=")[0]; |
| 24 |
|
| 25 |
const [blocksSettings, setBlocksSettings] = useState(options); |
| 26 |
const [modulesOptions, setModulesOptions] = useState(modules); |
| 27 |
const [integrationOptions, setIntegrationOptions] = useState(integrations); |
| 28 |
|
| 29 |
const [page, setPage] = useState(hashValue ? hashValue : "quick-start"); |
| 30 |
|
| 31 |
const postMenu = document.querySelector("#menu-posts-sp_post_carousel .wp-submenu"); |
| 32 |
const allItems = postMenu?.querySelectorAll("li"); |
| 33 |
|
| 34 |
useEffect(() => { |
| 35 |
const removeCurrentClass = () => { |
| 36 |
allItems?.forEach((element) => { |
| 37 |
element.classList.remove("current"); |
| 38 |
}); |
| 39 |
}; |
| 40 |
|
| 41 |
const postMenuAction = (e) => { |
| 42 |
const currentItem = e.target.closest("li"); |
| 43 |
removeCurrentClass(); |
| 44 |
currentItem?.classList.add("current"); |
| 45 |
|
| 46 |
setTimeout(() => { |
| 47 |
setPage(window.location.hash.replace("#", "")); |
| 48 |
}, 0); |
| 49 |
}; |
| 50 |
|
| 51 |
const pageLink = ["blocks", "savedTemplate", "settings", "lite-vs-pro"].includes(page) ? page : "pcp_help"; |
| 52 |
|
| 53 |
const blocksMenuLi = postMenu.querySelector(`a[href$="${pageLink}"]`)?.closest("li"); |
| 54 |
|
| 55 |
if(blocksMenuLi) { |
| 56 |
removeCurrentClass(); |
| 57 |
blocksMenuLi.classList.add("current"); |
| 58 |
} |
| 59 |
|
| 60 |
const recommendedPage = document.querySelector(".spspc-recommended-page"); |
| 61 |
|
| 62 |
if("about-us" === page && recommendedPage) { |
| 63 |
recommendedPage.style.display = "block"; |
| 64 |
} |
| 65 |
|
| 66 |
postMenu?.addEventListener("click", postMenuAction); |
| 67 |
|
| 68 |
return () => postMenu?.removeEventListener("click", postMenuAction); |
| 69 |
}, [page]); |
| 70 |
|
| 71 |
const successMes = useGetOptions(blocksSettings, modulesOptions, integrationOptions); |
| 72 |
|
| 73 |
const notify = (message, type = "success") => { |
| 74 |
if (type === "success") { |
| 75 |
toast.success(message, { style: { marginTop: "20px" } }); |
| 76 |
} else if (type === "error") { |
| 77 |
toast.error(message, { style: { marginTop: "20px" } }); |
| 78 |
} else { |
| 79 |
toast(message, { style: { marginTop: "20px" } }); |
| 80 |
} |
| 81 |
}; |
| 82 |
|
| 83 |
const blockShowHideHandler = (block_name, type = "block-option") => { |
| 84 |
const notifyChange = (name, currentValue, kind = "block") => { |
| 85 |
if (successMes) { |
| 86 |
notify(`The ${kind} has been ${currentValue ? "disabled" : "enabled"}`); |
| 87 |
} |
| 88 |
}; |
| 89 |
|
| 90 |
if (type === "block-option") { |
| 91 |
const newData = blocksSettings?.map((item) => |
| 92 |
item.block_name === block_name ? { ...item, show: !item.show } : item |
| 93 |
); |
| 94 |
const findCurrentBlock = blocksSettings.find((block) => block.block_name === block_name); |
| 95 |
notifyChange(block_name, findCurrentBlock?.show, "block"); |
| 96 |
setBlocksSettings(newData); |
| 97 |
} |
| 98 |
|
| 99 |
if (type === "modules-option") { |
| 100 |
const moduleData = modulesOptions?.map((item) => { |
| 101 |
return item.module_name === block_name ? { ...item, show: !item.show } : item; |
| 102 |
}); |
| 103 |
const findCurrentModule = modulesOptions.find((item) => item.module_name === block_name); |
| 104 |
notifyChange(block_name, findCurrentModule?.show, "module"); |
| 105 |
setModulesOptions(moduleData); |
| 106 |
} |
| 107 |
|
| 108 |
if (type === "integration-option") { |
| 109 |
const integrationData = integrationOptions?.map((item) => { |
| 110 |
return item.name === block_name ? { ...item, show: !item.show } : item; |
| 111 |
}); |
| 112 |
const findCurrentIntegration = integrationOptions.find((item) => item.name === block_name); |
| 113 |
notifyChange(block_name, findCurrentIntegration?.show, "integration"); |
| 114 |
setIntegrationOptions(integrationData); |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
const enabledSavedTemplate = modulesOptions?.some((item) => item.module_name === "saved-template" && item.show); |
| 119 |
|
| 120 |
if (page === "setupwizard") { |
| 121 |
return <SetupWizard |
| 122 |
blocksSettings={blocksSettings} |
| 123 |
modulesOptions={modulesOptions} |
| 124 |
blockShowHideHandler={blockShowHideHandler} |
| 125 |
/>; |
| 126 |
} |
| 127 |
|
| 128 |
return ( |
| 129 |
<> |
| 130 |
<Header setPage={setPage} page={page} modulesOptions={modulesOptions} /> |
| 131 |
|
| 132 |
<div className="sp-pcp-blocks-setting-wrapper"> |
| 133 |
{(page === "quick-start" || "" === page) && <QuickStart />} |
| 134 |
{page === "blocks" && ( |
| 135 |
<BlockPage blocksSettings={blocksSettings} blockShowHideHandler={blockShowHideHandler} /> |
| 136 |
)} |
| 137 |
{page === "modules" && ( |
| 138 |
<Modules modulesOptions={modulesOptions} blockShowHideHandler={blockShowHideHandler} /> |
| 139 |
)} |
| 140 |
{page === "lite-vs-pro" && <LightToPro />} |
| 141 |
{page === "about-us" && <AboutUs />} |
| 142 |
{( page === "savedTemplate" && enabledSavedTemplate ) && <SavedTemplate />} |
| 143 |
{page === "integrations" && <Integrations |
| 144 |
integrationOptions={integrationOptions} |
| 145 |
blockShowHideHandler={blockShowHideHandler} |
| 146 |
/>} |
| 147 |
{page === "settings" && <Settings />} |
| 148 |
</div> |
| 149 |
|
| 150 |
<Toaster position="top-right" reverseOrder={false} /> |
| 151 |
</> |
| 152 |
); |
| 153 |
}; |
| 154 |
|
| 155 |
export default Render; |
| 156 |
|