Skip to content

Commit ed7634d

Browse files
committed
Improve offline detection in Edit, Show, ReferenceField and ReferenceOneField
1 parent 6540e3b commit ed7634d

16 files changed

+69
-23
lines changed

packages/ra-core/src/controller/edit/EditBase.spec.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@ describe('EditBase', () => {
422422
screen.queryByText('You are offline, cannot load data')
423423
).toBeNull();
424424
rerender(<Offline isOnline={false} />);
425+
// Ensure the data is still displayed when going offline after it was loaded
425426
await screen.findByText('You are offline, the data may be outdated');
427+
await screen.findByText('Hello');
426428
});
427429
});

packages/ra-core/src/controller/edit/EditBase.stories.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,11 @@ export const Offline = ({
198198
{...defaultProps}
199199
{...props}
200200
mutationMode="pessimistic"
201-
offline={<p>You are offline, cannot load data</p>}
201+
offline={
202+
<p style={{ color: 'orange' }}>
203+
You are offline, cannot load data
204+
</p>
205+
}
202206
>
203207
<OfflineChild />
204208
</EditBase>

packages/ra-core/src/controller/edit/EditBase.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const EditBase = <RecordType extends RaRecord = any, ErrorType = Error>({
6161
);
6262
}
6363

64-
const { isPaused, record } = controllerProps;
64+
const { isPaused, isPending } = controllerProps;
6565

6666
const shouldRenderLoading =
6767
isAuthPending &&
@@ -70,7 +70,7 @@ export const EditBase = <RecordType extends RaRecord = any, ErrorType = Error>({
7070
loading !== undefined;
7171

7272
const shouldRenderOffline =
73-
isPaused && !record && offline !== false && offline !== undefined;
73+
isPaused && isPending && offline !== false && offline !== undefined;
7474

7575
return (
7676
// We pass props.resource here as we don't need to create a new ResourceContext if the props is not provided

packages/ra-core/src/controller/field/ReferenceFieldBase.spec.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,9 @@ describe('<ReferenceFieldBase />', () => {
175175
await screen.findByText('You are offline, cannot load data');
176176
fireEvent.click(await screen.findByText('Simulate online'));
177177
await screen.findByText('Leo');
178+
fireEvent.click(await screen.findByText('Simulate offline'));
179+
// Ensure the data is still displayed when going offline after it was loaded
180+
await screen.findByText('You are offline, the data may be outdated');
181+
await screen.findByText('Leo');
178182
});
179183
});

packages/ra-core/src/controller/field/ReferenceFieldBase.stories.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { useFieldValue } from '../../util/useFieldValue';
1010
import { useReferenceFieldContext } from './ReferenceFieldContext';
1111
import { DataProvider } from '../../types';
1212
import { useIsOffline } from '../../core/useIsOffline';
13+
import { IsOffline } from '../..';
1314

1415
export default {
1516
title: 'ra-core/controller/field/ReferenceFieldBase',
@@ -422,13 +423,19 @@ export const Offline = () => {
422423
source="author"
423424
reference="authors"
424425
offline={
425-
<p>
426+
<p style={{ color: 'orange' }}>
426427
You are offline, cannot load
427428
data
428429
</p>
429430
}
430431
>
431432
<MyReferenceField>
433+
<IsOffline>
434+
<p style={{ color: 'orange' }}>
435+
You are offline, the data
436+
may be outdated
437+
</p>
438+
</IsOffline>
432439
<TextField source="first_name" />
433440
</MyReferenceField>
434441
</ReferenceFieldBase>

packages/ra-core/src/controller/field/ReferenceFieldBase.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ export const ReferenceFieldBase = <
7272
loading !== false &&
7373
loading !== undefined;
7474
const shouldRenderOffline =
75-
isPaused &&
76-
!referenceRecord &&
77-
offline !== false &&
78-
offline !== undefined;
75+
isPaused && isPending && offline !== false && offline !== undefined;
7976
const shouldRenderError =
8077
!!controllerError && error !== false && error !== undefined;
8178
const shouldRenderEmpty =

packages/ra-core/src/controller/field/ReferenceOneFieldBase.spec.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ describe('ReferenceOneFieldBase', () => {
6767
render(<Offline />);
6868
fireEvent.click(await screen.findByText('Simulate offline'));
6969
fireEvent.click(await screen.findByText('Toggle Child'));
70-
await screen.findByText('Offline');
70+
await screen.findByText('You are offline, cannot load data');
7171
fireEvent.click(await screen.findByText('Simulate online'));
7272
await screen.findByText('9780393966473');
73+
fireEvent.click(await screen.findByText('Simulate offline'));
74+
// Ensure the data is still displayed when going offline after it was loaded
75+
await screen.findByText('You are offline, the data may be outdated');
76+
await screen.findByText('9780393966473');
7377
});
7478
});

packages/ra-core/src/controller/field/ReferenceOneFieldBase.stories.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react';
22

33
import {
44
CoreAdminContext,
5+
IsOffline,
56
RecordContextProvider,
67
ReferenceOneFieldBase,
78
ResourceContextProvider,
@@ -111,8 +112,17 @@ export const Offline = () => {
111112
<ReferenceOneFieldBase
112113
reference="book_details"
113114
target="book_id"
114-
offline={<span>Offline</span>}
115+
offline={
116+
<span style={{ color: 'orange' }}>
117+
You are offline, cannot load data
118+
</span>
119+
}
115120
>
121+
<IsOffline>
122+
<p style={{ color: 'orange' }}>
123+
You are offline, the data may be outdated
124+
</p>
125+
</IsOffline>
116126
<BookDetails />
117127
</ReferenceOneFieldBase>
118128
</RenderChildOnDemand>

packages/ra-core/src/controller/field/ReferenceOneFieldBase.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,7 @@ export const ReferenceOneFieldBase = <
8989
const shouldRenderLoading =
9090
!isPaused && isPending && loading !== false && loading !== undefined;
9191
const shouldRenderOffline =
92-
isPaused &&
93-
!referenceRecord &&
94-
offline !== false &&
95-
offline !== undefined;
92+
isPaused && isPending && offline !== false && offline !== undefined;
9693
const shouldRenderError =
9794
!!controllerError && error !== false && error !== undefined;
9895
const shouldRenderEmpty =

packages/ra-core/src/controller/show/ShowBase.spec.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,8 @@ describe('ShowBase', () => {
130130
).toBeNull();
131131
rerender(<Offline isOnline={false} />);
132132
await screen.findByText('You are offline, the data may be outdated');
133+
// Ensure the data is still displayed when going offline after it was loaded
134+
await screen.findByText('You are offline, the data may be outdated');
135+
await screen.findByText('Hello');
133136
});
134137
});

0 commit comments

Comments
 (0)