|
| 1 | +import * as React from 'react'; |
| 2 | +import { QueryClientProvider, QueryClient } from '@tanstack/react-query'; |
| 3 | +import { RecordContextProvider } from '../record'; |
| 4 | +import { DataProviderContext } from '../../dataProvider'; |
| 5 | +import { ResourceContextProvider } from '../../core'; |
| 6 | +import { TestMemoryRouter } from '../../routing'; |
| 7 | +import { ReferenceManyCountBase } from './ReferenceManyCountBase'; |
| 8 | + |
| 9 | +export default { |
| 10 | + title: 'ra-core/controller/field/ReferenceManyCountBase', |
| 11 | + excludeStories: ['Wrapper'], |
| 12 | +}; |
| 13 | + |
| 14 | +const post = { |
| 15 | + id: 1, |
| 16 | + title: 'Lorem Ipsum', |
| 17 | +}; |
| 18 | +const comments = [ |
| 19 | + { id: 1, post_id: 1, is_published: true }, |
| 20 | + { id: 2, post_id: 1, is_published: true }, |
| 21 | + { id: 3, post_id: 1, is_published: false }, |
| 22 | + { id: 4, post_id: 2, is_published: true }, |
| 23 | + { id: 5, post_id: 2, is_published: false }, |
| 24 | +]; |
| 25 | + |
| 26 | +export const Wrapper = ({ dataProvider, children }) => ( |
| 27 | + <TestMemoryRouter> |
| 28 | + <DataProviderContext.Provider value={dataProvider}> |
| 29 | + <QueryClientProvider |
| 30 | + client={ |
| 31 | + new QueryClient({ |
| 32 | + defaultOptions: { |
| 33 | + queries: { |
| 34 | + retry: false, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }) |
| 38 | + } |
| 39 | + > |
| 40 | + <ResourceContextProvider value="posts"> |
| 41 | + <RecordContextProvider value={post}> |
| 42 | + {children} |
| 43 | + </RecordContextProvider> |
| 44 | + </ResourceContextProvider> |
| 45 | + </QueryClientProvider> |
| 46 | + </DataProviderContext.Provider> |
| 47 | + </TestMemoryRouter> |
| 48 | +); |
| 49 | + |
| 50 | +export const Basic = () => ( |
| 51 | + <Wrapper |
| 52 | + dataProvider={{ |
| 53 | + getManyReference: () => |
| 54 | + Promise.resolve({ |
| 55 | + data: [comments.filter(c => c.post_id === 1)[0]], |
| 56 | + total: comments.filter(c => c.post_id === 1).length, |
| 57 | + }), |
| 58 | + }} |
| 59 | + > |
| 60 | + <ReferenceManyCountBase reference="comments" target="post_id" /> |
| 61 | + </Wrapper> |
| 62 | +); |
| 63 | + |
| 64 | +export const LoadingState = () => ( |
| 65 | + <Wrapper dataProvider={{ getManyReference: () => new Promise(() => {}) }}> |
| 66 | + <ReferenceManyCountBase |
| 67 | + reference="comments" |
| 68 | + target="post_id" |
| 69 | + loading="loading..." |
| 70 | + /> |
| 71 | + </Wrapper> |
| 72 | +); |
| 73 | + |
| 74 | +export const ErrorState = () => ( |
| 75 | + <Wrapper |
| 76 | + dataProvider={{ |
| 77 | + getManyReference: () => Promise.reject(new Error('problem')), |
| 78 | + }} |
| 79 | + > |
| 80 | + <ReferenceManyCountBase |
| 81 | + reference="comments" |
| 82 | + target="post_id" |
| 83 | + error="Error!" |
| 84 | + /> |
| 85 | + </Wrapper> |
| 86 | +); |
| 87 | + |
| 88 | +export const Filter = () => ( |
| 89 | + <Wrapper |
| 90 | + dataProvider={{ |
| 91 | + getManyReference: (resource, params) => |
| 92 | + Promise.resolve({ |
| 93 | + data: comments |
| 94 | + .filter(c => c.post_id === 1) |
| 95 | + .filter(post => |
| 96 | + Object.keys(params.filter).every( |
| 97 | + key => post[key] === params.filter[key] |
| 98 | + ) |
| 99 | + ), |
| 100 | + total: comments |
| 101 | + .filter(c => c.post_id === 1) |
| 102 | + .filter(post => |
| 103 | + Object.keys(params.filter).every( |
| 104 | + key => post[key] === params.filter[key] |
| 105 | + ) |
| 106 | + ).length, |
| 107 | + }), |
| 108 | + }} |
| 109 | + > |
| 110 | + <ReferenceManyCountBase |
| 111 | + reference="comments" |
| 112 | + target="post_id" |
| 113 | + filter={{ is_published: true }} |
| 114 | + /> |
| 115 | + </Wrapper> |
| 116 | +); |
| 117 | + |
| 118 | +export const Slow = () => ( |
| 119 | + <Wrapper |
| 120 | + dataProvider={{ |
| 121 | + getManyReference: () => |
| 122 | + new Promise(resolve => |
| 123 | + setTimeout( |
| 124 | + () => |
| 125 | + resolve({ |
| 126 | + data: [ |
| 127 | + comments.filter(c => c.post_id === 1)[0], |
| 128 | + ], |
| 129 | + total: comments.filter(c => c.post_id === 1) |
| 130 | + .length, |
| 131 | + }), |
| 132 | + 2000 |
| 133 | + ) |
| 134 | + ), |
| 135 | + }} |
| 136 | + > |
| 137 | + <ReferenceManyCountBase |
| 138 | + reference="comments" |
| 139 | + target="post_id" |
| 140 | + loading="Loading..." |
| 141 | + /> |
| 142 | + </Wrapper> |
| 143 | +); |
0 commit comments