|
1 | 1 | import * as React from 'react'; |
2 | 2 | import { useState } from 'react'; |
3 | 3 | import { QueryClient, useIsMutating } from '@tanstack/react-query'; |
| 4 | +import fakeRestDataProvider from 'ra-data-fakerest'; |
4 | 5 |
|
5 | | -import { CoreAdminContext } from '../core'; |
| 6 | +import { CoreAdmin, CoreAdminContext, Resource } from '../core'; |
6 | 7 | import { useDelete } from './useDelete'; |
7 | 8 | import { useGetList } from './useGetList'; |
8 | 9 | import type { DataProvider, MutationMode as MutationModeType } from '../types'; |
| 10 | +import { |
| 11 | + EditBase, |
| 12 | + ListBase, |
| 13 | + RecordsIterator, |
| 14 | + useDeleteController, |
| 15 | + WithRecord, |
| 16 | +} from '../controller'; |
| 17 | +import { TestMemoryRouter } from '../routing'; |
| 18 | +import { useNotificationContext } from '../notification'; |
| 19 | +import { useTakeUndoableMutation } from './undo'; |
9 | 20 |
|
10 | 21 | export default { title: 'ra-core/dataProvider/useDelete' }; |
11 | 22 |
|
@@ -162,3 +173,110 @@ const ParamsCore = () => { |
162 | 173 | </> |
163 | 174 | ); |
164 | 175 | }; |
| 176 | + |
| 177 | +const Notification = () => { |
| 178 | + const { notifications, resetNotifications } = useNotificationContext(); |
| 179 | + const takeMutation = useTakeUndoableMutation(); |
| 180 | + |
| 181 | + return notifications.length > 0 ? ( |
| 182 | + <> |
| 183 | + <div>{notifications[0].message}</div> |
| 184 | + <div style={{ display: 'flex', gap: '16px' }}> |
| 185 | + <button |
| 186 | + onClick={() => { |
| 187 | + if (notifications[0].notificationOptions?.undoable) { |
| 188 | + const mutation = takeMutation(); |
| 189 | + if (mutation) { |
| 190 | + mutation({ isUndo: false }); |
| 191 | + } |
| 192 | + } |
| 193 | + resetNotifications(); |
| 194 | + }} |
| 195 | + > |
| 196 | + Close |
| 197 | + </button> |
| 198 | + </div> |
| 199 | + </> |
| 200 | + ) : null; |
| 201 | +}; |
| 202 | + |
| 203 | +const DeleteButton = ({ mutationMode }: { mutationMode: MutationModeType }) => { |
| 204 | + const { isPending, handleDelete } = useDeleteController({ |
| 205 | + mutationMode, |
| 206 | + redirect: 'list', |
| 207 | + }); |
| 208 | + return ( |
| 209 | + <button onClick={handleDelete} disabled={isPending}> |
| 210 | + Delete |
| 211 | + </button> |
| 212 | + ); |
| 213 | +}; |
| 214 | + |
| 215 | +export const InvalidateList = ({ |
| 216 | + mutationMode, |
| 217 | +}: { |
| 218 | + mutationMode: MutationModeType; |
| 219 | +}) => { |
| 220 | + const dataProvider = fakeRestDataProvider( |
| 221 | + { |
| 222 | + posts: [ |
| 223 | + { id: 1, title: 'Hello' }, |
| 224 | + { id: 2, title: 'World' }, |
| 225 | + ], |
| 226 | + }, |
| 227 | + process.env.NODE_ENV !== 'test', |
| 228 | + process.env.NODE_ENV === 'test' ? 10 : 1000 |
| 229 | + ); |
| 230 | + |
| 231 | + return ( |
| 232 | + <TestMemoryRouter initialEntries={['/posts/1']}> |
| 233 | + <CoreAdmin dataProvider={dataProvider}> |
| 234 | + <Resource |
| 235 | + name="posts" |
| 236 | + edit={ |
| 237 | + <EditBase> |
| 238 | + <div> |
| 239 | + <h1>Edit Post</h1> |
| 240 | + <WithRecord |
| 241 | + render={record => ( |
| 242 | + <div>Title: {record.title}</div> |
| 243 | + )} |
| 244 | + /> |
| 245 | + <DeleteButton mutationMode={mutationMode} /> |
| 246 | + </div> |
| 247 | + </EditBase> |
| 248 | + } |
| 249 | + list={ |
| 250 | + <ListBase loading={<p>Loading...</p>}> |
| 251 | + <RecordsIterator |
| 252 | + render={(record: any) => ( |
| 253 | + <div |
| 254 | + style={{ |
| 255 | + display: 'flex', |
| 256 | + gap: '8px', |
| 257 | + alignItems: 'center', |
| 258 | + }} |
| 259 | + > |
| 260 | + {record.id}: {record.title} |
| 261 | + </div> |
| 262 | + )} |
| 263 | + /> |
| 264 | + <Notification /> |
| 265 | + </ListBase> |
| 266 | + } |
| 267 | + /> |
| 268 | + </CoreAdmin> |
| 269 | + </TestMemoryRouter> |
| 270 | + ); |
| 271 | +}; |
| 272 | +InvalidateList.args = { |
| 273 | + mutationMode: 'undoable', |
| 274 | +}; |
| 275 | +InvalidateList.argTypes = { |
| 276 | + mutationMode: { |
| 277 | + control: { |
| 278 | + type: 'select', |
| 279 | + }, |
| 280 | + options: ['pessimistic', 'optimistic', 'undoable'], |
| 281 | + }, |
| 282 | +}; |
0 commit comments