|
1 | 1 | import * as React from 'react'; |
2 | 2 | import { Admin } from 'react-admin'; |
3 | | -import { Resource, useRecordContext, TestMemoryRouter } from 'ra-core'; |
4 | | -import { Box, Card, Stack, ThemeOptions } from '@mui/material'; |
| 3 | +import { |
| 4 | + Resource, |
| 5 | + useRecordContext, |
| 6 | + TestMemoryRouter, |
| 7 | + IsOffline, |
| 8 | +} from 'ra-core'; |
| 9 | +import { Alert, Box, Card, Stack, ThemeOptions } from '@mui/material'; |
| 10 | +import { deepmerge } from '@mui/utils'; |
| 11 | +import { onlineManager } from '@tanstack/react-query'; |
5 | 12 |
|
6 | 13 | import { TextField } from '../field'; |
7 | 14 | import { Labeled } from '../Labeled'; |
8 | 15 | import { SimpleShowLayout } from './SimpleShowLayout'; |
9 | 16 | import { EditButton } from '../button'; |
10 | 17 | import TopToolbar from '../layout/TopToolbar'; |
11 | | -import { Show } from './Show'; |
12 | | -import { deepmerge } from '@mui/utils'; |
| 18 | +import { Show, ShowProps } from './Show'; |
13 | 19 | import { defaultLightTheme } from '../theme'; |
14 | 20 |
|
15 | 21 | export default { title: 'ra-ui-materialui/detail/Show' }; |
@@ -299,3 +305,67 @@ export const WithRenderProp = () => ( |
299 | 305 | </Admin> |
300 | 306 | </TestMemoryRouter> |
301 | 307 | ); |
| 308 | + |
| 309 | +export const Offline = ({ |
| 310 | + isOnline = true, |
| 311 | + offline, |
| 312 | +}: { |
| 313 | + isOnline?: boolean; |
| 314 | + offline?: React.ReactNode; |
| 315 | +}) => { |
| 316 | + React.useEffect(() => { |
| 317 | + onlineManager.setOnline(isOnline); |
| 318 | + }, [isOnline]); |
| 319 | + return ( |
| 320 | + <TestMemoryRouter initialEntries={['/books/1/show']}> |
| 321 | + <Admin dataProvider={dataProvider}> |
| 322 | + <Resource |
| 323 | + name="books" |
| 324 | + show={<BookShowOffline offline={offline} />} |
| 325 | + /> |
| 326 | + </Admin> |
| 327 | + </TestMemoryRouter> |
| 328 | + ); |
| 329 | +}; |
| 330 | + |
| 331 | +const BookShowOffline = (props: ShowProps) => { |
| 332 | + return ( |
| 333 | + <Show emptyWhileLoading {...props}> |
| 334 | + <IsOffline> |
| 335 | + <Alert severity="warning"> |
| 336 | + You are offline, the data may be outdated |
| 337 | + </Alert> |
| 338 | + </IsOffline> |
| 339 | + <SimpleShowLayout> |
| 340 | + <TextField source="title" /> |
| 341 | + <TextField source="author" /> |
| 342 | + <TextField source="summary" /> |
| 343 | + <TextField source="year" /> |
| 344 | + </SimpleShowLayout> |
| 345 | + </Show> |
| 346 | + ); |
| 347 | +}; |
| 348 | + |
| 349 | +const CustomOffline = () => { |
| 350 | + return <Alert severity="warning">You are offline!</Alert>; |
| 351 | +}; |
| 352 | + |
| 353 | +Offline.args = { |
| 354 | + isOnline: true, |
| 355 | + offline: false, |
| 356 | +}; |
| 357 | + |
| 358 | +Offline.argTypes = { |
| 359 | + isOnline: { |
| 360 | + control: { type: 'boolean' }, |
| 361 | + }, |
| 362 | + offline: { |
| 363 | + name: 'Offline component', |
| 364 | + control: { type: 'radio' }, |
| 365 | + options: ['default', 'custom'], |
| 366 | + mapping: { |
| 367 | + default: undefined, |
| 368 | + custom: <CustomOffline />, |
| 369 | + }, |
| 370 | + }, |
| 371 | +}; |
0 commit comments