|
| 1 | +/* global neveDash */ |
| 2 | +import { __, sprintf } from '@wordpress/i18n'; |
| 3 | +import { |
| 4 | + NEVE_AVAILABLE_MODULES_ICON_MAP, |
| 5 | + NEVE_STORE, |
| 6 | +} from '../../utils/constants'; |
| 7 | +import { ArrowRight, LoaderCircle, LucideSettings } from 'lucide-react'; |
| 8 | +import Card from '../../Layout/Card'; |
| 9 | +import { useState } from '@wordpress/element'; |
| 10 | +import { useDispatch, useSelect } from '@wordpress/data'; |
| 11 | +import Toggle from '../Common/Toggle'; |
| 12 | +import { send } from '../../utils/rest'; |
| 13 | + |
| 14 | +const Toast = ({ message }) => { |
| 15 | + return ( |
| 16 | + <div className="fixed flex items-center gap-2 z-[99999] top-10 right-5 bg-white text-black text-base font-medium px-4 py-2 rounded shadow border border-solid border-blue-600"> |
| 17 | + {message} |
| 18 | + </div> |
| 19 | + ); |
| 20 | +}; |
| 21 | + |
| 22 | +const ModuleToggle = ({ |
| 23 | + slug, |
| 24 | + moduleData, |
| 25 | + setMessage, |
| 26 | + isActive, |
| 27 | + setIsActive, |
| 28 | + isInstalled, |
| 29 | + setIsInstalled, |
| 30 | +}) => { |
| 31 | + const [loading, setLoading] = useState(false); |
| 32 | + const { changeModuleStatus, setObfxModuleStatus, setToast } = |
| 33 | + useDispatch(NEVE_STORE); |
| 34 | + const { moduleStatus } = useSelect((select) => { |
| 35 | + const { getObfxModuleStatus } = select(NEVE_STORE); |
| 36 | + |
| 37 | + return { |
| 38 | + moduleStatus: getObfxModuleStatus(slug) || false, |
| 39 | + }; |
| 40 | + }); |
| 41 | + |
| 42 | + const { api } = neveDash; |
| 43 | + const { title } = moduleData; |
| 44 | + const toastMessage = { |
| 45 | + //translators: %s - Plugin name |
| 46 | + installing: sprintf(__('Installing %s', 'neve'), 'Orbit Fox Plugin'), |
| 47 | + //translators: %s - Plugin name |
| 48 | + activating: sprintf(__('Activating %s', 'neve'), 'Orbit Fox Plugin'), |
| 49 | + }; |
| 50 | + |
| 51 | + const handleToggle = async (value) => { |
| 52 | + try { |
| 53 | + setLoading(true); |
| 54 | + changeModuleStatus(slug, value); |
| 55 | + |
| 56 | + let isPluginActive = true; |
| 57 | + // Handle plugin installation or activation |
| 58 | + if (!isInstalled) { |
| 59 | + setMessage(toastMessage.installing); |
| 60 | + isPluginActive = false; |
| 61 | + } else if (!isActive) { |
| 62 | + setMessage(toastMessage.activating); |
| 63 | + isPluginActive = false; |
| 64 | + } |
| 65 | + |
| 66 | + if (!isPluginActive) { |
| 67 | + await send(api + 'activate-plugin', { |
| 68 | + slug: 'themeisle-companion', |
| 69 | + }).then((res) => { |
| 70 | + if (res.success) { |
| 71 | + setIsInstalled(true); |
| 72 | + setIsActive(true); |
| 73 | + } |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + // Fire the send method after install/activate or immediately if both are done |
| 78 | + const response = await send(api + 'activate-module', { |
| 79 | + slug, |
| 80 | + value, |
| 81 | + }); |
| 82 | + |
| 83 | + setObfxModuleStatus(slug, response.success ? value : !value); |
| 84 | + setToast( |
| 85 | + response.success |
| 86 | + ? (value |
| 87 | + ? __('Module Activated', 'neve') |
| 88 | + : __('Module Deactivated.', 'neve')) + ` (${title})` |
| 89 | + : response.data |
| 90 | + ); |
| 91 | + } catch (error) { |
| 92 | + setToast( |
| 93 | + __('Something went wrong while activating the module.', 'neve') |
| 94 | + ); |
| 95 | + } finally { |
| 96 | + setLoading(false); |
| 97 | + setMessage(''); |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + return ( |
| 102 | + <div className="flex gap-2 items-center"> |
| 103 | + <Toggle |
| 104 | + checked={moduleStatus} |
| 105 | + onToggle={handleToggle} |
| 106 | + disabled={loading} |
| 107 | + /> |
| 108 | + </div> |
| 109 | + ); |
| 110 | +}; |
| 111 | + |
| 112 | +const AvailableModuleCard = ({ |
| 113 | + moduleData, |
| 114 | + slug, |
| 115 | + setMessage, |
| 116 | + isActive, |
| 117 | + setIsActive, |
| 118 | + isInstalled, |
| 119 | + setIsInstalled, |
| 120 | +}) => { |
| 121 | + const { title, description } = moduleData; |
| 122 | + const CardIcon = NEVE_AVAILABLE_MODULES_ICON_MAP[slug] || LucideSettings; |
| 123 | + |
| 124 | + return ( |
| 125 | + <Card |
| 126 | + icon={<CardIcon size={18} />} |
| 127 | + title={title} |
| 128 | + className="bg-white p-6 rounded-lg shadow-sm" |
| 129 | + afterTitle={ |
| 130 | + <ModuleToggle |
| 131 | + slug={slug} |
| 132 | + moduleData={moduleData} |
| 133 | + setMessage={setMessage} |
| 134 | + isActive={isActive} |
| 135 | + setIsActive={setIsActive} |
| 136 | + isInstalled={isInstalled} |
| 137 | + setIsInstalled={setIsInstalled} |
| 138 | + /> |
| 139 | + } |
| 140 | + id={`module-${slug}`} |
| 141 | + > |
| 142 | + <p className="text-gray-600 text-sm leading-relaxed"> |
| 143 | + {description} |
| 144 | + </p> |
| 145 | + {!isActive ? ( |
| 146 | + <p className="mt-2 italic text-sm text-gray-500"> |
| 147 | + {__( |
| 148 | + 'This feature is part of OrbitFox plugin, built by the Neve team. Enabling the toggle will automatically install and activate the plugin.', |
| 149 | + 'neve' |
| 150 | + )} |
| 151 | + </p> |
| 152 | + ) : ( |
| 153 | + <a |
| 154 | + className="flex mt-2 text-blue-600 gap-2 align-middle" |
| 155 | + href="admin.php?page=obfx_companion" |
| 156 | + > |
| 157 | + {__('Go to Settings to Edit', 'neve')} |
| 158 | + <ArrowRight size={18} /> |
| 159 | + </a> |
| 160 | + )} |
| 161 | + </Card> |
| 162 | + ); |
| 163 | +}; |
| 164 | + |
| 165 | +export default () => { |
| 166 | + const [message, setMessage] = useState(''); |
| 167 | + const [isInstalled, setIsInstalled] = useState( |
| 168 | + neveDash.orbitFox.isInstalled |
| 169 | + ); |
| 170 | + const [isActive, setIsActive] = useState(neveDash.orbitFox.isActive); |
| 171 | + |
| 172 | + return ( |
| 173 | + <> |
| 174 | + <div className="mb-6"> |
| 175 | + <div className="flex items-center justify-between"> |
| 176 | + <h2 className="text-lg font-semibold"> |
| 177 | + {__('Available Modules', 'neve')} |
| 178 | + </h2> |
| 179 | + </div> |
| 180 | + <div className="grid xl:grid-cols-2 gap-6"> |
| 181 | + {Object.entries(neveDash.availableModules).map( |
| 182 | + ([slug, moduleData]) => ( |
| 183 | + <AvailableModuleCard |
| 184 | + key={slug} |
| 185 | + slug={slug} |
| 186 | + moduleData={moduleData} |
| 187 | + setMessage={setMessage} |
| 188 | + isActive={isActive} |
| 189 | + setIsActive={setIsActive} |
| 190 | + isInstalled={isInstalled} |
| 191 | + setIsInstalled={setIsInstalled} |
| 192 | + /> |
| 193 | + ) |
| 194 | + )} |
| 195 | + </div> |
| 196 | + </div> |
| 197 | + {message && ( |
| 198 | + <Toast |
| 199 | + message={ |
| 200 | + <> |
| 201 | + <LoaderCircle |
| 202 | + size={18} |
| 203 | + className="animate-spin text-blue-800" |
| 204 | + /> |
| 205 | + {message} |
| 206 | + </> |
| 207 | + } |
| 208 | + /> |
| 209 | + )} |
| 210 | + </> |
| 211 | + ); |
| 212 | +}; |
0 commit comments