82 lines
2.3 KiB
JavaScript
Raw Normal View History

2023-07-19 11:07:32 +01:00
import { useEffect } from 'react';
import { useAPIErrorHandler, useNotification, useFetchClient } from '@strapi/strapi/admin';
import { useIntl } from 'react-intl';
2023-07-19 11:07:32 +01:00
import { useMutation, useQuery } from 'react-query';
import pluginId from '../pluginId';
import getTrad from '../utils/getTrad';
export const useDocumentation = () => {
const { toggleNotification } = useNotification();
const { formatMessage } = useIntl();
2023-07-19 11:07:32 +01:00
const { del, post, put, get } = useFetchClient();
const { formatAPIError } = useAPIErrorHandler();
const { isLoading, isError, data, refetch, error } = useQuery(
['get-documentation', pluginId],
async () => {
const { data } = await get(`/${pluginId}/getInfos`);
return data;
}
);
useEffect(() => {
if (isError && error) {
toggleNotification({
type: 'danger',
message: error
? formatAPIError(error)
: formatMessage({ id: 'notification.error', defaultMessage: 'An error occurred' }),
2023-07-19 11:07:32 +01:00
});
}
}, [isError, error, toggleNotification, formatAPIError, formatMessage]);
2023-07-19 11:07:32 +01:00
const handleError = (err) => {
toggleNotification({
type: 'danger',
2023-07-19 11:07:32 +01:00
message: formatAPIError(err),
});
};
const handleSuccess = (type, tradId, defaultMessage) => {
refetch();
toggleNotification({
type,
message: formatMessage({ id: getTrad(tradId), defaultMessage }),
2023-07-19 11:07:32 +01:00
});
};
const deleteMutation = useMutation(
({ prefix, version }) => del(`${prefix}/deleteDoc/${version}`),
{
onSuccess: () =>
handleSuccess('info', 'notification.delete.success', 'Successfully deleted documentation'),
onError: handleError,
}
);
const submit = useMutation(({ prefix, body }) => put(`${prefix}/updateSettings`, body), {
onSuccess: () =>
handleSuccess('success', 'notification.update.success', 'Successfully updated settings'),
onError: handleError,
});
const regenerate = useMutation(
({ prefix, version }) => post(`${prefix}/regenerateDoc`, { version }),
{
onSuccess: () =>
handleSuccess(
'info',
'notification.generate.success',
'Successfully generated documentation'
),
onError: handleError,
}
);
return { data, isLoading, isError, remove: deleteMutation, submit, regenerate };
};