mirror of
https://github.com/strapi/strapi.git
synced 2025-07-26 02:20:32 +00:00
chore(admin): convert useRegenerate to TS
This commit is contained in:
parent
99d74cdc29
commit
e9ee4789b7
@ -3,7 +3,6 @@ export { useContentTypes } from './useContentTypes';
|
|||||||
export { default as useLicenseLimitNotification } from './useLicenseLimitNotification';
|
export { default as useLicenseLimitNotification } from './useLicenseLimitNotification';
|
||||||
export { default as useMenu } from './useMenu';
|
export { default as useMenu } from './useMenu';
|
||||||
export { default as usePermissionsDataManager } from './usePermissionsDataManager';
|
export { default as usePermissionsDataManager } from './usePermissionsDataManager';
|
||||||
export { default as useRegenerate } from './useRegenerate';
|
|
||||||
export { default as useReleaseNotification } from './useReleaseNotification';
|
export { default as useReleaseNotification } from './useReleaseNotification';
|
||||||
export { default as useSettingsForm } from './useSettingsForm';
|
export { default as useSettingsForm } from './useSettingsForm';
|
||||||
export { default as useSettingsMenu } from './useSettingsMenu';
|
export { default as useSettingsMenu } from './useSettingsMenu';
|
||||||
|
@ -6,7 +6,7 @@ import { Refresh } from '@strapi/icons';
|
|||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { useIntl } from 'react-intl';
|
import { useIntl } from 'react-intl';
|
||||||
|
|
||||||
import { useRegenerate } from '../../../../../hooks';
|
import { useRegenerate } from '../../../hooks/useRegenerate';
|
||||||
|
|
||||||
export const Regenerate = ({ onRegenerate, idToRegenerate, backUrl, onError }) => {
|
export const Regenerate = ({ onRegenerate, idToRegenerate, backUrl, onError }) => {
|
||||||
const { formatMessage } = useIntl();
|
const { formatMessage } = useIntl();
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import React from 'react';
|
/* eslint-disable check-file/filename-naming-convention */
|
||||||
|
|
||||||
import { renderHook } from '@testing-library/react';
|
import { renderHook } from '@testing-library/react';
|
||||||
import { IntlProvider } from 'react-intl';
|
import { IntlProvider } from 'react-intl';
|
||||||
|
|
||||||
import useRegenerate from '../index';
|
import { useRegenerate } from '../useRegenerate';
|
||||||
|
|
||||||
jest.mock('@strapi/helper-plugin', () => ({
|
jest.mock('@strapi/helper-plugin', () => ({
|
||||||
...jest.requireActual('@strapi/helper-plugin'),
|
...jest.requireActual('@strapi/helper-plugin'),
|
||||||
@ -18,19 +18,17 @@ jest.mock('@strapi/helper-plugin', () => ({
|
|||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
}));
|
}));
|
||||||
// eslint-disable-next-line react/prop-types
|
|
||||||
function RegenerateWrapper({ children }) {
|
|
||||||
return (
|
|
||||||
<IntlProvider messages={{}} locale="en">
|
|
||||||
{children}
|
|
||||||
</IntlProvider>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
describe('useRegenerate', () => {
|
describe('useRegenerate', () => {
|
||||||
it('returns a function to regenerate the data and a boolean', () => {
|
it('returns a function to regenerate the data and a boolean', () => {
|
||||||
const { result } = renderHook(() => useRegenerate('/test', 1, (accessKey) => accessKey), {
|
const { result } = renderHook(() => useRegenerate('/test', 1, (accessKey) => accessKey), {
|
||||||
wrapper: RegenerateWrapper,
|
wrapper({ children }) {
|
||||||
|
return (
|
||||||
|
<IntlProvider messages={{}} locale="en">
|
||||||
|
{children}
|
||||||
|
</IntlProvider>
|
||||||
|
);
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result.current).toEqual({
|
expect(result.current).toEqual({
|
@ -1,8 +1,14 @@
|
|||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
|
|
||||||
import { useAPIErrorHandler, useFetchClient, useNotification } from '@strapi/helper-plugin';
|
import { useAPIErrorHandler, useFetchClient, useNotification } from '@strapi/helper-plugin';
|
||||||
|
import { AxiosError } from 'axios';
|
||||||
|
|
||||||
const useRegenerate = (url, id, onRegenerate, onError) => {
|
export const useRegenerate = (
|
||||||
|
url: string,
|
||||||
|
id: number | string,
|
||||||
|
onRegenerate: (accessKey: string) => void,
|
||||||
|
onError?: (error: unknown) => void
|
||||||
|
): { isLoadingConfirmation: boolean; regenerateData: () => void } => {
|
||||||
const [isLoadingConfirmation, setIsLoadingConfirmation] = useState(false);
|
const [isLoadingConfirmation, setIsLoadingConfirmation] = useState(false);
|
||||||
const toggleNotification = useNotification();
|
const toggleNotification = useNotification();
|
||||||
const { post } = useFetchClient();
|
const { post } = useFetchClient();
|
||||||
@ -23,10 +29,12 @@ const useRegenerate = (url, id, onRegenerate, onError) => {
|
|||||||
if (onError) {
|
if (onError) {
|
||||||
onError(error);
|
onError(error);
|
||||||
} else {
|
} else {
|
||||||
toggleNotification({
|
if (error instanceof AxiosError) {
|
||||||
type: 'warning',
|
toggleNotification({
|
||||||
message: formatAPIError(error),
|
type: 'warning',
|
||||||
});
|
message: formatAPIError(error),
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -36,5 +44,3 @@ const useRegenerate = (url, id, onRegenerate, onError) => {
|
|||||||
isLoadingConfirmation,
|
isLoadingConfirmation,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export default useRegenerate;
|
|
@ -6,7 +6,7 @@ import { Refresh } from '@strapi/icons';
|
|||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { useIntl } from 'react-intl';
|
import { useIntl } from 'react-intl';
|
||||||
|
|
||||||
import { useRegenerate } from '../../../../../../../hooks';
|
import { useRegenerate } from '../../../../../hooks/useRegenerate';
|
||||||
|
|
||||||
export const Regenerate = ({ onRegenerate, idToRegenerate }) => {
|
export const Regenerate = ({ onRegenerate, idToRegenerate }) => {
|
||||||
const { formatMessage } = useIntl();
|
const { formatMessage } = useIntl();
|
||||||
|
@ -44,7 +44,9 @@ export interface NotificationsContextValue {
|
|||||||
toggleNotification: (config: NotificationConfig) => void;
|
toggleNotification: (config: NotificationConfig) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const NotificationsContext = React.createContext<NotificationsContextValue | null>(null);
|
const NotificationsContext = React.createContext<NotificationsContextValue>({
|
||||||
|
toggleNotification: () => {},
|
||||||
|
});
|
||||||
|
|
||||||
/* -------------------------------------------------------------------------------------------------
|
/* -------------------------------------------------------------------------------------------------
|
||||||
* Provider
|
* Provider
|
||||||
@ -255,10 +257,10 @@ const Notification = ({
|
|||||||
* import { useNotification } from '@strapi/helper-plugin';
|
* import { useNotification } from '@strapi/helper-plugin';
|
||||||
*
|
*
|
||||||
* const MyComponent = () => {
|
* const MyComponent = () => {
|
||||||
* const { toggleNotification } = useNotification();
|
* const toggleNotification = useNotification();
|
||||||
*
|
*
|
||||||
* return <button onClick={() => toggleNotification({ message: 'Hello world!' })}>Click me</button>;
|
* return <button onClick={() => toggleNotification({ message: 'Hello world!' })}>Click me</button>;
|
||||||
*/
|
*/
|
||||||
const useNotification = () => React.useContext(NotificationsContext)?.toggleNotification;
|
const useNotification = () => React.useContext(NotificationsContext).toggleNotification;
|
||||||
|
|
||||||
export { NotificationsContext, NotificationsProvider, useNotification };
|
export { NotificationsContext, NotificationsProvider, useNotification };
|
||||||
|
Loading…
x
Reference in New Issue
Block a user