@@ -10,7 +10,7 @@ import {
1010} from "@mui/x-data-grid" ;
1111import { ActionButtons } from "./ActionButtons" ;
1212import { usersService } from "../../services" ;
13- import { Role } from "../../types" ;
13+ import { Role , User } from "../../types" ;
1414import { useSnackbar } from "notistack" ;
1515import { useUserDispatch , useUserState } from "../../contexts" ;
1616
@@ -61,15 +61,15 @@ const UserList = () => {
6161 userDispatch ( {
6262 type : "getAll" ,
6363 payload : users ,
64- } ) ,
64+ } )
6565 ) ;
6666 } , [ userDispatch ] ) ;
6767
68- const handleEditCellChangeCommitted = React . useCallback (
69- ( { id , field , value } ) => {
70- if ( field === " role" ) {
68+ const processRowUpdate = React . useCallback (
69+ ( newState : User , oldState : User ) => {
70+ if ( newState . role !== oldState . role ) {
7171 usersService
72- . assignRole ( id , value as Role )
72+ . assignRole ( oldState . id , newState . role as Role )
7373 . then ( ( ) => {
7474 enqueueSnackbar ( "Updated" , {
7575 variant : "success" ,
@@ -78,11 +78,12 @@ const UserList = () => {
7878 . catch ( ( err ) =>
7979 enqueueSnackbar ( err , {
8080 variant : "error" ,
81- } ) ,
81+ } )
8282 ) ;
8383 }
84+ return newState ;
8485 } ,
85- [ enqueueSnackbar ] ,
86+ [ enqueueSnackbar ]
8687 ) ;
8788
8889 const apiRef = useGridApiRef ( ) ;
@@ -101,7 +102,7 @@ const UserList = () => {
101102 slots = { {
102103 toolbar : DataGridCustomToolbar ,
103104 } }
104- onCellEditStop = { handleEditCellChangeCommitted }
105+ processRowUpdate = { processRowUpdate }
105106 />
106107 ) ;
107108} ;
0 commit comments