PluginProbe
Extendify / 3.2.0
Extendify v3.2.0
3.2.3 3.2.2 3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 All 129 releases
extendify / src / Shared / components / ProductAccountActivation / usePluginsActivation.js

usePluginsActivation.js in Extendify 3.2.0, at src/Shared/components/ProductAccountActivation/usePluginsActivation.js

65 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { AI_HOST } from '@constants';
2 import { reqDataBasics } from '@shared/lib/data';
3 import useSWRImmutable from 'swr/immutable';
4
5 // The route never reads siteProfile, and the GET would put it in the query string.
6 const { siteProfile, ...basics } = reqDataBasics;
7
8 export const ACTIVATION_STATUS = {
9 displayed: 'displayed',
10 completed: 'completed',
11 skipped: 'skipped',
12 };
13
14 export const getPluginsScriptData = async ([slugs, ineligible]) => {
15 const params = new URLSearchParams(slugs.map((slug) => ['plugins', slug]));
16 for (const slug of ineligible) {
17 params.append('ineligible', slug);
18 }
19 for (const [key, value] of Object.entries(basics)) {
20 params.append(key, value);
21 }
22
23 const response = await fetch(`${AI_HOST}/api/plugins/activate?${params}`);
24 return response.json();
25 };
26
27 export const patchActivation = async ({
28 activationId,
29 selectedPlugins,
30 status,
31 context,
32 }) => {
33 await fetch(`${AI_HOST}/api/plugins/activate`, {
34 method: 'PATCH',
35 headers: { 'Content-Type': 'application/json' },
36 body: JSON.stringify({
37 ...basics,
38 activationId,
39 selectedPlugins,
40 status,
41 context,
42 }),
43 });
44 };
45
46 export const usePluginsActivation = (plugins, ineligible = []) => {
47 const slugs = plugins.map((plugin) => plugin.slug);
48 // SWR skips an empty-array key, which would lose the record for an all-ineligible site.
49 const {
50 data,
51 isLoading: loading,
52 error,
53 } = useSWRImmutable([slugs, ineligible], getPluginsScriptData);
54 const { activationId, ...scriptData } = data ?? {};
55
56 const selectedPlugins = plugins
57 .filter((plugin) => plugin.selected)
58 .map((plugin) => plugin.slug);
59
60 const activatePlugins = ({ status, context = undefined }) =>
61 patchActivation({ activationId, selectedPlugins, status, context });
62
63 return { scriptData, activatePlugins, loading, error };
64 };
65