|
| 1 | +import * as React from 'react'; |
| 2 | +import fakeRestDataProvider from 'ra-data-fakerest'; |
| 3 | +import generateData from 'data-generator-retail'; |
| 4 | +import Admin from './Admin'; |
| 5 | +import { Resource, useIsOffline } from 'ra-core'; |
| 6 | +import { |
| 7 | + AppBar, |
| 8 | + Button, |
| 9 | + Create, |
| 10 | + DataTable, |
| 11 | + DateInput, |
| 12 | + Edit, |
| 13 | + Layout, |
| 14 | + List, |
| 15 | + NumberField, |
| 16 | + NumberInput, |
| 17 | + ReferenceField, |
| 18 | + ReferenceInput, |
| 19 | + Show, |
| 20 | + SimpleForm, |
| 21 | + SimpleShowLayout, |
| 22 | + TextField, |
| 23 | + TextInput, |
| 24 | + TitlePortal, |
| 25 | +} from 'ra-ui-materialui'; |
| 26 | +import { onlineManager, QueryClient } from '@tanstack/react-query'; |
| 27 | +import { PersistQueryClientProvider } from '@tanstack/react-query-persist-client'; |
| 28 | +import { createAsyncStoragePersister } from '@tanstack/query-async-storage-persister'; |
| 29 | + |
| 30 | +export default { |
| 31 | + title: 'react-admin/offline', |
| 32 | +}; |
| 33 | + |
| 34 | +export const FullApp = () => { |
| 35 | + const dataProvider = fakeRestDataProvider(generateData(), true, 350); |
| 36 | + |
| 37 | + const queryClient = new QueryClient({ |
| 38 | + defaultOptions: { |
| 39 | + queries: { |
| 40 | + gcTime: 1000 * 60 * 60 * 24, // 24 hours |
| 41 | + }, |
| 42 | + }, |
| 43 | + }); |
| 44 | + |
| 45 | + const asyncStoragePersister = createAsyncStoragePersister({ |
| 46 | + storage: localStorage, |
| 47 | + }); |
| 48 | + |
| 49 | + return ( |
| 50 | + <PersistQueryClientProvider |
| 51 | + client={queryClient} |
| 52 | + persistOptions={{ persister: asyncStoragePersister }} |
| 53 | + > |
| 54 | + <Admin |
| 55 | + dataProvider={dataProvider} |
| 56 | + queryClient={queryClient} |
| 57 | + layout={CustomLayout} |
| 58 | + > |
| 59 | + <Resource |
| 60 | + name="products" |
| 61 | + list={ProductList} |
| 62 | + edit={ProductEdit} |
| 63 | + create={ProductCreate} |
| 64 | + show={ProductShow} |
| 65 | + /> |
| 66 | + </Admin> |
| 67 | + </PersistQueryClientProvider> |
| 68 | + ); |
| 69 | +}; |
| 70 | + |
| 71 | +const ProductList = () => ( |
| 72 | + <List> |
| 73 | + <DataTable> |
| 74 | + <DataTable.Col source="reference" /> |
| 75 | + <DataTable.Col source="category_id"> |
| 76 | + <ReferenceField source="category_id" reference="categories" /> |
| 77 | + </DataTable.Col> |
| 78 | + </DataTable> |
| 79 | + </List> |
| 80 | +); |
| 81 | + |
| 82 | +const ProductEdit = () => ( |
| 83 | + <Edit> |
| 84 | + <ProductForm /> |
| 85 | + </Edit> |
| 86 | +); |
| 87 | + |
| 88 | +const ProductCreate = () => ( |
| 89 | + <Create |
| 90 | + mutationMode="optimistic" |
| 91 | + transform={data => ({ |
| 92 | + id: crypto.randomUUID(), |
| 93 | + ...data, |
| 94 | + })} |
| 95 | + > |
| 96 | + <ProductForm /> |
| 97 | + </Create> |
| 98 | +); |
| 99 | + |
| 100 | +const ProductForm = () => ( |
| 101 | + <SimpleForm> |
| 102 | + <TextInput source="reference" /> |
| 103 | + <ReferenceInput source="category_id" reference="categories" /> |
| 104 | + <TextInput source="description" /> |
| 105 | + <NumberInput source="width" /> |
| 106 | + <NumberInput source="height" /> |
| 107 | + <NumberInput source="price" /> |
| 108 | + <TextInput source="thumbnail" /> |
| 109 | + <TextInput source="image" /> |
| 110 | + <DateInput source="stock" /> |
| 111 | + <NumberInput source="sales" /> |
| 112 | + </SimpleForm> |
| 113 | +); |
| 114 | + |
| 115 | +const ProductShow = () => ( |
| 116 | + <Show> |
| 117 | + <SimpleShowLayout> |
| 118 | + <TextField source="id" /> |
| 119 | + <TextField source="reference" /> |
| 120 | + <ReferenceField source="category_id" reference="categories" /> |
| 121 | + <TextField source="description" /> |
| 122 | + <NumberField source="width" /> |
| 123 | + <NumberField source="height" /> |
| 124 | + <NumberField source="price" /> |
| 125 | + <TextField source="thumbnail" /> |
| 126 | + <TextField source="image" /> |
| 127 | + <NumberField source="stock" /> |
| 128 | + <NumberField source="sales" /> |
| 129 | + </SimpleShowLayout> |
| 130 | + </Show> |
| 131 | +); |
| 132 | + |
| 133 | +const CustomLayout = ({ children }) => ( |
| 134 | + <Layout appBar={CustomAppBar}>{children}</Layout> |
| 135 | +); |
| 136 | + |
| 137 | +const CustomAppBar = () => { |
| 138 | + const isOffline = useIsOffline(); |
| 139 | + return ( |
| 140 | + <AppBar color="primary"> |
| 141 | + <TitlePortal /> |
| 142 | + <Button |
| 143 | + onClick={() => { |
| 144 | + onlineManager.setOnline(isOffline); |
| 145 | + }} |
| 146 | + > |
| 147 | + {isOffline ? 'Simulate online' : 'Simulate offline'} |
| 148 | + </Button> |
| 149 | + </AppBar> |
| 150 | + ); |
| 151 | +}; |
0 commit comments