-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathAdmin.stories.tsx
315 lines (300 loc) · 10.1 KB
/
Admin.stories.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import * as React from 'react';
import { Routes, Route, Link } from 'react-router-dom';
import { Resource, testDataProvider, TestMemoryRouter } from 'ra-core';
import type { AuthProvider } from 'ra-core';
import {
Layout,
ListGuesser,
EditGuesser,
ShowGuesser,
} from 'ra-ui-materialui';
import { Box, Typography, Button } from '@mui/material';
import fakeRestDataProvider from 'ra-data-fakerest';
import { useQueryClient, QueryClient } from '@tanstack/react-query';
import { Admin, AdminProps } from './Admin';
export default {
title: 'react-admin/Admin',
};
const PostList = () => <h1>Post List</h1>;
const CommentList = () => <h1>Comment List</h1>;
export const Basic = () => (
<Admin dataProvider={testDataProvider()}>
<Resource name="posts" list={PostList} />
<Resource name="comments" list={CommentList} />
</Admin>
);
export const InsideRouter = () => (
<TestMemoryRouter>
<Admin dataProvider={testDataProvider()}>
<Resource name="posts" list={PostList} />
<Resource name="comments" list={CommentList} />
</Admin>
</TestMemoryRouter>
);
export const SubPath = () => (
<TestMemoryRouter>
<Routes>
<Route
path="/"
element={
<>
<h1>Main</h1>
<div>
<Link to="/admin">Go to admin</Link>
</div>
</>
}
/>
<Route
path="/admin/*"
element={
<Admin dataProvider={testDataProvider()} basename="/admin">
<Resource name="posts" list={PostList} />
<Resource name="comments" list={CommentList} />
</Admin>
}
/>
</Routes>
</TestMemoryRouter>
);
// @ts-ignore
const FailingAppBar = () => {
throw new Error('AppBar rendering failed');
};
const FailedLayout = props => <Layout {...props} appBar={FailingAppBar} />;
export const DefaultError = () => (
<Admin layout={FailedLayout}>
<Resource name="posts" list={PostList} />
</Admin>
);
const ErrorPage = ({ errorInfo }: { errorInfo?: React.ErrorInfo }) => (
<Box
sx={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
minHeight: '100vh',
backgroundColor: '#f44336',
}}
>
<Typography variant="h1" style={{ color: 'white' }}>
<b>Error</b>
</Typography>
<ul>
{errorInfo?.componentStack
?.split(' at ')
?.slice(1)
?.map((line, index) => <li key={index}>At {line}</li>)}
</ul>
</Box>
);
export const CustomError = () => (
<Admin layout={FailedLayout} error={ErrorPage}>
<Resource name="posts" list={PostList} />
</Admin>
);
const dataProvider = fakeRestDataProvider({
books: [
{ id: 1, title: 'War and Peace', author_id: 1 },
{ id: 2, title: 'Pride and Prejudice', author_id: 2 },
{ id: 3, title: 'The Picture of Dorian Gray', author_id: 3 },
],
authors: [
{ id: 1, firstName: 'Leo', lastName: 'Tolstoy' },
{ id: 2, firstName: 'Jane', lastName: 'Austen' },
{ id: 3, firstName: 'Oscar', lastName: 'Wilde' },
],
users: [
{ id: 1, fullName: 'John Appleseed' },
{ id: 2, fullName: 'Jane Doe' },
],
});
export const AccessControl = () => <AccessControlAdmin />;
export const AccessControlInSubPath = () => (
<TestMemoryRouter>
<Routes>
<Route
path="/"
element={
<>
<h1>Main</h1>
<div>
<Link to="/admin">Go to admin</Link>
</div>
</>
}
/>
<Route
path="/admin/*"
element={
<AccessControlAdmin AdminProps={{ basename: '/admin' }} />
}
/>
</Routes>
</TestMemoryRouter>
);
const AccessControlAdmin = ({ AdminProps }: { AdminProps?: AdminProps }) => {
const readerPermissions = [
{ action: 'list', resource: 'books' },
{ action: 'show', resource: 'books' },
{ action: 'list', resource: 'authors' },
{ action: 'show', resource: 'authors' },
];
const editorPermissions = [
{ action: 'list', resource: 'books' },
{ action: 'create', resource: 'books' },
{ action: 'edit', resource: 'books' },
{ action: 'delete', resource: 'books' },
{ action: 'list', resource: 'authors' },
{ action: 'create', resource: 'authors' },
{ action: 'edit', resource: 'authors' },
{ action: 'delete', resource: 'authors' },
];
const adminPermissions = [
...editorPermissions,
{ action: 'list', resource: 'users' },
{ action: 'show', resource: 'users' },
{ action: 'create', resource: 'users' },
{ action: 'edit', resource: 'users' },
{ action: 'delete', resource: 'users' },
];
const [permissions, setPermissions] = React.useState(readerPermissions);
const [triggerAccessControlError, setTriggerAccessControlError] =
React.useState(false);
const authProvider: AuthProvider = {
// authentication
async login() {},
async checkError() {},
async checkAuth() {},
async logout() {},
async getIdentity() {
return { id: 'user', fullName: 'John Doe' };
},
async handleCallback() {}, // for third-party authentication only
// authorization (optional)
async canAccess({ resource, action }) {
if (triggerAccessControlError) {
throw new Error('Access control error');
}
return permissions.some(
p => p.resource === resource && p.action === action
);
},
async getPermissions() {},
};
const CustomLayout = ({ children }) => {
const queryClient = useQueryClient();
return (
<div>
<Box
display="flex"
gap={2}
position="absolute"
bottom={10}
left="50%"
zIndex={1000}
sx={{ transform: 'translate(-50%, 0)' }}
>
<Button
variant="outlined"
size="small"
onClick={() => {
setPermissions(readerPermissions);
queryClient.invalidateQueries({
queryKey: ['auth', 'canAccess'],
});
}}
disabled={
permissions.length === readerPermissions.length
}
>
View as reader
</Button>
<Button
variant="outlined"
size="small"
onClick={() => {
setPermissions(editorPermissions);
queryClient.invalidateQueries({
queryKey: ['auth', 'canAccess'],
});
}}
disabled={
permissions.length === editorPermissions.length
}
>
View as editor
</Button>
<Button
variant="outlined"
size="small"
onClick={() => {
setPermissions(adminPermissions);
queryClient.invalidateQueries({
queryKey: ['auth', 'canAccess'],
});
}}
disabled={
permissions.length === adminPermissions.length
}
>
View as admin
</Button>
<Button
variant="outlined"
size="small"
onClick={() => {
setTriggerAccessControlError(prev => !prev);
queryClient.invalidateQueries({
queryKey: ['auth', 'canAccess'],
});
}}
>
Toggle Access Control Error
</Button>
</Box>
<Layout>{children}</Layout>
</div>
);
};
return (
<Admin
dataProvider={dataProvider}
authProvider={authProvider}
layout={CustomLayout}
queryClient={
new QueryClient({
defaultOptions: {
queries: { retry: false },
},
})
}
{...AdminProps}
>
<Resource
name="books"
list={ListGuesser}
edit={EditGuesser}
show={ShowGuesser}
create={<>Create view</>}
/>
<Resource
name="authors"
list={ListGuesser}
edit={EditGuesser}
show={ShowGuesser}
create={<>Create view</>}
recordRepresentation={record =>
`${record.firstName} ${record.lastName}`
}
/>
<Resource
name="users"
list={ListGuesser}
edit={EditGuesser}
show={ShowGuesser}
create={<>Create view</>}
/>
</Admin>
);
};