PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 4.0.3
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v4.0.3
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 2.3.5 2.3.6 2.4.0 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.15 2.4.16 2.4.17 2.4.18 2.4.19 2.4.2 2.4.20 2.4.21 All 88 releases
post-carousel / src / admin / Render.js

Render.js in Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News 4.0.3, at src/admin/Render.js

187 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { useEffect, useState } from "@wordpress/element";
2 import { Toaster, toast } from "react-hot-toast";
3 import Header from "./components/layout/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/saved-template";
10 import "./editor.scss";
11 import LightToPro from "./pages/lite-to-pro";
12 import AboutUs from "./pages/about-us";
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 // Inject Crisp chat script
19 const initCrispChat = () => {
20 if (window.$crisp) {
21 return;
22 }
23 window.$crisp = [];
24 window.CRISP_WEBSITE_ID = "3770a55e-947f-424d-87b8-d93a86f2cd7b";
25 (function () {
26 const d = document;
27 const s = d.createElement("script");
28 s.src = "https://client.crisp.chat/l.js";
29 s.async = 1;
30 d.getElementsByTagName("head")[0].appendChild(s);
31 })();
32 };
33
34 const options = sp_pcp_block_settings.getOptions;
35 const modules = sp_pcp_block_settings?.modulesOptions;
36 const integrations = sp_pcp_block_settings?.integrationOptions;
37
38 const Render = () => {
39 const hashValue = window.location.hash.replace("#", "").split("=")[0];
40 const hashTabValue = window.location.hash.replace("#", "").split("=")[1];
41
42 const [blocksSettings, setBlocksSettings] = useState(options);
43 const [modulesOptions, setModulesOptions] = useState(modules);
44 const [integrationOptions, setIntegrationOptions] = useState(integrations);
45
46 const [page, setPage] = useState(hashValue ? hashValue : "quick-start");
47
48 const postMenu = document.querySelector("#menu-posts-sp_post_carousel .wp-submenu");
49 const allItems = postMenu?.querySelectorAll("li");
50
51 useEffect(() => {
52 const removeCurrentClass = () => {
53 allItems?.forEach((element) => {
54 element.classList.remove("current");
55 });
56 };
57
58 const postMenuAction = (e) => {
59 const currentItem = e.target.closest("li");
60 removeCurrentClass();
61 currentItem?.classList.add("current");
62
63 setTimeout(() => {
64 setPage(window.location.hash.replace("#", ""));
65 }, 0);
66 };
67
68 // Extract base page name from page value (e.g., "settings" from "settings=advanced")
69 const basePageName = page.includes("=") ? page.split("=")[0] : page;
70 const pageLink = ["blocks", "savedTemplate", "settings", "lite-vs-pro"].includes(basePageName) ? basePageName : "pcp_help";
71
72 const blocksMenuLi = postMenu.querySelector(`a[href$="${pageLink}"]`)?.closest("li");
73
74 if(blocksMenuLi) {
75 removeCurrentClass();
76 blocksMenuLi.classList.add("current");
77 }
78
79 const recommendedPage = document.querySelector(".spspc-recommended-page");
80
81 if("our-plugins" === page && recommendedPage) {
82 recommendedPage.style.display = "block";
83 }
84
85 postMenu?.addEventListener("click", postMenuAction);
86
87 return () => postMenu?.removeEventListener("click", postMenuAction);
88 }, [page]);
89
90 // Initialize Crisp chat
91 useEffect(() => {
92 initCrispChat();
93 }, []);
94
95 // Update hash when page changes (for pages with tab parameters like settings=advanced)
96 useEffect(() => {
97 if (page && page.includes("=")) {
98 window.location.hash = `#${page}`;
99 }
100 }, [page]);
101
102 const successMes = useGetOptions(blocksSettings, modulesOptions, integrationOptions);
103
104 const notify = (message, type = "success") => {
105 if (type === "success") {
106 toast.success(message, { style: { marginTop: "20px" } });
107 } else if (type === "error") {
108 toast.error(message, { style: { marginTop: "20px" } });
109 } else {
110 toast(message, { style: { marginTop: "20px" } });
111 }
112 };
113
114 const blockShowHideHandler = (block_name, type = "block-option") => {
115 const notifyChange = (name, currentValue, kind = "block") => {
116 if (successMes) {
117 notify(`The ${kind} has been ${currentValue ? "disabled" : "enabled"}`);
118 }
119 };
120
121 if (type === "block-option") {
122 const newData = blocksSettings?.map((item) =>
123 item.block_name === block_name ? { ...item, show: !item.show } : item
124 );
125 const findCurrentBlock = blocksSettings.find((block) => block.block_name === block_name);
126 notifyChange(block_name, findCurrentBlock?.show, "block");
127 setBlocksSettings(newData);
128 }
129
130 if (type === "modules-option") {
131 const moduleData = modulesOptions?.map((item) => {
132 return item.module_name === block_name ? { ...item, show: !item.show } : item;
133 });
134 const findCurrentModule = modulesOptions.find((item) => item.module_name === block_name);
135 notifyChange(block_name, findCurrentModule?.show, "module");
136 setModulesOptions(moduleData);
137 }
138
139 if (type === "integration-option") {
140 const integrationData = integrationOptions?.map((item) => {
141 return item.name === block_name ? { ...item, show: !item.show } : item;
142 });
143 const findCurrentIntegration = integrationOptions.find((item) => item.name === block_name);
144 notifyChange(block_name, findCurrentIntegration?.show, "integration");
145 setIntegrationOptions(integrationData);
146 }
147 };
148
149 const enabledSavedTemplate = modulesOptions?.some((item) => item.module_name === "saved-template" && item.show);
150
151 if (page === "setupwizard") {
152 return <SetupWizard
153 blocksSettings={blocksSettings}
154 modulesOptions={modulesOptions}
155 blockShowHideHandler={blockShowHideHandler}
156 />;
157 }
158
159 return (
160 <>
161 <Header setPage={setPage} page={page} modulesOptions={modulesOptions} />
162
163 <div className="sp-pcp-blocks-setting-wrapper">
164 {(page === "quick-start" || "" === page) && <QuickStart blocksSettings={blocksSettings} blockShowHideHandler={blockShowHideHandler} setPageAndHash={setPage} modulesOptions={modulesOptions} />}
165 {page === "blocks" && (
166 <BlockPage blocksSettings={blocksSettings} blockShowHideHandler={blockShowHideHandler} />
167 )}
168 {page === "modules" && (
169 <Modules modulesOptions={modulesOptions} blockShowHideHandler={blockShowHideHandler} />
170 )}
171 {page === "lite-vs-pro" && <LightToPro />}
172 {page === "about-us" && <AboutUs />}
173 {( page === "savedTemplate" && enabledSavedTemplate ) && <SavedTemplate />}
174 {page === "integrations" && <Integrations
175 integrationOptions={integrationOptions}
176 blockShowHideHandler={blockShowHideHandler}
177 />}
178 {(page === "settings" || page?.startsWith("settings=")) && <Settings setPage={setPage} />}
179 </div>
180
181 <Toaster position="top-right" reverseOrder={false} />
182 </>
183 );
184 };
185
186 export default Render;
187