|
| 1 | +import * as React from 'react'; |
| 2 | +import { useState } from 'react'; |
| 3 | +import { QueryClient, useIsMutating } from '@tanstack/react-query'; |
| 4 | + |
| 5 | +import { CoreAdminContext } from '../core'; |
| 6 | +import { useGetList } from './useGetList'; |
| 7 | +import { MutationMode } from '../types'; |
| 8 | +import { useDeleteMany } from './useDeleteMany'; |
| 9 | + |
| 10 | +export default { title: 'ra-core/dataProvider/useDeleteMany' }; |
| 11 | + |
| 12 | +export const Basic = ({ timeout = 1000 }: { timeout?: number }) => { |
| 13 | + const posts = [ |
| 14 | + { id: 1, title: 'Hello World 1' }, |
| 15 | + { id: 2, title: 'Hello World 2' }, |
| 16 | + { id: 3, title: 'Hello World 3' }, |
| 17 | + { id: 4, title: 'Hello World 4' }, |
| 18 | + ]; |
| 19 | + const dataProvider = { |
| 20 | + getList: (resource, params) => { |
| 21 | + console.log('getList', resource, params); |
| 22 | + return Promise.resolve({ |
| 23 | + data: posts, |
| 24 | + total: posts.length, |
| 25 | + }); |
| 26 | + }, |
| 27 | + deleteMany: (resource, params) => { |
| 28 | + console.log('deleteMany', resource, params); |
| 29 | + return new Promise(resolve => { |
| 30 | + setTimeout(() => { |
| 31 | + for (const id of params.ids) { |
| 32 | + const index = posts.findIndex(p => p.id === id); |
| 33 | + posts.splice(index, 1); |
| 34 | + } |
| 35 | + resolve({ data: params.ids }); |
| 36 | + }, timeout); |
| 37 | + }); |
| 38 | + }, |
| 39 | + } as any; |
| 40 | + return ( |
| 41 | + <CoreAdminContext |
| 42 | + queryClient={new QueryClient()} |
| 43 | + dataProvider={dataProvider} |
| 44 | + > |
| 45 | + <SuccessCore /> |
| 46 | + </CoreAdminContext> |
| 47 | + ); |
| 48 | +}; |
| 49 | + |
| 50 | +const SuccessCore = () => { |
| 51 | + const isMutating = useIsMutating(); |
| 52 | + const [success, setSuccess] = useState<string>(); |
| 53 | + const { data, refetch } = useGetList('posts'); |
| 54 | + const [id, setId] = useState<number>(1); |
| 55 | + const [mutationMode, setMutationMode] = |
| 56 | + useState<MutationMode>('pessimistic'); |
| 57 | + const [deleteMany, { isPending }] = useDeleteMany('posts', undefined, { |
| 58 | + mutationMode, |
| 59 | + onSuccess: () => setSuccess('success'), |
| 60 | + }); |
| 61 | + const handleClick = () => { |
| 62 | + deleteMany('posts', { |
| 63 | + ids: [id, id + 1], |
| 64 | + }); |
| 65 | + }; |
| 66 | + return ( |
| 67 | + <> |
| 68 | + <ul>{data?.map(post => <li key={post.id}>{post.title}</li>)}</ul> |
| 69 | + <div> |
| 70 | + <button onClick={handleClick} disabled={isPending}> |
| 71 | + Delete posts |
| 72 | + </button> |
| 73 | + |
| 74 | + <button onClick={() => refetch()}>Refetch</button> |
| 75 | + <button onClick={() => setId(prev => prev + 2)}> |
| 76 | + Increment id |
| 77 | + </button> |
| 78 | + <button onClick={() => setMutationMode('pessimistic')}> |
| 79 | + pessimistic |
| 80 | + </button> |
| 81 | + <button onClick={() => setMutationMode('optimistic')}> |
| 82 | + optimistic |
| 83 | + </button> |
| 84 | + <button onClick={() => setMutationMode('undoable')}> |
| 85 | + undoable |
| 86 | + </button> |
| 87 | + </div> |
| 88 | + {success && <div>{success}</div>} |
| 89 | + {isMutating !== 0 && <div>mutating</div>} |
| 90 | + </> |
| 91 | + ); |
| 92 | +}; |
0 commit comments