Skip to content

Commit

Permalink
Sending messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ChampionBuffalo1 committed Oct 13, 2023
1 parent dc8db25 commit 5116d99
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/pages/Group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,32 @@ export default function Group({ id }: { id: string }) {

// TODO: Complete this
const mutation = useMutation({
mutationFn: async (): Promise<Record<string, string>> => {
mutationFn: async () => {
const { data } = await axiosInstance.post(
'/group/message',
`/group/${id}/messages/create`,
JSON.stringify({
groupId: id,
content: messageState
text: messageState
})
);
setMessageState('');
return data;
},
onSuccess: ({ data }) => {

dispatch(addMessage(data));
onSuccess: ({
message
}: {
message: {
id: string;
text: string;
};
}) => {
dispatch(
addMessage({
id,
message
})
);
},
onError: error => {
onError: (error: unknown) => {
console.log(error);
}
});
Expand All @@ -54,6 +65,12 @@ export default function Group({ id }: { id: string }) {
id="message"
value={messageState}
className="w-full input outline-none"
onKeyDown={e => {
if (e.key === 'Enter') {
e.preventDefault();
mutation.mutateAsync();
}
}}
onChange={e => {
setMessageState(e.target.value);
}}
Expand Down

0 comments on commit 5116d99

Please sign in to comment.