|
| 1 | +import * as React from 'react'; |
| 2 | +import { |
| 3 | + DataGridPro, |
| 4 | + useGridApiRef, |
| 5 | + GridToolbar, |
| 6 | + GridDataSource, |
| 7 | + GridGetRowsParams, |
| 8 | + GRID_ROOT_GROUP_ID, |
| 9 | +} from '@mui/x-data-grid-pro'; |
| 10 | +import Checkbox from '@mui/material/Checkbox'; |
| 11 | +import FormControlLabel from '@mui/material/FormControlLabel'; |
| 12 | +import { useMockServer } from '@mui/x-data-grid-generator'; |
| 13 | +import Alert from '@mui/material/Alert'; |
| 14 | +import Button from '@mui/material/Button'; |
| 15 | +import Snackbar, { SnackbarProps } from '@mui/material/Snackbar'; |
| 16 | + |
| 17 | +function ErrorSnackbar(props: SnackbarProps & { onRetry: () => void }) { |
| 18 | + const { onRetry, ...rest } = props; |
| 19 | + return ( |
| 20 | + <Snackbar {...rest}> |
| 21 | + <Alert |
| 22 | + severity="error" |
| 23 | + variant="filled" |
| 24 | + sx={{ width: '100%' }} |
| 25 | + action={ |
| 26 | + <Button color="inherit" size="small" onClick={onRetry}> |
| 27 | + Retry |
| 28 | + </Button> |
| 29 | + } |
| 30 | + > |
| 31 | + Failed to fetch row data |
| 32 | + </Alert> |
| 33 | + </Snackbar> |
| 34 | + ); |
| 35 | +} |
| 36 | + |
| 37 | +function ServerSideLazyLoadingErrorHandling() { |
| 38 | + const apiRef = useGridApiRef(); |
| 39 | + const [retryParams, setRetryParams] = React.useState<GridGetRowsParams | null>( |
| 40 | + null, |
| 41 | + ); |
| 42 | + const [shouldRequestsFail, setShouldRequestsFail] = React.useState(false); |
| 43 | + |
| 44 | + const { fetchRows, ...props } = useMockServer( |
| 45 | + { rowLength: 100 }, |
| 46 | + { useCursorPagination: false, minDelay: 300, maxDelay: 800 }, |
| 47 | + shouldRequestsFail, |
| 48 | + ); |
| 49 | + |
| 50 | + const dataSource: GridDataSource = React.useMemo( |
| 51 | + () => ({ |
| 52 | + getRows: async (params) => { |
| 53 | + const urlParams = new URLSearchParams({ |
| 54 | + filterModel: JSON.stringify(params.filterModel), |
| 55 | + sortModel: JSON.stringify(params.sortModel), |
| 56 | + start: `${params.start}`, |
| 57 | + end: `${params.end}`, |
| 58 | + }); |
| 59 | + const getRowsResponse = await fetchRows( |
| 60 | + `https://mui.com/x/api/data-grid?${urlParams.toString()}`, |
| 61 | + ); |
| 62 | + |
| 63 | + // Reset the retryParams when new rows are fetched |
| 64 | + setRetryParams(null); |
| 65 | + return { |
| 66 | + rows: getRowsResponse.rows, |
| 67 | + rowCount: getRowsResponse.rowCount, |
| 68 | + }; |
| 69 | + }, |
| 70 | + }), |
| 71 | + [fetchRows], |
| 72 | + ); |
| 73 | + |
| 74 | + return ( |
| 75 | + <div style={{ width: '100%' }}> |
| 76 | + <FormControlLabel |
| 77 | + control={ |
| 78 | + <Checkbox |
| 79 | + checked={shouldRequestsFail} |
| 80 | + onChange={(event) => setShouldRequestsFail(event.target.checked)} |
| 81 | + /> |
| 82 | + } |
| 83 | + label="Make the requests fail" |
| 84 | + /> |
| 85 | + <div style={{ height: 400, position: 'relative' }}> |
| 86 | + {retryParams && ( |
| 87 | + <ErrorSnackbar |
| 88 | + open={!!retryParams} |
| 89 | + onRetry={() => { |
| 90 | + apiRef.current.unstable_dataSource.fetchRows( |
| 91 | + GRID_ROOT_GROUP_ID, |
| 92 | + retryParams, |
| 93 | + ); |
| 94 | + setRetryParams(null); |
| 95 | + }} |
| 96 | + /> |
| 97 | + )} |
| 98 | + <DataGridPro |
| 99 | + {...props} |
| 100 | + apiRef={apiRef} |
| 101 | + unstable_dataSource={dataSource} |
| 102 | + unstable_onDataSourceError={(_, params) => setRetryParams(params)} |
| 103 | + unstable_dataSourceCache={null} |
| 104 | + unstable_lazyLoading |
| 105 | + paginationModel={{ page: 0, pageSize: 10 }} |
| 106 | + slots={{ toolbar: GridToolbar }} |
| 107 | + /> |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + ); |
| 111 | +} |
| 112 | + |
| 113 | +export default ServerSideLazyLoadingErrorHandling; |
0 commit comments