Last active
September 17, 2020 09:09
-
-
Save alldayalone/537bb202fee07805022a0e841d075038 to your computer and use it in GitHub Desktop.
[My WET codebase trying] WET part
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useMutation, queryCache } from 'react-query'; | |
import { useLocation, useHistory } from 'react-router-dom'; | |
import { UserStatus, api, showSuccessMessage } from 'package'; | |
const SuccessMessageByStatus = { | |
[UserStatus.BLOCKED]: 'User blocked', | |
[UserStatus.PROMOTED]: 'User promoted', | |
[UserStatus.ACTIVE]: 'User activated', | |
}; | |
export function useUpdateUser(userId) { | |
return useMutation( | |
(data) => api.updateById(userId, data), | |
{ | |
onSuccess: () => { | |
queryCache.invalidateQueries('users'); | |
}, | |
}, | |
); | |
} | |
export function useUploadUserPhoto(userId) { | |
return useMutation( | |
(data) => api.uploadPhotoById(userId, data), | |
{ | |
onSuccess: () => { | |
queryCache.invalidateQueries('users'); | |
}, | |
}, | |
); | |
} | |
function useUpdateUserStatus(userId, status) { | |
const location = useLocation(); | |
const history = useHistory(); | |
return useMutation( | |
() => api.updateUserStatusById(userId, { status }), | |
{ | |
onSuccess: () => { | |
showSuccessMessage(SuccessMessageByStatus[status]); | |
if (status === UserStatus.BLOCKED && location.pathname !== 'users') { | |
history.replace('users'); | |
} | |
queryCache.invalidateQueries('users'); | |
}, | |
}, | |
); | |
} | |
export function useActivateUser(userId) { | |
return useUpdateUserStatus(userId, UserStatus.ACTIVE); | |
} | |
export function useBlockUser(userId) { | |
return useUpdateUserStatus(userId, UserStatus.BLOCKED); | |
} | |
export function usePromoteUser(userId) { | |
return useUpdateUserStatus(userId, UserStatus.PROMOTED); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment