|
1 | 1 | import * as React from 'react'; |
2 | 2 | import { QueryClient, useIsMutating } from '@tanstack/react-query'; |
3 | 3 |
|
4 | | -import { CoreAdminContext } from '../core'; |
| 4 | +import { CoreAdmin, CoreAdminContext, Resource } from '../core'; |
5 | 5 | import { useCreate } from './useCreate'; |
6 | 6 | import { useGetOne } from './useGetOne'; |
7 | 7 | import type { MutationMode as MutationModeType } from '../types'; |
| 8 | +import { |
| 9 | + CreateBase, |
| 10 | + ListBase, |
| 11 | + RecordsIterator, |
| 12 | + useRegisterMutationMiddleware, |
| 13 | +} from '../controller'; |
| 14 | +import { useNotificationContext } from '../notification'; |
| 15 | +import { useTakeUndoableMutation } from './undo'; |
| 16 | +import { Form, InputProps, useInput } from '../form'; |
| 17 | +import { TestMemoryRouter } from '../routing'; |
| 18 | +import { Link } from 'react-router-dom'; |
| 19 | +import { testDataProvider } from './testDataProvider'; |
| 20 | +import { useRefresh } from './useRefresh'; |
8 | 21 |
|
9 | 22 | export default { title: 'ra-core/dataProvider/useCreate' }; |
10 | 23 |
|
@@ -208,3 +221,166 @@ const ParamsCore = () => { |
208 | 221 | </> |
209 | 222 | ); |
210 | 223 | }; |
| 224 | + |
| 225 | +export const Middleware = ({ |
| 226 | + middleware = (resource: string, params: any) => { |
| 227 | + console.log( |
| 228 | + `Creating resource ${resource} with params:`, |
| 229 | + JSON.stringify(params) |
| 230 | + ); |
| 231 | + }, |
| 232 | + mutationMode = 'undoable', |
| 233 | + timeout = 1000, |
| 234 | +}: { |
| 235 | + mutationMode?: MutationModeType; |
| 236 | + timeout?: number; |
| 237 | + middleware?: (resource: string, params: any) => void; |
| 238 | +}) => { |
| 239 | + const posts = [{ id: 1, title: 'Hello', author: 'John Doe' }]; |
| 240 | + const dataProvider = testDataProvider({ |
| 241 | + // @ts-ignore |
| 242 | + getList: (resource, params) => { |
| 243 | + return Promise.resolve({ |
| 244 | + data: posts, |
| 245 | + total: posts.length, |
| 246 | + }); |
| 247 | + }, |
| 248 | + create: (resource, params) => { |
| 249 | + return new Promise(resolve => { |
| 250 | + setTimeout(() => { |
| 251 | + const post = { id: posts.length + 1, ...params.data }; |
| 252 | + // @ts-ignore |
| 253 | + posts.push(post); |
| 254 | + // @ts-ignore |
| 255 | + resolve({ data: post }); |
| 256 | + }, timeout); |
| 257 | + }); |
| 258 | + }, |
| 259 | + }); |
| 260 | + return ( |
| 261 | + <TestMemoryRouter initialEntries={['/posts/create']}> |
| 262 | + <CoreAdmin |
| 263 | + queryClient={new QueryClient()} |
| 264 | + dataProvider={dataProvider} |
| 265 | + layout={({ children }) => ( |
| 266 | + <> |
| 267 | + {children} |
| 268 | + <Notification /> |
| 269 | + </> |
| 270 | + )} |
| 271 | + > |
| 272 | + <Resource |
| 273 | + name="posts" |
| 274 | + list={ |
| 275 | + <ListBase> |
| 276 | + <ul> |
| 277 | + <RecordsIterator |
| 278 | + render={record => <li>{record.title}</li>} |
| 279 | + /> |
| 280 | + </ul> |
| 281 | + <RefreshButton /> |
| 282 | + </ListBase> |
| 283 | + } |
| 284 | + create={ |
| 285 | + <CreateBase |
| 286 | + mutationMode={mutationMode} |
| 287 | + redirect="list" |
| 288 | + transform={data => ({ |
| 289 | + id: |
| 290 | + mutationMode === 'pessimistic' |
| 291 | + ? undefined |
| 292 | + : posts.length + 1, |
| 293 | + ...data, |
| 294 | + })} |
| 295 | + > |
| 296 | + <Form> |
| 297 | + <TextInput source="title" /> |
| 298 | + <CreateMiddleware middleware={middleware} /> |
| 299 | + <button type="submit">Save</button> |
| 300 | + </Form> |
| 301 | + </CreateBase> |
| 302 | + } |
| 303 | + /> |
| 304 | + </CoreAdmin> |
| 305 | + </TestMemoryRouter> |
| 306 | + ); |
| 307 | +}; |
| 308 | + |
| 309 | +Middleware.args = { |
| 310 | + timeout: 1000, |
| 311 | + mutationMode: 'optimistic', |
| 312 | +}; |
| 313 | + |
| 314 | +Middleware.argTypes = { |
| 315 | + timeout: { |
| 316 | + control: { |
| 317 | + type: 'number', |
| 318 | + }, |
| 319 | + }, |
| 320 | + mutationMode: { |
| 321 | + control: { |
| 322 | + type: 'select', |
| 323 | + }, |
| 324 | + options: ['pessimistic', 'optimistic', 'undoable'], |
| 325 | + }, |
| 326 | +}; |
| 327 | + |
| 328 | +const CreateMiddleware = ({ |
| 329 | + middleware, |
| 330 | +}: { |
| 331 | + middleware: (resource: string, params: any) => void; |
| 332 | +}) => { |
| 333 | + useRegisterMutationMiddleware((resource, params, next) => { |
| 334 | + middleware(resource, params); |
| 335 | + return next(resource, params); |
| 336 | + }); |
| 337 | + |
| 338 | + return null; |
| 339 | +}; |
| 340 | + |
| 341 | +const Notification = () => { |
| 342 | + const { notifications, resetNotifications } = useNotificationContext(); |
| 343 | + const takeMutation = useTakeUndoableMutation(); |
| 344 | + |
| 345 | + return notifications.length > 0 ? ( |
| 346 | + <> |
| 347 | + <div>{notifications[0].message}</div> |
| 348 | + <div style={{ display: 'flex', gap: '16px' }}> |
| 349 | + <button |
| 350 | + onClick={() => { |
| 351 | + if (notifications[0].notificationOptions.undoable) { |
| 352 | + const mutation = takeMutation(); |
| 353 | + if (mutation) { |
| 354 | + mutation({ isUndo: false }); |
| 355 | + } |
| 356 | + } |
| 357 | + resetNotifications(); |
| 358 | + }} |
| 359 | + > |
| 360 | + Close |
| 361 | + </button> |
| 362 | + </div> |
| 363 | + </> |
| 364 | + ) : null; |
| 365 | +}; |
| 366 | + |
| 367 | +const TextInput = (props: InputProps) => { |
| 368 | + const { field, id } = useInput(props); |
| 369 | + |
| 370 | + return ( |
| 371 | + <div style={{ display: 'flex', flexDirection: 'column', gap: '5px' }}> |
| 372 | + <label htmlFor={id}>{props.label || field.name}</label> |
| 373 | + <input id={id} {...field} /> |
| 374 | + </div> |
| 375 | + ); |
| 376 | +}; |
| 377 | + |
| 378 | +const RefreshButton = () => { |
| 379 | + const refresh = useRefresh(); |
| 380 | + |
| 381 | + return ( |
| 382 | + <button type="button" onClick={() => refresh()}> |
| 383 | + Refresh |
| 384 | + </button> |
| 385 | + ); |
| 386 | +}; |
0 commit comments