forked from mui/mui-x
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ServerSideErrorHandling.js
131 lines (121 loc) · 3.6 KB
/
ServerSideErrorHandling.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import * as React from 'react';
import { DataGridPro, useGridApiRef, GridToolbar } from '@mui/x-data-grid-pro';
import Button from '@mui/material/Button';
import Checkbox from '@mui/material/Checkbox';
import FormControlLabel from '@mui/material/FormControlLabel';
import { alpha, styled, darken, lighten } from '@mui/material/styles';
import { useMockServer } from '@mui/x-data-grid-generator';
import LoadingSlate from './LoadingSlateNoSnap';
const pageSizeOptions = [5, 10, 50];
const serverOptions = { useCursorPagination: false };
const datasetOptions = {};
function getBorderColor(theme) {
if (theme.palette.mode === 'light') {
return lighten(alpha(theme.palette.divider, 1), 0.88);
}
return darken(alpha(theme.palette.divider, 1), 0.68);
}
const StyledDiv = styled('div')(({ theme: t }) => ({
position: 'absolute',
zIndex: 10,
fontSize: '0.875em',
top: 0,
height: '100%',
width: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
borderRadius: '4px',
border: `1px solid ${getBorderColor(t)}`,
backgroundColor: t.palette.background.default,
}));
function ErrorOverlay({ error }) {
if (!error) {
return null;
}
return <StyledDiv>{error}</StyledDiv>;
}
export default function ServerSideErrorHandling() {
const apiRef = useGridApiRef();
const [error, setError] = React.useState();
const [shouldRequestsFail, setShouldRequestsFail] = React.useState(false);
const { isInitialized, fetchRows, ...props } = useMockServer(
datasetOptions,
serverOptions,
shouldRequestsFail,
);
const dataSource = React.useMemo(
() => ({
getRows: async (params) => {
const urlParams = new URLSearchParams({
paginationModel: encodeURIComponent(
JSON.stringify(params.paginationModel),
),
filterModel: encodeURIComponent(JSON.stringify(params.filterModel)),
sortModel: encodeURIComponent(JSON.stringify(params.sortModel)),
});
const getRowsResponse = await fetchRows(
`https://mui.com/x/api/data-grid?${urlParams.toString()}`,
);
return {
rows: getRowsResponse.rows,
rowCount: getRowsResponse.rowCount,
};
},
}),
[fetchRows],
);
const initialState = React.useMemo(
() => ({
...props.initialState,
pagination: {
paginationModel: {
pageSize: 5,
},
rowCount: 0,
},
}),
[props.initialState],
);
return (
<div style={{ width: '100%' }}>
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<Button
onClick={() => {
setError('');
apiRef.current?.fetchTopLevelRows();
}}
>
Refetch rows
</Button>
<FormControlLabel
control={
<Checkbox
checked={shouldRequestsFail}
onChange={(e) => setShouldRequestsFail(e.target.checked)}
/>
}
label="Make the requests fail"
/>
</div>
<div style={{ height: 400, position: 'relative' }}>
{isInitialized ? (
<DataGridPro
{...props}
unstable_dataSource={dataSource}
unstable_onDataSourceError={(e) => setError(e.message)}
unstable_dataSourceCache={null}
apiRef={apiRef}
pagination
pageSizeOptions={pageSizeOptions}
initialState={initialState}
slots={{ toolbar: GridToolbar }}
/>
) : (
<LoadingSlate />
)}
{error && <ErrorOverlay error={error} />}
</div>
</div>
);
}