|
| 1 | +import * as React from 'react'; |
| 2 | +import { QueryClient, useIsMutating } from '@tanstack/react-query'; |
| 3 | + |
| 4 | +import { CoreAdminContext } from '../core'; |
| 5 | +import { useCreate } from './useCreate'; |
| 6 | +import { useGetOne } from './useGetOne'; |
| 7 | +import type { MutationMode as MutationModeType } from '../types'; |
| 8 | + |
| 9 | +export default { title: 'ra-core/dataProvider/useCreate' }; |
| 10 | + |
| 11 | +export const MutationMode = ({ timeout = 1000 }) => { |
| 12 | + const posts = [{ id: 1, title: 'Hello', author: 'John Doe' }]; |
| 13 | + const dataProvider = { |
| 14 | + getOne: (resource, params) => { |
| 15 | + return new Promise((resolve, reject) => { |
| 16 | + const data = posts.find(p => p.id === params.id); |
| 17 | + setTimeout(() => { |
| 18 | + if (!data) { |
| 19 | + reject(new Error('nothing yet')); |
| 20 | + } |
| 21 | + resolve({ data }); |
| 22 | + }, timeout); |
| 23 | + }); |
| 24 | + }, |
| 25 | + create: (resource, params) => { |
| 26 | + return new Promise(resolve => { |
| 27 | + setTimeout(() => { |
| 28 | + posts.push(params.data); |
| 29 | + resolve({ data: params.data }); |
| 30 | + }, timeout); |
| 31 | + }); |
| 32 | + }, |
| 33 | + } as any; |
| 34 | + return ( |
| 35 | + <CoreAdminContext |
| 36 | + queryClient={ |
| 37 | + new QueryClient({ |
| 38 | + defaultOptions: { |
| 39 | + queries: { retry: false }, |
| 40 | + mutations: { retry: false }, |
| 41 | + }, |
| 42 | + }) |
| 43 | + } |
| 44 | + dataProvider={dataProvider} |
| 45 | + > |
| 46 | + <MutationModeCore /> |
| 47 | + </CoreAdminContext> |
| 48 | + ); |
| 49 | +}; |
| 50 | + |
| 51 | +const MutationModeCore = () => { |
| 52 | + const isMutating = useIsMutating(); |
| 53 | + const [success, setSuccess] = React.useState<string>(); |
| 54 | + const [mutationMode, setMutationMode] = |
| 55 | + React.useState<MutationModeType>('pessimistic'); |
| 56 | + |
| 57 | + const { |
| 58 | + isPending: isPendingGetOne, |
| 59 | + data, |
| 60 | + error, |
| 61 | + refetch, |
| 62 | + } = useGetOne('posts', { id: 2 }); |
| 63 | + const [create, { isPending }] = useCreate( |
| 64 | + 'posts', |
| 65 | + { |
| 66 | + data: { id: 2, title: 'Hello World' }, |
| 67 | + }, |
| 68 | + { |
| 69 | + mutationMode, |
| 70 | + onSuccess: () => setSuccess('success'), |
| 71 | + } |
| 72 | + ); |
| 73 | + const handleClick = () => { |
| 74 | + create(); |
| 75 | + }; |
| 76 | + return ( |
| 77 | + <> |
| 78 | + {isPendingGetOne ? ( |
| 79 | + <p>Loading...</p> |
| 80 | + ) : error ? ( |
| 81 | + <p>{error.message}</p> |
| 82 | + ) : ( |
| 83 | + <dl> |
| 84 | + <dt>id</dt> |
| 85 | + <dd>{data?.id}</dd> |
| 86 | + <dt>title</dt> |
| 87 | + <dd>{data?.title}</dd> |
| 88 | + <dt>author</dt> |
| 89 | + <dd>{data?.author}</dd> |
| 90 | + </dl> |
| 91 | + )} |
| 92 | + <div> |
| 93 | + <button onClick={handleClick} disabled={isPending}> |
| 94 | + Create post |
| 95 | + </button> |
| 96 | + |
| 97 | + <button |
| 98 | + onClick={() => setMutationMode('optimistic')} |
| 99 | + disabled={isPending} |
| 100 | + > |
| 101 | + Change mutation mode to optimistic |
| 102 | + </button> |
| 103 | + |
| 104 | + <button onClick={() => refetch()}>Refetch</button> |
| 105 | + </div> |
| 106 | + {success && <div>{success}</div>} |
| 107 | + {isMutating !== 0 && <div>mutating</div>} |
| 108 | + </> |
| 109 | + ); |
| 110 | +}; |
| 111 | + |
| 112 | +export const Params = ({ timeout = 1000 }) => { |
| 113 | + const posts = [{ id: 1, title: 'Hello', author: 'John Doe' }]; |
| 114 | + const dataProvider = { |
| 115 | + getOne: (resource, params) => { |
| 116 | + return new Promise((resolve, reject) => { |
| 117 | + const data = posts.find(p => p.id === params.id); |
| 118 | + setTimeout(() => { |
| 119 | + if (!data) { |
| 120 | + reject(new Error('nothing yet')); |
| 121 | + } |
| 122 | + resolve({ data }); |
| 123 | + }, timeout); |
| 124 | + }); |
| 125 | + }, |
| 126 | + create: (resource, params) => { |
| 127 | + return new Promise(resolve => { |
| 128 | + setTimeout(() => { |
| 129 | + posts.push(params.data); |
| 130 | + resolve({ data: params.data }); |
| 131 | + }, timeout); |
| 132 | + }); |
| 133 | + }, |
| 134 | + } as any; |
| 135 | + return ( |
| 136 | + <CoreAdminContext |
| 137 | + queryClient={ |
| 138 | + new QueryClient({ |
| 139 | + defaultOptions: { |
| 140 | + queries: { retry: false }, |
| 141 | + mutations: { retry: false }, |
| 142 | + }, |
| 143 | + }) |
| 144 | + } |
| 145 | + dataProvider={dataProvider} |
| 146 | + > |
| 147 | + <ParamsCore /> |
| 148 | + </CoreAdminContext> |
| 149 | + ); |
| 150 | +}; |
| 151 | + |
| 152 | +const ParamsCore = () => { |
| 153 | + const isMutating = useIsMutating(); |
| 154 | + const [success, setSuccess] = React.useState<string>(); |
| 155 | + const [params, setParams] = React.useState<any>({ title: 'Hello World' }); |
| 156 | + |
| 157 | + const { |
| 158 | + isPending: isPendingGetOne, |
| 159 | + data, |
| 160 | + error, |
| 161 | + refetch, |
| 162 | + } = useGetOne('posts', { id: 2 }); |
| 163 | + const [create, { isPending }] = useCreate( |
| 164 | + 'posts', |
| 165 | + { |
| 166 | + data: { id: 2, ...params }, |
| 167 | + }, |
| 168 | + { |
| 169 | + mutationMode: 'optimistic', |
| 170 | + onSuccess: () => setSuccess('success'), |
| 171 | + } |
| 172 | + ); |
| 173 | + const handleClick = () => { |
| 174 | + create(); |
| 175 | + }; |
| 176 | + return ( |
| 177 | + <> |
| 178 | + {isPendingGetOne ? ( |
| 179 | + <p>Loading...</p> |
| 180 | + ) : error ? ( |
| 181 | + <p>{error.message}</p> |
| 182 | + ) : ( |
| 183 | + <dl> |
| 184 | + <dt>id</dt> |
| 185 | + <dd>{data?.id}</dd> |
| 186 | + <dt>title</dt> |
| 187 | + <dd>{data?.title}</dd> |
| 188 | + <dt>author</dt> |
| 189 | + <dd>{data?.author}</dd> |
| 190 | + </dl> |
| 191 | + )} |
| 192 | + <div> |
| 193 | + <button onClick={handleClick} disabled={isPending}> |
| 194 | + Create post |
| 195 | + </button> |
| 196 | + |
| 197 | + <button |
| 198 | + onClick={() => setParams({ title: 'Goodbye World' })} |
| 199 | + disabled={isPending} |
| 200 | + > |
| 201 | + Change params |
| 202 | + </button> |
| 203 | + |
| 204 | + <button onClick={() => refetch()}>Refetch</button> |
| 205 | + </div> |
| 206 | + {success && <div>{success}</div>} |
| 207 | + {isMutating !== 0 && <div>mutating</div>} |
| 208 | + </> |
| 209 | + ); |
| 210 | +}; |
0 commit comments