/
Percival
/
react_final_project
Обзор
Документация
Войти
/
Percival
/
react_final_project
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/app/store/HOCs/WithQuery.tsx
59 строк
1 KB
Rituparn
Added code
23 ноя 2025, 19:02
23 ноя 2025, 19:02
2aec7c7
Код
Авторство
О чём код?
import { Alert, AlertTitle, Box, Button, CircularProgress, Container, } from '@mui/material'; import { FC, ComponentType } from 'react'; import { getMessageFromError } from '@/src/shared/utils'; import { SerializedError } from '@reduxjs/toolkit'; import { FetchBaseQueryError } from '@reduxjs/toolkit/query'; interface 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; };