mirror of
https://github.com/strapi/strapi.git
synced 2025-07-25 01:49:34 +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 useMenu } from './useMenu';
|
||||
export { default as usePermissionsDataManager } from './usePermissionsDataManager';
|
||||
export { default as useRegenerate } from './useRegenerate';
|
||||
export { default as useReleaseNotification } from './useReleaseNotification';
|
||||
export { default as useSettingsForm } from './useSettingsForm';
|
||||
export { default as useSettingsMenu } from './useSettingsMenu';
|
||||
|
@ -6,7 +6,7 @@ import { Refresh } from '@strapi/icons';
|
||||
import PropTypes from 'prop-types';
|
||||
import { useIntl } from 'react-intl';
|
||||
|
||||
import { useRegenerate } from '../../../../../hooks';
|
||||
import { useRegenerate } from '../../../hooks/useRegenerate';
|
||||
|
||||
export const Regenerate = ({ onRegenerate, idToRegenerate, backUrl, onError }) => {
|
||||
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 { IntlProvider } from 'react-intl';
|
||||
|
||||
import useRegenerate from '../index';
|
||||
import { useRegenerate } from '../useRegenerate';
|
||||
|
||||
jest.mock('@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', () => {
|
||||
it('returns a function to regenerate the data and a boolean', () => {
|
||||
const { result } = renderHook(() => useRegenerate('/test', 1, (accessKey) => accessKey), {
|
||||
wrapper: RegenerateWrapper,
|
||||
wrapper({ children }) {
|
||||
return (
|
||||
<IntlProvider messages={{}} locale="en">
|
||||
{children}
|
||||
</IntlProvider>
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.current).toEqual({
|
@ -1,8 +1,14 @@
|
||||
import { useState } from 'react';
|
||||
|
||||
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 toggleNotification = useNotification();
|
||||
const { post } = useFetchClient();
|
||||
@ -23,10 +29,12 @@ const useRegenerate = (url, id, onRegenerate, onError) => {
|
||||
if (onError) {
|
||||
onError(error);
|
||||
} else {
|
||||
toggleNotification({
|
||||
type: 'warning',
|
||||
message: formatAPIError(error),
|
||||
});
|
||||
if (error instanceof AxiosError) {
|
||||
toggleNotification({
|
||||
type: 'warning',
|
||||
message: formatAPIError(error),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -36,5 +44,3 @@ const useRegenerate = (url, id, onRegenerate, onError) => {
|
||||
isLoadingConfirmation,
|
||||
};
|
||||
};
|
||||
|
||||
export default useRegenerate;
|
@ -6,7 +6,7 @@ import { Refresh } from '@strapi/icons';
|
||||
import PropTypes from 'prop-types';
|
||||
import { useIntl } from 'react-intl';
|
||||
|
||||
import { useRegenerate } from '../../../../../../../hooks';
|
||||
import { useRegenerate } from '../../../../../hooks/useRegenerate';
|
||||
|
||||
export const Regenerate = ({ onRegenerate, idToRegenerate }) => {
|
||||
const { formatMessage } = useIntl();
|
||||
|
@ -44,7 +44,9 @@ export interface NotificationsContextValue {
|
||||
toggleNotification: (config: NotificationConfig) => void;
|
||||
}
|
||||
|
||||
const NotificationsContext = React.createContext<NotificationsContextValue | null>(null);
|
||||
const NotificationsContext = React.createContext<NotificationsContextValue>({
|
||||
toggleNotification: () => {},
|
||||
});
|
||||
|
||||
/* -------------------------------------------------------------------------------------------------
|
||||
* Provider
|
||||
@ -255,10 +257,10 @@ const Notification = ({
|
||||
* import { useNotification } from '@strapi/helper-plugin';
|
||||
*
|
||||
* const MyComponent = () => {
|
||||
* const { toggleNotification } = useNotification();
|
||||
* const toggleNotification = useNotification();
|
||||
*
|
||||
* 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 };
|
||||
|
Loading…
x
Reference in New Issue
Block a user