|
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 { useDeleteMany } from './useDeleteMany'; |
7 | 8 | import { useGetList } from './useGetList'; |
8 | 9 | import type { DataProvider, MutationMode as MutationModeType } from '../types'; |
| 10 | +import { TestMemoryRouter, useRedirect } from '../routing'; |
| 11 | +import { useNotificationContext, useNotify } from '../notification'; |
| 12 | +import { useTakeUndoableMutation } from './undo'; |
| 13 | +import { EditBase, ListBase, RecordsIterator } from '../controller'; |
9 | 14 |
|
10 | 15 | export default { title: 'ra-core/dataProvider/useDeleteMany' }; |
11 | 16 |
|
@@ -84,6 +89,125 @@ const MutationModeCore = () => { |
84 | 89 | ); |
85 | 90 | }; |
86 | 91 |
|
| 92 | +const Notification = () => { |
| 93 | + const { notifications, resetNotifications } = useNotificationContext(); |
| 94 | + const takeMutation = useTakeUndoableMutation(); |
| 95 | + |
| 96 | + return notifications.length > 0 ? ( |
| 97 | + <> |
| 98 | + <div>{notifications[0].message}</div> |
| 99 | + <div style={{ display: 'flex', gap: '16px' }}> |
| 100 | + <button |
| 101 | + onClick={() => { |
| 102 | + if (notifications[0].notificationOptions?.undoable) { |
| 103 | + const mutation = takeMutation(); |
| 104 | + if (mutation) { |
| 105 | + mutation({ isUndo: false }); |
| 106 | + } |
| 107 | + } |
| 108 | + resetNotifications(); |
| 109 | + }} |
| 110 | + > |
| 111 | + Close |
| 112 | + </button> |
| 113 | + </div> |
| 114 | + </> |
| 115 | + ) : null; |
| 116 | +}; |
| 117 | + |
| 118 | +const DeleteButton = ({ mutationMode }: { mutationMode: MutationModeType }) => { |
| 119 | + const notify = useNotify(); |
| 120 | + const redirect = useRedirect(); |
| 121 | + const [deleteMany, { isPending }] = useDeleteMany(); |
| 122 | + const handleClick = () => { |
| 123 | + deleteMany( |
| 124 | + 'posts', |
| 125 | + { |
| 126 | + ids: [1], |
| 127 | + }, |
| 128 | + { |
| 129 | + mutationMode, |
| 130 | + onSuccess: () => { |
| 131 | + notify('resources.posts.notifications.deleted', { |
| 132 | + type: 'info', |
| 133 | + undoable: mutationMode === 'undoable', |
| 134 | + }); |
| 135 | + redirect('list', 'posts'); |
| 136 | + }, |
| 137 | + } |
| 138 | + ); |
| 139 | + }; |
| 140 | + return ( |
| 141 | + <button onClick={handleClick} disabled={isPending}> |
| 142 | + Delete |
| 143 | + </button> |
| 144 | + ); |
| 145 | +}; |
| 146 | + |
| 147 | +export const InvalidateList = ({ |
| 148 | + mutationMode, |
| 149 | +}: { |
| 150 | + mutationMode: MutationModeType; |
| 151 | +}) => { |
| 152 | + const dataProvider = fakeRestDataProvider( |
| 153 | + { |
| 154 | + posts: [ |
| 155 | + { id: 1, title: 'Hello' }, |
| 156 | + { id: 2, title: 'World' }, |
| 157 | + ], |
| 158 | + }, |
| 159 | + process.env.NODE_ENV !== 'test', |
| 160 | + process.env.NODE_ENV === 'test' ? 10 : 1000 |
| 161 | + ); |
| 162 | + |
| 163 | + return ( |
| 164 | + <TestMemoryRouter initialEntries={['/posts/1']}> |
| 165 | + <CoreAdmin dataProvider={dataProvider}> |
| 166 | + <Resource |
| 167 | + name="posts" |
| 168 | + edit={ |
| 169 | + <EditBase> |
| 170 | + <div> |
| 171 | + <h1>Edit Post</h1> |
| 172 | + <DeleteButton mutationMode={mutationMode} /> |
| 173 | + </div> |
| 174 | + </EditBase> |
| 175 | + } |
| 176 | + list={ |
| 177 | + <ListBase loading={<p>Loading...</p>}> |
| 178 | + <RecordsIterator |
| 179 | + render={(record: any) => ( |
| 180 | + <div |
| 181 | + style={{ |
| 182 | + display: 'flex', |
| 183 | + gap: '8px', |
| 184 | + alignItems: 'center', |
| 185 | + }} |
| 186 | + > |
| 187 | + {record.id}: {record.title} |
| 188 | + </div> |
| 189 | + )} |
| 190 | + /> |
| 191 | + <Notification /> |
| 192 | + </ListBase> |
| 193 | + } |
| 194 | + /> |
| 195 | + </CoreAdmin> |
| 196 | + </TestMemoryRouter> |
| 197 | + ); |
| 198 | +}; |
| 199 | +InvalidateList.args = { |
| 200 | + mutationMode: 'undoable', |
| 201 | +}; |
| 202 | +InvalidateList.argTypes = { |
| 203 | + mutationMode: { |
| 204 | + control: { |
| 205 | + type: 'select', |
| 206 | + }, |
| 207 | + options: ['pessimistic', 'optimistic', 'undoable'], |
| 208 | + }, |
| 209 | +}; |
| 210 | + |
87 | 211 | export const Params = ({ dataProvider }: { dataProvider?: DataProvider }) => { |
88 | 212 | let posts = [ |
89 | 213 | { id: 1, title: 'Hello' }, |
|
0 commit comments