Skip to content

Commit

Permalink
fix: 🐛 reset send message mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
apotdevin committed May 24, 2020
1 parent cf965f3 commit 60ad4b3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
20 changes: 20 additions & 0 deletions src/hooks/UseMutationWithReset.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as React from 'react';

export const useMutationResultWithReset = <T extends {}>(
data: T | undefined
): [T | undefined, () => void] => {
const current = React.useRef(data);
const latest = React.useRef(data);
const [, setState] = React.useState(0);
const clearCurrentData = () => {
current.current = undefined;
setState(state => state + 1);
};

if (data !== latest.current) {
current.current = data;
latest.current = data;
}

return [current.current, clearCurrentData];
};
7 changes: 5 additions & 2 deletions src/views/chat/ChatBubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Circle } from 'react-feather';
import ScaleLoader from 'react-spinners/ScaleLoader';
import { useAccountState } from 'src/context/AccountContext';
import { useSendMessageMutation } from 'src/graphql/mutations/__generated__/sendMessage.generated';
import { useMutationResultWithReset } from 'src/hooks/UseMutationWithReset';
import {
chatBubbleColor,
chatSentBubbleColor,
Expand Down Expand Up @@ -34,9 +35,10 @@ const SendButton = ({ amount }: SendButtonProps) => {
const dispatch = useChatDispatch();
const { account } = useAccountState();

const [sendMessage, { loading, data }] = useSendMessageMutation({
const [sendMessage, { loading, data: _data }] = useSendMessageMutation({
onError: error => toast.error(getErrorContent(error)),
});
const [data, resetMutationResult] = useMutationResultWithReset(_data);

React.useEffect(() => {
if (!loading && data && data.sendMessage >= 0) {
Expand All @@ -54,8 +56,9 @@ const SendButton = ({ amount }: SendButtonProps) => {
userId: account.id,
sender,
});
resetMutationResult();
}
}, [loading, data, amount, dispatch, sender, account]);
}, [loading, data, amount, dispatch, sender, account, resetMutationResult]);

return (
<SecureWrapper
Expand Down
6 changes: 5 additions & 1 deletion src/views/chat/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import { toast } from 'react-toastify';
import { useAccountState } from 'src/context/AccountContext';
import { useSendMessageMutation } from 'src/graphql/mutations/__generated__/sendMessage.generated';
import { useMutationResultWithReset } from 'src/hooks/UseMutationWithReset';
import { Input } from '../../components/input/Input';
import { SingleLine } from '../../components/generic/Styled';
import { SecureButton } from '../../components/buttons/secureButton/SecureButton';
Expand All @@ -26,9 +27,10 @@ export const ChatInput = ({
const { sender } = useChatState();
const dispatch = useChatDispatch();

const [sendMessage, { loading, data }] = useSendMessageMutation({
const [sendMessage, { loading, data: _data }] = useSendMessageMutation({
onError: error => toast.error(getErrorContent(error)),
});
const [data, resetMutationResult] = useMutationResultWithReset(_data);

const [formattedMessage, contentType, tokens, canSend] = handleMessage(
message
Expand All @@ -51,6 +53,7 @@ export const ChatInput = ({
userId: account.id,
sender: customSender || sender,
});
resetMutationResult();
}
}, [
loading,
Expand All @@ -62,6 +65,7 @@ export const ChatInput = ({
tokens,
account,
dispatch,
resetMutationResult,
]);

return (
Expand Down

0 comments on commit 60ad4b3

Please sign in to comment.