From bf635f157f6f224b6c27d51f76f8c7d4cbc1a121 Mon Sep 17 00:00:00 2001 From: Gustav Hansen Date: Tue, 1 Aug 2023 17:10:21 +0200 Subject: [PATCH] Chore: Move marketplace and plugins hooks into their page contexts --- .../useFetchMarketplacePlugins/index.js | 44 ------------------- .../useFetchMarketplaceProviders/index.js | 44 ------------------- .../src/hooks/useFetchEnabledPlugins/index.js | 24 ---------- .../hooks/useFetchEnabledPlugins/utils/api.js | 10 ----- .../src/pages/InstalledPluginsPage/Plugins.js | 4 +- .../hooks/usePlugins/index.js | 29 ++++++++++++ .../MarketplacePage}/constants.js | 0 .../hooks/useFetchMarketplacePlugins/index.js | 44 +++++++++++++++++++ .../useFetchMarketplaceProviders/index.js | 44 +++++++++++++++++++ .../utils/useMarketplaceData.js | 4 +- 10 files changed, 121 insertions(+), 126 deletions(-) delete mode 100644 packages/core/admin/admin/src/hooks/marketplace/useFetchMarketplacePlugins/index.js delete mode 100644 packages/core/admin/admin/src/hooks/marketplace/useFetchMarketplaceProviders/index.js delete mode 100644 packages/core/admin/admin/src/hooks/useFetchEnabledPlugins/index.js delete mode 100644 packages/core/admin/admin/src/hooks/useFetchEnabledPlugins/utils/api.js create mode 100644 packages/core/admin/admin/src/pages/InstalledPluginsPage/hooks/usePlugins/index.js rename packages/core/admin/admin/src/{hooks/marketplace => pages/MarketplacePage}/constants.js (100%) create mode 100644 packages/core/admin/admin/src/pages/MarketplacePage/hooks/useFetchMarketplacePlugins/index.js create mode 100644 packages/core/admin/admin/src/pages/MarketplacePage/hooks/useFetchMarketplaceProviders/index.js diff --git a/packages/core/admin/admin/src/hooks/marketplace/useFetchMarketplacePlugins/index.js b/packages/core/admin/admin/src/hooks/marketplace/useFetchMarketplacePlugins/index.js deleted file mode 100644 index 5731e6dc97..0000000000 --- a/packages/core/admin/admin/src/hooks/marketplace/useFetchMarketplacePlugins/index.js +++ /dev/null @@ -1,44 +0,0 @@ -import { useNotification } from '@strapi/helper-plugin'; -import qs from 'qs'; -import { useQuery } from 'react-query'; - -import { MARKETPLACE_API_URL } from '../constants'; - -const fetchMarketplacePlugins = async (params = {}) => { - try { - const queryString = qs.stringify(qs.parse(params)); - const res = await fetch(`${MARKETPLACE_API_URL}/plugins?${queryString}`); - - if (!res.ok) { - throw new Error('Failed to fetch marketplace plugins.'); - } - - const data = await res.json(); - - return data; - } catch (error) { - console.error(error); - } - - return null; -}; - -const useFetchMarketplacePlugins = (notifyLoad, params) => { - const toggleNotification = useNotification(); - - return useQuery(['list-marketplace-plugins', params], () => fetchMarketplacePlugins(params), { - onSuccess() { - if (notifyLoad) { - notifyLoad(); - } - }, - onError() { - toggleNotification({ - type: 'warning', - message: { id: 'notification.error', defaultMessage: 'An error occured' }, - }); - }, - }); -}; - -export default useFetchMarketplacePlugins; diff --git a/packages/core/admin/admin/src/hooks/marketplace/useFetchMarketplaceProviders/index.js b/packages/core/admin/admin/src/hooks/marketplace/useFetchMarketplaceProviders/index.js deleted file mode 100644 index 613ddce059..0000000000 --- a/packages/core/admin/admin/src/hooks/marketplace/useFetchMarketplaceProviders/index.js +++ /dev/null @@ -1,44 +0,0 @@ -import { useNotification } from '@strapi/helper-plugin'; -import qs from 'qs'; -import { useQuery } from 'react-query'; - -import { MARKETPLACE_API_URL } from '../constants'; - -const fetchMarketplaceProviders = async (params = {}) => { - try { - const queryString = qs.stringify(qs.parse(params)); - const res = await fetch(`${MARKETPLACE_API_URL}/providers?${queryString}`); - - if (!res.ok) { - throw new Error('Failed to fetch marketplace providers.'); - } - - const data = await res.json(); - - return data; - } catch (error) { - console.error(error); - } - - return null; -}; - -const useFetchMarketplaceProviders = (notifyLoad, params) => { - const toggleNotification = useNotification(); - - return useQuery(['list-marketplace-providers', params], () => fetchMarketplaceProviders(params), { - onSuccess() { - if (notifyLoad) { - notifyLoad(); - } - }, - onError() { - toggleNotification({ - type: 'warning', - message: { id: 'notification.error', defaultMessage: 'An error occured' }, - }); - }, - }); -}; - -export default useFetchMarketplaceProviders; diff --git a/packages/core/admin/admin/src/hooks/useFetchEnabledPlugins/index.js b/packages/core/admin/admin/src/hooks/useFetchEnabledPlugins/index.js deleted file mode 100644 index 2e43a0427e..0000000000 --- a/packages/core/admin/admin/src/hooks/useFetchEnabledPlugins/index.js +++ /dev/null @@ -1,24 +0,0 @@ -import { useNotification } from '@strapi/helper-plugin'; -import { useQuery } from 'react-query'; - -import { fetchEnabledPlugins } from './utils/api'; - -const useFetchEnabledPlugins = (notifyLoad) => { - const toggleNotification = useNotification(); - - return useQuery('list-enabled-plugins', () => fetchEnabledPlugins(), { - onSuccess() { - if (notifyLoad) { - notifyLoad(); - } - }, - onError() { - toggleNotification({ - type: 'warning', - message: { id: 'notification.error', defaultMessage: 'An error occured' }, - }); - }, - }); -}; - -export default useFetchEnabledPlugins; diff --git a/packages/core/admin/admin/src/hooks/useFetchEnabledPlugins/utils/api.js b/packages/core/admin/admin/src/hooks/useFetchEnabledPlugins/utils/api.js deleted file mode 100644 index 2200e50282..0000000000 --- a/packages/core/admin/admin/src/hooks/useFetchEnabledPlugins/utils/api.js +++ /dev/null @@ -1,10 +0,0 @@ -import { getFetchClient } from '@strapi/helper-plugin'; - -const fetchEnabledPlugins = async () => { - const { get } = getFetchClient(); - const { data } = await get('/admin/plugins'); - - return data; -}; - -export { fetchEnabledPlugins }; diff --git a/packages/core/admin/admin/src/pages/InstalledPluginsPage/Plugins.js b/packages/core/admin/admin/src/pages/InstalledPluginsPage/Plugins.js index 4dfc60a9ba..e9c674231d 100644 --- a/packages/core/admin/admin/src/pages/InstalledPluginsPage/Plugins.js +++ b/packages/core/admin/admin/src/pages/InstalledPluginsPage/Plugins.js @@ -17,7 +17,7 @@ import { import { LoadingIndicatorPage, useFocusWhenNavigate } from '@strapi/helper-plugin'; import { useIntl } from 'react-intl'; -import useFetchEnabledPlugins from '../../hooks/useFetchEnabledPlugins'; +import { usePlugins } from './hooks/usePlugins'; const Plugins = () => { const { formatMessage } = useIntl(); @@ -41,7 +41,7 @@ const Plugins = () => { ); }; - const { status, data } = useFetchEnabledPlugins(notifyPluginPageLoad); + const { status, data } = usePlugins(notifyPluginPageLoad); const isLoading = status !== 'success' && status !== 'error'; diff --git a/packages/core/admin/admin/src/pages/InstalledPluginsPage/hooks/usePlugins/index.js b/packages/core/admin/admin/src/pages/InstalledPluginsPage/hooks/usePlugins/index.js new file mode 100644 index 0000000000..0758ec83d2 --- /dev/null +++ b/packages/core/admin/admin/src/pages/InstalledPluginsPage/hooks/usePlugins/index.js @@ -0,0 +1,29 @@ +import { useFetchClient, useNotification } from '@strapi/helper-plugin'; +import { useQuery } from 'react-query'; + +export const usePlugins = (notifyLoad) => { + const toggleNotification = useNotification(); + const { get } = useFetchClient(); + + return useQuery( + ['plugins'], + async () => { + const { data } = await get('/admin/plugins'); + + return data; + }, + { + onSuccess() { + if (notifyLoad) { + notifyLoad(); + } + }, + onError() { + toggleNotification({ + type: 'warning', + message: { id: 'notification.error', defaultMessage: 'An error occured' }, + }); + }, + } + ); +}; diff --git a/packages/core/admin/admin/src/hooks/marketplace/constants.js b/packages/core/admin/admin/src/pages/MarketplacePage/constants.js similarity index 100% rename from packages/core/admin/admin/src/hooks/marketplace/constants.js rename to packages/core/admin/admin/src/pages/MarketplacePage/constants.js diff --git a/packages/core/admin/admin/src/pages/MarketplacePage/hooks/useFetchMarketplacePlugins/index.js b/packages/core/admin/admin/src/pages/MarketplacePage/hooks/useFetchMarketplacePlugins/index.js new file mode 100644 index 0000000000..6f414ae49c --- /dev/null +++ b/packages/core/admin/admin/src/pages/MarketplacePage/hooks/useFetchMarketplacePlugins/index.js @@ -0,0 +1,44 @@ +import { useNotification } from '@strapi/helper-plugin'; +import qs from 'qs'; +import { useQuery } from 'react-query'; + +import { MARKETPLACE_API_URL } from '../../constants'; + +export const useFetchMarketplacePlugins = (notifyLoad, params = {}) => { + const toggleNotification = useNotification(); + + return useQuery( + ['marketplace', 'plugins', params], + async () => { + try { + const queryString = qs.stringify(qs.parse(params)); + const res = await fetch(`${MARKETPLACE_API_URL}/plugins?${queryString}`); + + if (!res.ok) { + throw new Error('Failed to fetch marketplace plugins.'); + } + + const data = await res.json(); + + return data; + } catch (error) { + // silence + } + + return null; + }, + { + onSuccess() { + if (notifyLoad) { + notifyLoad(); + } + }, + onError() { + toggleNotification({ + type: 'warning', + message: { id: 'notification.error', defaultMessage: 'An error occured' }, + }); + }, + } + ); +}; diff --git a/packages/core/admin/admin/src/pages/MarketplacePage/hooks/useFetchMarketplaceProviders/index.js b/packages/core/admin/admin/src/pages/MarketplacePage/hooks/useFetchMarketplaceProviders/index.js new file mode 100644 index 0000000000..b82c05792a --- /dev/null +++ b/packages/core/admin/admin/src/pages/MarketplacePage/hooks/useFetchMarketplaceProviders/index.js @@ -0,0 +1,44 @@ +import { useNotification } from '@strapi/helper-plugin'; +import qs from 'qs'; +import { useQuery } from 'react-query'; + +import { MARKETPLACE_API_URL } from '../../constants'; + +export const useFetchMarketplaceProviders = (notifyLoad, params = {}) => { + const toggleNotification = useNotification(); + + return useQuery( + ['marketplace', 'providers', params], + async () => { + try { + const queryString = qs.stringify(qs.parse(params)); + const res = await fetch(`${MARKETPLACE_API_URL}/providers?${queryString}`); + + if (!res.ok) { + throw new Error('Failed to fetch marketplace providers.'); + } + + const data = await res.json(); + + return data; + } catch (error) { + // silence + } + + return null; + }, + { + onSuccess() { + if (notifyLoad) { + notifyLoad(); + } + }, + onError() { + toggleNotification({ + type: 'warning', + message: { id: 'notification.error', defaultMessage: 'An error occured' }, + }); + }, + } + ); +}; diff --git a/packages/core/admin/admin/src/pages/MarketplacePage/utils/useMarketplaceData.js b/packages/core/admin/admin/src/pages/MarketplacePage/utils/useMarketplaceData.js index e338ec0ae2..282f46c97b 100644 --- a/packages/core/admin/admin/src/pages/MarketplacePage/utils/useMarketplaceData.js +++ b/packages/core/admin/admin/src/pages/MarketplacePage/utils/useMarketplaceData.js @@ -3,8 +3,8 @@ import { useEffect, useState } from 'react'; import { useNotifyAT } from '@strapi/design-system'; import { useIntl } from 'react-intl'; -import useFetchMarketplacePlugins from '../../../hooks/marketplace/useFetchMarketplacePlugins'; -import useFetchMarketplaceProviders from '../../../hooks/marketplace/useFetchMarketplaceProviders'; +import { useFetchMarketplacePlugins } from '../hooks/useFetchMarketplacePlugins'; +import { useFetchMarketplaceProviders } from '../hooks/useFetchMarketplaceProviders'; function useMarketplaceData({ npmPackageType, debouncedSearch, query, tabQuery }) { const { notifyStatus } = useNotifyAT();