From 9178f1e902631081b55265a183f1886566e9a9f3 Mon Sep 17 00:00:00 2001 From: Boaz Poolman Date: Wed, 8 May 2024 20:46:09 +0200 Subject: [PATCH] fix: admin requests --- admin/src/components/ActionButtons/index.jsx | 7 ++-- admin/src/components/FirstExport/index.jsx | 7 ++-- admin/src/containers/ConfigPage/index.jsx | 11 +++++-- admin/src/state/actions/Config.js | 34 ++++++++------------ 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/admin/src/components/ActionButtons/index.jsx b/admin/src/components/ActionButtons/index.jsx index 61433c7..afe5cfc 100644 --- a/admin/src/components/ActionButtons/index.jsx +++ b/admin/src/components/ActionButtons/index.jsx @@ -4,15 +4,16 @@ import { useDispatch, useSelector } from 'react-redux'; import { isEmpty } from 'lodash'; import { Button } from '@strapi/design-system'; import { Map } from 'immutable'; -import { useNotification } from '@strapi/strapi/admin'; +import { getFetchClient, useNotification } from '@strapi/strapi/admin'; import { useIntl } from 'react-intl'; import ConfirmModal from '../ConfirmModal'; import { exportAllConfig, importAllConfig } from '../../state/actions/Config'; const ActionButtons = () => { + const { post, get } = getFetchClient(); const dispatch = useDispatch(); - const toggleNotification = useNotification(); + const { toggleNotification } = useNotification(); const [modalIsOpen, setModalIsOpen] = useState(false); const [actionType, setActionType] = useState(''); const partialDiff = useSelector((state) => state.getIn(['config', 'partialDiff'], Map({}))).toJS(); @@ -43,7 +44,7 @@ const ActionButtons = () => { isOpen={modalIsOpen} onClose={closeModal} type={actionType} - onSubmit={(force) => actionType === 'import' ? dispatch(importAllConfig(partialDiff, force, toggleNotification)) : dispatch(exportAllConfig(partialDiff, toggleNotification))} + onSubmit={(force) => actionType === 'import' ? dispatch(importAllConfig(partialDiff, force, toggleNotification, formatMessage, post, get)) : dispatch(exportAllConfig(partialDiff, toggleNotification, formatMessage, post, get))} /> ); diff --git a/admin/src/components/FirstExport/index.jsx b/admin/src/components/FirstExport/index.jsx index 4090cac..1087667 100644 --- a/admin/src/components/FirstExport/index.jsx +++ b/admin/src/components/FirstExport/index.jsx @@ -1,7 +1,7 @@ import React, { useState } from 'react'; import { useIntl } from 'react-intl'; import { useDispatch } from 'react-redux'; -import { useNotification } from '@strapi/strapi/admin'; +import { getFetchClient, useNotification } from '@strapi/strapi/admin'; import { Button, EmptyStateLayout } from '@strapi/design-system'; import { EmptyDocuments } from '@strapi/icons'; @@ -9,7 +9,8 @@ import { exportAllConfig } from '../../state/actions/Config'; import ConfirmModal from '../ConfirmModal'; const FirstExport = () => { - const toggleNotification = useNotification(); + const { post, get } = getFetchClient(); + const { toggleNotification } = useNotification(); const dispatch = useDispatch(); const [modalIsOpen, setModalIsOpen] = useState(false); const { formatMessage } = useIntl(); @@ -20,7 +21,7 @@ const FirstExport = () => { isOpen={modalIsOpen} onClose={() => setModalIsOpen(false)} type="export" - onSubmit={() => dispatch(exportAllConfig([], toggleNotification))} + onSubmit={() => dispatch(exportAllConfig([], toggleNotification, formatMessage, post, get))} /> { - const toggleNotification = useNotification(); + const { toggleNotification } = useNotification(); + const { get } = getFetchClient(); + const { formatMessage } = useIntl(); + const dispatch = useDispatch(); const isLoading = useSelector((state) => state.getIn(['config', 'isLoading'], Map({}))); const configDiff = useSelector((state) => state.getIn(['config', 'configDiff'], Map({}))); const appEnv = useSelector((state) => state.getIn(['config', 'appEnv', 'env'])); useEffect(() => { - dispatch(getAllConfigDiff(toggleNotification)); - dispatch(getAppEnv(toggleNotification)); + dispatch(getAllConfigDiff(toggleNotification, formatMessage, get)); + dispatch(getAppEnv(toggleNotification, formatMessage, get)); }, []); return ( diff --git a/admin/src/state/actions/Config.js b/admin/src/state/actions/Config.js index e60f184..ef1115e 100644 --- a/admin/src/state/actions/Config.js +++ b/admin/src/state/actions/Config.js @@ -4,19 +4,16 @@ * */ -import { useFetchClient } from '@strapi/admin/strapi-admin'; - -export function getAllConfigDiff(toggleNotification) { +export function getAllConfigDiff(toggleNotification, formatMessage, get) { return async function(dispatch) { - const { get } = useFetchClient(); dispatch(setLoadingState(true)); try { const configDiff = await get('/config-sync/diff'); dispatch(setConfigPartialDiffInState([])); - dispatch(setConfigDiffInState(configDiff)); + dispatch(setConfigDiffInState(configDiff.data)); dispatch(setLoadingState(false)); } catch (err) { - toggleNotification({ type: 'warning', message: { id: 'notification.error' } }); + toggleNotification({ type: 'warning', message: formatMessage({ id: 'notification.error' }) }); dispatch(setLoadingState(false)); } }; @@ -38,36 +35,34 @@ export function setConfigPartialDiffInState(config) { }; } -export function exportAllConfig(partialDiff, toggleNotification) { +export function exportAllConfig(partialDiff, toggleNotification, formatMessage, post, get) { return async function(dispatch) { - const { post } = useFetchClient(); dispatch(setLoadingState(true)); try { const response = await post('/config-sync/export', partialDiff); - toggleNotification({ type: 'success', response }); - dispatch(getAllConfigDiff(toggleNotification)); + toggleNotification({ type: 'success', message: response.data.message }); + dispatch(getAllConfigDiff(toggleNotification, formatMessage, get)); dispatch(setLoadingState(false)); } catch (err) { - toggleNotification({ type: 'warning', message: { id: 'notification.error' } }); + toggleNotification({ type: 'warning', message: formatMessage({ id: 'notification.error' }) }); dispatch(setLoadingState(false)); } }; } -export function importAllConfig(partialDiff, force, toggleNotification) { +export function importAllConfig(partialDiff, force, toggleNotification, formatMessage, post, get) { return async function(dispatch) { - const { post } = useFetchClient(); dispatch(setLoadingState(true)); try { const response = await post('/config-sync/import', { force, config: partialDiff, }); - toggleNotification({ type: 'success', response }); - dispatch(getAllConfigDiff(toggleNotification)); + toggleNotification({ type: 'success', message: response.data.message }); + dispatch(getAllConfigDiff(toggleNotification, formatMessage, get)); dispatch(setLoadingState(false)); } catch (err) { - toggleNotification({ type: 'warning', message: { id: 'notification.error' } }); + toggleNotification({ type: 'warning', message: formatMessage({ id: 'notification.error' }) }); dispatch(setLoadingState(false)); } }; @@ -81,14 +76,13 @@ export function setLoadingState(value) { }; } -export function getAppEnv(toggleNotification) { +export function getAppEnv(toggleNotification, formatMessage, get) { return async function(dispatch) { - const { get } = useFetchClient(); try { const envVars = await get('/config-sync/app-env'); - dispatch(setAppEnvInState(envVars)); + dispatch(setAppEnvInState(envVars.data)); } catch (err) { - toggleNotification({ type: 'warning', message: { id: 'notification.error' } }); + toggleNotification({ type: 'warning', message: formatMessage({ id: 'notification.error' }) }); } }; }