/
lizardking
/
react_final
Обзор
Документация
Войти
/
lizardking
/
react_final
Код
Запросы
1
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
old
src/shared/store/HOCs/WithQuery.tsx
45 строк
1 KB
LizardKing
Initial commit
22 ноя 2025, 15:56
22 ноя 2025, 15:56
0df0d9e
Код
Авторство
О чём код?
import { Alert, AlertTitle, Box, Button, CircularProgress, Container } from '@mui/material' import type { SerializedError } from '@reduxjs/toolkit' import type { FetchBaseQueryError } from '@reduxjs/toolkit/query' import type { FC, ComponentType } from 'react' import { getMessageFromError } from '../../utils' type WithQueryProps = { isLoading: boolean isError: boolean refetch?: () => void error?: FetchBaseQueryError | SerializedError | undefined } export const WithQuery = <T extends object>(WrappedComponent: ComponentType<T>) => { const ReturnedComponent: FC<WithQueryProps & T> = (props) => { const { isError, isLoading, refetch, error, ...propsForWrappedComponent } = props if (isError) { return ( <Container> <Alert action={<Button onClick={refetch}>Retry</Button>} severity="error"> <AlertTitle> {getMessageFromError(error, 'Неизвестная ошибка при получение данных')} </AlertTitle> </Alert> </Container> ) } if (isLoading) { return ( <Box sx={{ display: 'flex', justifyContent: 'center' }}> <CircularProgress /> </Box> ) } return <WrappedComponent {...(propsForWrappedComponent as T)} /> } ReturnedComponent.displayName = `withQuery${WrappedComponent.displayName}` return ReturnedComponent }