mirror of
https://github.com/strapi/strapi.git
synced 2025-09-25 08:19:07 +00:00
Merge pull request #18536 from strapi/chore/useContentTypes-ts
chore(admin): convert useContentTypes to TS
This commit is contained in:
commit
24d524e6ae
@ -1,3 +1,2 @@
|
||||
export { useContentTypes } from './useContentTypes';
|
||||
export { default as useSettingsForm } from './useSettingsForm';
|
||||
export { default as useSettingsMenu } from './useSettingsMenu';
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { renderHook, waitFor } from '@tests/utils';
|
||||
|
||||
import { useContentTypes } from '../index';
|
||||
import { useContentTypes } from '../useContentTypes';
|
||||
|
||||
describe('useContentTypes', () => {
|
||||
test('fetches models and content-types', async () => {
|
@ -1,32 +1,55 @@
|
||||
import * as React from 'react';
|
||||
|
||||
import { useAPIErrorHandler, useFetchClient, useNotification } from '@strapi/helper-plugin';
|
||||
import { AxiosError } from 'axios';
|
||||
import { useQueries } from 'react-query';
|
||||
|
||||
import { Component, ContentType } from '../../../shared/schema';
|
||||
import { APIResponse } from '../types/adminAPI';
|
||||
|
||||
export function useContentTypes() {
|
||||
const { get } = useFetchClient();
|
||||
const { formatAPIError } = useAPIErrorHandler();
|
||||
const toggleNotification = useNotification();
|
||||
const queries = useQueries(
|
||||
['components', 'content-types'].map((type) => {
|
||||
return {
|
||||
queryKey: ['content-manager', type],
|
||||
async queryFn() {
|
||||
const {
|
||||
data: { data },
|
||||
} = await get(`/content-manager/${type}`);
|
||||
const queries = useQueries([
|
||||
{
|
||||
queryKey: ['content-manager', 'components'],
|
||||
async queryFn() {
|
||||
const {
|
||||
data: { data },
|
||||
} = await get<APIResponse<Component[]>>(`/content-manager/components`);
|
||||
|
||||
return data;
|
||||
},
|
||||
onError(error) {
|
||||
return data;
|
||||
},
|
||||
onError(error: unknown) {
|
||||
if (error instanceof AxiosError) {
|
||||
toggleNotification({
|
||||
type: 'warning',
|
||||
message: formatAPIError(error),
|
||||
});
|
||||
},
|
||||
};
|
||||
})
|
||||
);
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
queryKey: ['content-manager', 'content-types'],
|
||||
async queryFn() {
|
||||
const {
|
||||
data: { data },
|
||||
} = await get<APIResponse<ContentType[]>>(`/content-manager/content-types`);
|
||||
|
||||
return data;
|
||||
},
|
||||
onError(error: unknown) {
|
||||
if (error instanceof AxiosError) {
|
||||
toggleNotification({
|
||||
type: 'warning',
|
||||
message: formatAPIError(error),
|
||||
});
|
||||
}
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
const [components, contentTypes] = queries;
|
||||
const isLoading = components.isLoading || contentTypes.isLoading;
|
@ -1 +0,0 @@
|
||||
export * from './useContentTypes';
|
9
packages/core/admin/shared/schema.ts
Normal file
9
packages/core/admin/shared/schema.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { Schema } from '@strapi/types';
|
||||
|
||||
export interface ContentType extends Schema.ContentType {
|
||||
isDisplayed: boolean;
|
||||
}
|
||||
|
||||
export interface Component extends Schema.Component {
|
||||
isDisplayed: boolean;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user