Add user and rooms to global state

This commit is contained in:
2023-11-20 17:52:51 +07:00
parent 8377a7d5b0
commit 4aae26e994
15 changed files with 329 additions and 43 deletions
@@ -6,6 +6,7 @@ import { createUser as createUserFn } from '@service/userServices';
// Constants
import { ADD_SUCCESS } from '@constant/messages';
import { useUserRoomAvailable } from '@hook/useUserRoomAvailable';
/**
* Create user in database
@@ -13,12 +14,19 @@ import { ADD_SUCCESS } from '@constant/messages';
*/
const useCreateUser = () => {
const queryClient = useQueryClient();
const { dispatch } = useUserRoomAvailable();
const { mutate: createUser, isPending: isCreating } = useMutation({
mutationFn: createUserFn,
onSuccess: () => {
onSuccess: (user) => {
toast.success(ADD_SUCCESS);
queryClient.invalidateQueries({ queryKey: ['users'] });
// Add user to global statement
dispatch!({
type: 'addUser',
payload: [{ id: user.id, name: user.name }],
});
},
onError: (err) => toast.error(err.message),
});