UsePluginInstallActivate.js
187 lines
| 1 | /** |
| 2 | * External Dependencies |
| 3 | */ |
| 4 | import React, { useContext } from "react"; |
| 5 | import { useToast, Button } from "@chakra-ui/react"; |
| 6 | import apiFetch from "@wordpress/api-fetch"; |
| 7 | import { sprintf, __ } from "@wordpress/i18n"; |
| 8 | |
| 9 | /** |
| 10 | * Internal Dependencies |
| 11 | */ |
| 12 | import DashboardContext from "./../../context/DashboardContext"; |
| 13 | import { actionTypes } from "./../../reducers/DashboardReducer"; |
| 14 | |
| 15 | const UsePluginInstallActivate = ({ |
| 16 | cancelRef, |
| 17 | onClose, |
| 18 | slug, |
| 19 | isPluginStatusLoading, |
| 20 | setIsPluginStatusLoading, |
| 21 | }) => { |
| 22 | const toast = useToast(); |
| 23 | const [{ pluginsStatus }, dispatch] = useContext(DashboardContext); |
| 24 | /* global _EVF_DASHBOARD_ */ |
| 25 | const { evfRestApiNonce, restURL } = |
| 26 | typeof _EVF_DASHBOARD_ !== "undefined" && _EVF_DASHBOARD_; |
| 27 | |
| 28 | const successCallback = (closeFunction) => { |
| 29 | if (typeof closeFunction === "function") { |
| 30 | closeFunction(); |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | const errorCallback = (closeFunction) => { |
| 35 | if (typeof closeFunction === "function") { |
| 36 | closeFunction(); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | const activatePlugin = async ({ slug, file }) => { |
| 41 | setIsPluginStatusLoading(true); |
| 42 | try { |
| 43 | const data = await apiFetch({ |
| 44 | path: restURL + `wp/v2/plugins/${slug}`, |
| 45 | method: "POST", |
| 46 | headers: { |
| 47 | "X-WP-Nonce": evfRestApiNonce, |
| 48 | }, |
| 49 | data: { |
| 50 | plugin: file.replace(".php", ""), |
| 51 | status: "active", |
| 52 | }, |
| 53 | }); |
| 54 | |
| 55 | pluginsStatus[`${data.plugin}.php`] = data.status; |
| 56 | dispatch({ |
| 57 | type: actionTypes.GET_PLUGINS_STATUS, |
| 58 | pluginsStatus: pluginsStatus, |
| 59 | }); |
| 60 | |
| 61 | toast({ |
| 62 | title: "Success", |
| 63 | description: sprintf( |
| 64 | __("%s plugin activated successfully", "everest-forms"), |
| 65 | data.name |
| 66 | ), |
| 67 | status: "success", |
| 68 | duration: 5000, |
| 69 | isClosable: true, |
| 70 | }); |
| 71 | |
| 72 | successCallback(onClose); |
| 73 | } catch (e) { |
| 74 | toast({ |
| 75 | title: "Error", |
| 76 | description: |
| 77 | e.message || __("An error occurred", "everest-forms"), |
| 78 | status: "error", |
| 79 | duration: 5000, |
| 80 | isClosable: true, |
| 81 | }); |
| 82 | |
| 83 | errorCallback(onClose); |
| 84 | } finally { |
| 85 | setIsPluginStatusLoading(false); |
| 86 | onClose(); |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | const installPlugin = async (slug) => { |
| 91 | setIsPluginStatusLoading(true); |
| 92 | |
| 93 | try { |
| 94 | const data = await apiFetch({ |
| 95 | path: restURL + "wp/v2/plugins", |
| 96 | method: "POST", |
| 97 | headers: { |
| 98 | "X-WP-Nonce": evfRestApiNonce, |
| 99 | }, |
| 100 | data: { |
| 101 | slug: slug, |
| 102 | status: "active", |
| 103 | }, |
| 104 | }); |
| 105 | |
| 106 | pluginsStatus[`${data.plugin}.php`] = data.status; |
| 107 | dispatch({ |
| 108 | type: actionTypes.GET_PLUGINS_STATUS, |
| 109 | pluginsStatus: pluginsStatus, |
| 110 | }); |
| 111 | toast({ |
| 112 | title: "Success", |
| 113 | description: sprintf( |
| 114 | __( |
| 115 | "%s plugin installed and activated successfully", |
| 116 | "everest-forms" |
| 117 | ), |
| 118 | data.name |
| 119 | ), |
| 120 | status: "success", |
| 121 | duration: 9000, |
| 122 | isClosable: true, |
| 123 | }); |
| 124 | successCallback(onClose); |
| 125 | } catch (e) { |
| 126 | toast({ |
| 127 | title: "Error", |
| 128 | description: e.message || "An error occurred", |
| 129 | status: "error", |
| 130 | duration: 9000, |
| 131 | isClosable: true, |
| 132 | }); |
| 133 | errorCallback(onClose); |
| 134 | } finally { |
| 135 | setIsPluginStatusLoading(false); |
| 136 | } |
| 137 | onClose(); |
| 138 | }; |
| 139 | |
| 140 | const performPluginAction = (slug) => { |
| 141 | const pluginSlug = slug.split("/")[0]; |
| 142 | |
| 143 | if (pluginsStatus[slug] === "not-installed") { |
| 144 | installPlugin(pluginSlug); |
| 145 | } else if (pluginsStatus[slug] === "inactive") { |
| 146 | activatePlugin({ |
| 147 | slug: pluginSlug, |
| 148 | file: slug, |
| 149 | }); |
| 150 | } |
| 151 | }; |
| 152 | |
| 153 | return ( |
| 154 | <> |
| 155 | <Button |
| 156 | size="sm" |
| 157 | fontSize="xs" |
| 158 | fontWeight="normal" |
| 159 | variant="outline" |
| 160 | colorScheme="primary" |
| 161 | isDisabled={isPluginStatusLoading} |
| 162 | ref={cancelRef} |
| 163 | onClick={onClose} |
| 164 | > |
| 165 | {__("Cancel", "everest-forms")} |
| 166 | </Button> |
| 167 | <Button |
| 168 | size="sm" |
| 169 | fontSize="xs" |
| 170 | fontWeight="normal" |
| 171 | colorScheme="primary" |
| 172 | onClick={() => { |
| 173 | performPluginAction(slug); |
| 174 | }} |
| 175 | ml={3} |
| 176 | isLoading={isPluginStatusLoading} |
| 177 | > |
| 178 | {"inactive" === pluginsStatus[slug] |
| 179 | ? __("Activate", "everest-forms") |
| 180 | : __("Install", "everest-forms")} |
| 181 | </Button> |
| 182 | </> |
| 183 | ); |
| 184 | }; |
| 185 | |
| 186 | export default UsePluginInstallActivate; |
| 187 |