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