← All changes
|
src/Shared/components/ProductAccountActivation/usePluginsActivation.js
+32
-8
3.1.4
→
trunk
View file →
| @@ -1,7 +1,11 @@ | ||
| 1 | 1 | import { AI_HOST } from '@constants'; |
| 2 | +import { reqDataBasics } from '@shared/lib/data'; | |
| 2 | 3 | import useSWRImmutable from 'swr/immutable'; |
| 3 | 4 | |
| 5 | +// The route never reads siteProfile, and the GET would put it in the query string. | |
| 6 | +const { siteProfile, ...basics } = reqDataBasics; | |
| 7 | + | |
| 4 | 8 | export const ACTIVATION_STATUS = { |
| 5 | 9 | displayed: 'displayed', |
| 6 | 10 | completed: 'completed', |
| 7 | 11 | skipped: 'skipped', |
| @@ -6,37 +10,57 @@ | ||
| 6 | 10 | completed: 'completed', |
| 7 | 11 | skipped: 'skipped', |
| 8 | 12 | }; |
| 9 | 13 | |
| 10 | -const getPluginsScriptData = async (slugs) => { | |
| 14 | +export const ACCOUNT_STATUS = { | |
| 15 | + pending: 'pending', | |
| 16 | + success: 'success', | |
| 17 | + error: 'error', | |
| 18 | +}; | |
| 19 | + | |
| 20 | +export const getPluginsScriptData = async ([slugs, ineligible]) => { | |
| 11 | 21 | const params = new URLSearchParams(slugs.map((slug) => ['plugins', slug])); |
| 12 | - params.append('siteId', window.extSharedData.siteId); | |
| 13 | - params.append('partnerId', window.extSharedData.partnerId); | |
| 22 | + for (const slug of ineligible) { | |
| 23 | + params.append('ineligible', slug); | |
| 24 | + } | |
| 25 | + for (const [key, value] of Object.entries(basics)) { | |
| 26 | + params.append(key, value); | |
| 27 | + } | |
| 14 | 28 | |
| 15 | 29 | const response = await fetch(`${AI_HOST}/api/plugins/activate?${params}`); |
| 16 | 30 | return response.json(); |
| 17 | 31 | }; |
| 18 | 32 | |
| 19 | -const patchActivation = async ({ | |
| 33 | +export const patchActivation = async ({ | |
| 20 | 34 | activationId, |
| 21 | 35 | selectedPlugins, |
| 22 | 36 | status, |
| 23 | 37 | context, |
| 24 | 38 | }) => { |
| 39 | + // Losing this record must not cost the account it was recording. | |
| 25 | 40 | await fetch(`${AI_HOST}/api/plugins/activate`, { |
| 26 | 41 | method: 'PATCH', |
| 27 | 42 | headers: { 'Content-Type': 'application/json' }, |
| 28 | - body: JSON.stringify({ activationId, selectedPlugins, status, context }), | |
| 29 | - }); | |
| 43 | + body: JSON.stringify({ | |
| 44 | + ...basics, | |
| 45 | + activationId, | |
| 46 | + selectedPlugins, | |
| 47 | + status, | |
| 48 | + context, | |
| 49 | + }), | |
| 50 | + // The copy invites closing the window; the settled write has to outlive it. | |
| 51 | + keepalive: true, | |
| 52 | + }).catch(() => {}); | |
| 30 | 53 | }; |
| 31 | 54 | |
| 32 | -export const usePluginsActivation = (plugins) => { | |
| 55 | +export const usePluginsActivation = (plugins, ineligible = []) => { | |
| 33 | 56 | const slugs = plugins.map((plugin) => plugin.slug); |
| 57 | + // SWR skips an empty-array key, which would lose the record for an all-ineligible site. | |
| 34 | 58 | const { |
| 35 | 59 | data, |
| 36 | 60 | isLoading: loading, |
| 37 | 61 | error, |
| 38 | - } = useSWRImmutable(slugs, getPluginsScriptData); | |
| 62 | + } = useSWRImmutable([slugs, ineligible], getPluginsScriptData); | |
| 39 | 63 | const { activationId, ...scriptData } = data ?? {}; |
| 40 | 64 | |
| 41 | 65 | const selectedPlugins = plugins |
| 42 | 66 | .filter((plugin) => plugin.selected) |