ModuleBody.js
288 lines
| 1 | /** |
| 2 | * External Dependencies |
| 3 | */ |
| 4 | import React, { useState, useEffect, useContext } from "react"; |
| 5 | import { |
| 6 | Tabs, |
| 7 | Container, |
| 8 | Modal, |
| 9 | ModalOverlay, |
| 10 | ModalContent, |
| 11 | ModalCloseButton, |
| 12 | ModalFooter, |
| 13 | Button, |
| 14 | Text, |
| 15 | SimpleGrid, |
| 16 | Input, |
| 17 | Link, |
| 18 | VStack, |
| 19 | useToast, |
| 20 | } from "@chakra-ui/react"; |
| 21 | import { sprintf, __ } from "@wordpress/i18n"; |
| 22 | |
| 23 | /** |
| 24 | * Internal Dependencies |
| 25 | */ |
| 26 | import { isArray, isEmpty } from "./../../../utils/utils"; |
| 27 | import { actionTypes } from "./../../../reducers/DashboardReducer"; |
| 28 | import DashboardContext from "./../../../context/DashboardContext"; |
| 29 | import { Lock } from "./../../../components/Icon/Icon"; |
| 30 | import ModuleItem from "./ModuleItem"; |
| 31 | import AddonsSkeleton from "./../../../skeleton/AddonsSkeleton/AddonsSkeleton"; |
| 32 | import { activateLicense } from "./modules-api"; |
| 33 | |
| 34 | const ModuleBody = ({ |
| 35 | isPerformingBulkAction, |
| 36 | filteredAddons, |
| 37 | setSelectedModuleData, |
| 38 | selectedModuleData, |
| 39 | }) => { |
| 40 | /* global _EVF_DASHBOARD_ */ |
| 41 | const { upgradeURL, licenseActivationURL, licensePlan, isPro } = |
| 42 | typeof _EVF_DASHBOARD_ !== "undefined" && _EVF_DASHBOARD_; |
| 43 | const [{ upgradeModal }, dispatch] = useContext(DashboardContext); |
| 44 | const [upgradeContent, setUpgradeContent] = useState({ |
| 45 | title: "", |
| 46 | body: "", |
| 47 | buttonText: __("Upgrade to Pro", "everest-forms"), |
| 48 | upgradeURL: |
| 49 | upgradeURL + |
| 50 | "&utm_source=dashboard-addons&utm_medium=upgrade-popup", |
| 51 | licenseActivationPlaceholder: __("License key","everest-forms"), |
| 52 | }); |
| 53 | |
| 54 | const toast = useToast(); |
| 55 | |
| 56 | const [licenseActivationKey, setLicenseKey] = useState(''); |
| 57 | const [licenseActivationValidationMessage, setLicenseValidationMessage] = useState(''); |
| 58 | const [reloadPage, setReloadPage] = useState(false); |
| 59 | |
| 60 | const handleActivationKeyChange = (event) => { |
| 61 | setLicenseKey(event.target.value); |
| 62 | }; |
| 63 | |
| 64 | const [licenseValidationStatus, setLicenseValidationStatus] = useState(''); |
| 65 | |
| 66 | const [isLicenseActivation, setLicenseActivation] = useState(false); |
| 67 | |
| 68 | const handleCheckedChange = (slug, checked, name, type) => { |
| 69 | var selectedModules = { ...selectedModuleData }; |
| 70 | |
| 71 | if (checked) { |
| 72 | selectedModules[slug] = { |
| 73 | slug: slug + "/" + slug + ".php", |
| 74 | name, |
| 75 | type, |
| 76 | }; |
| 77 | } else { |
| 78 | if (selectedModules.hasOwnProperty(slug)) { |
| 79 | delete selectedModules[slug]; |
| 80 | } |
| 81 | } |
| 82 | setSelectedModuleData(selectedModules); |
| 83 | }; |
| 84 | |
| 85 | useEffect(() => { |
| 86 | const upgradeContentRef = { ...upgradeContent }; |
| 87 | |
| 88 | if (upgradeModal.enable) { |
| 89 | if (!isPro) { |
| 90 | upgradeContentRef.title = __( |
| 91 | "Everest Froms Pro Required", |
| 92 | "everest-forms" |
| 93 | ); |
| 94 | upgradeContentRef.body = sprintf( |
| 95 | __( |
| 96 | "%s requires Everest Froms Pro to be activated. Please upgrade to a premium plan and unlock this %s.", |
| 97 | "everest-forms" |
| 98 | ), |
| 99 | upgradeModal.moduleName, |
| 100 | upgradeModal.moduleType |
| 101 | ); |
| 102 | } else { |
| 103 | if ( !licensePlan) { |
| 104 | upgradeContentRef.title = __( |
| 105 | "License Activation Required", |
| 106 | "everest-forms" |
| 107 | ); |
| 108 | upgradeContentRef.body = sprintf( |
| 109 | __( |
| 110 | "Please activate license of Everest Froms Pro plugin in order to use %s", |
| 111 | "everest-forms" |
| 112 | ), |
| 113 | upgradeModal.moduleName |
| 114 | ); |
| 115 | upgradeContentRef.buttonText = sprintf( |
| 116 | __("Activate License", "everest-forms"), |
| 117 | upgradeModal.moduleName |
| 118 | ); |
| 119 | upgradeContentRef.licenseActivationPlaceholder = sprintf( |
| 120 | __("Enter your license key", "everest-forms"), |
| 121 | upgradeModal.moduleName |
| 122 | ); |
| 123 | upgradeContentRef.upgradeURL = licenseActivationURL; |
| 124 | } |
| 125 | |
| 126 | } |
| 127 | setUpgradeContent(upgradeContentRef); |
| 128 | } |
| 129 | }, [upgradeModal]); |
| 130 | |
| 131 | useEffect(() => { |
| 132 | if(reloadPage){ |
| 133 | window.location.reload(); |
| 134 | setReloadPage(false); |
| 135 | } |
| 136 | },[reloadPage]); |
| 137 | |
| 138 | const updateUpgradeModal = () => { |
| 139 | const upgradeModalRef = { ...upgradeModal }; |
| 140 | upgradeModalRef.enable = false; |
| 141 | dispatch({ |
| 142 | type: actionTypes.GET_UPGRADE_MODAL, |
| 143 | upgradeModal: upgradeModalRef, |
| 144 | }); |
| 145 | }; |
| 146 | |
| 147 | const licenseActivation = () => { |
| 148 | if( '' === licenseActivationKey ){ |
| 149 | setLicenseValidationMessage(sprintf(__('Please enter your plugin activation license key','everest-forms'))); |
| 150 | setLicenseValidationStatus(true); |
| 151 | } else if( licenseActivationKey.length < 32 ){ |
| 152 | setLicenseValidationMessage(sprintf(__('Please enter the valid license key','everest-forms'))); |
| 153 | setLicenseValidationStatus(true); |
| 154 | } else { |
| 155 | setLicenseValidationMessage(''); |
| 156 | setLicenseValidationStatus(false); |
| 157 | setLicenseActivation(true); |
| 158 | |
| 159 | activateLicense(licenseActivationKey) |
| 160 | .then((data) => { |
| 161 | setLicenseActivation(true); |
| 162 | if (data.code === 200) { |
| 163 | toast({ |
| 164 | title: data.message, |
| 165 | status: "success", |
| 166 | duration: 3000, |
| 167 | }); |
| 168 | setLicenseActivation(false); |
| 169 | setReloadPage(true); |
| 170 | } else if( data.code === 400 ) { |
| 171 | toast({ |
| 172 | title: data.message, |
| 173 | status: "error", |
| 174 | duration: 3000, |
| 175 | }); |
| 176 | } |
| 177 | }).catch((e) => { |
| 178 | toast({ |
| 179 | title: e.message, |
| 180 | status: "error", |
| 181 | duration: 3000, |
| 182 | }); |
| 183 | }).finally(() => { |
| 184 | setLicenseActivation(false); |
| 185 | }); |
| 186 | } |
| 187 | }; |
| 188 | |
| 189 | return ( |
| 190 | <> |
| 191 | <Tabs> |
| 192 | {upgradeModal.enable && ( |
| 193 | <Modal |
| 194 | isOpen={true} |
| 195 | onClose={updateUpgradeModal} |
| 196 | size="lg" |
| 197 | isCentered |
| 198 | > |
| 199 | <ModalOverlay /> |
| 200 | <ModalContent |
| 201 | alignItems={"center"} |
| 202 | p="50px 11px 55px 11px" |
| 203 | > |
| 204 | <Lock h={"131px"} w={"150px"} /> |
| 205 | <Text |
| 206 | fontSize="24px" |
| 207 | lineHeight="44px" |
| 208 | fontWeight="600" |
| 209 | > |
| 210 | {upgradeContent.title} |
| 211 | </Text> |
| 212 | <ModalCloseButton boxShadow="none !important" /> |
| 213 | <Text |
| 214 | fontSize="16px" |
| 215 | lineHeight="26px" |
| 216 | fontWeight="400" |
| 217 | padding="10px 50px" |
| 218 | > |
| 219 | {upgradeContent.body} |
| 220 | </Text> |
| 221 | <ModalFooter paddingBottom="0px" w="400px"> |
| 222 | <VStack |
| 223 | width="100%" |
| 224 | > |
| 225 | {isPro && ( |
| 226 | <Input |
| 227 | placeholder={upgradeContent.licenseActivationPlaceholder} |
| 228 | onChange={handleActivationKeyChange} |
| 229 | /> |
| 230 | )} |
| 231 | <Button |
| 232 | colorScheme="primary" |
| 233 | color="white !important" |
| 234 | textDecor="none !important" |
| 235 | onClick={licenseActivation} |
| 236 | w="100%" |
| 237 | as={!isPro ? Link : ''} |
| 238 | href={!isPro ? upgradeContent.upgradeURL : ''} |
| 239 | isExternal |
| 240 | isLoading = {isLicenseActivation} |
| 241 | > |
| 242 | {upgradeContent.buttonText} |
| 243 | </Button> |
| 244 | {isPro && licenseActivationValidationMessage && ( |
| 245 | <Text fontSize='md' color='red'>{licenseActivationValidationMessage}</Text> |
| 246 | )} |
| 247 | </VStack> |
| 248 | </ModalFooter> |
| 249 | </ModalContent> |
| 250 | </Modal> |
| 251 | )} |
| 252 | <Container maxW="container.xl"> |
| 253 | {isEmpty(filteredAddons) ? ( |
| 254 | <AddonsSkeleton /> |
| 255 | ) : ( |
| 256 | <SimpleGrid columns={4} spacing="5"> |
| 257 | {isArray(filteredAddons) && |
| 258 | filteredAddons?.map((data) => ( |
| 259 | <ModuleItem |
| 260 | key={data.slug} |
| 261 | data={data} |
| 262 | isChecked={selectedModuleData.hasOwnProperty( |
| 263 | data.slug |
| 264 | )} |
| 265 | onCheckedChange={(slug, checked) => { |
| 266 | handleCheckedChange( |
| 267 | slug, |
| 268 | checked, |
| 269 | data.name, |
| 270 | data.type |
| 271 | ); |
| 272 | }} |
| 273 | isPerformingBulkAction={ |
| 274 | isPerformingBulkAction |
| 275 | } |
| 276 | selectedModuleData={selectedModuleData} |
| 277 | /> |
| 278 | ))} |
| 279 | </SimpleGrid> |
| 280 | )} |
| 281 | </Container> |
| 282 | </Tabs> |
| 283 | </> |
| 284 | ); |
| 285 | }; |
| 286 | |
| 287 | export default ModuleBody; |
| 288 |