Merge pull request #18536 from strapi/chore/useContentTypes-ts

chore(admin): convert useContentTypes to TS
This commit is contained in:
Gustav Hansen 2023-10-23 14:05:29 +02:00 committed by GitHub
commit 24d524e6ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 18 deletions

View File

@ -1,3 +1,2 @@
export { useContentTypes } from './useContentTypes';
export { default as useSettingsForm } from './useSettingsForm';
export { default as useSettingsMenu } from './useSettingsMenu';

View File

@ -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 () => {

View File

@ -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],
const queries = useQueries([
{
queryKey: ['content-manager', 'components'],
async queryFn() {
const {
data: { data },
} = await get(`/content-manager/${type}`);
} = await get<APIResponse<Component[]>>(`/content-manager/components`);
return data;
},
onError(error) {
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;

View File

@ -1 +0,0 @@
export * from './useContentTypes';

View 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;
}