Skip to content

Commit

Permalink
fix: add new thread as a newMessage and select it (Greenstand#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
gwynndp authored Apr 10, 2022
1 parent 041e28a commit c98c63d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
3 changes: 1 addition & 2 deletions src/components/Messaging/Messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ const Messaging = () => {
loadMessages,
loadRegions,
loadAuthors,
setErrorMessage,
setIsLoading,
} = useContext(MessagingContext);

Expand Down Expand Up @@ -115,7 +114,7 @@ const Messaging = () => {
<NewMessage
openModal={openModal}
handleClose={handleClose}
setErrorMessage={setErrorMessage}
setMessageRecipient={setMessageRecipient}
/>
<Button
className={button}
Expand Down
29 changes: 10 additions & 19 deletions src/components/Messaging/NewMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const useStyles = makeStyles((theme) => ({
},
}));

const NewMessage = ({ openModal, handleClose }) => {
const NewMessage = ({ openModal, handleClose, setMessageRecipient }) => {
const { box, formContent, header, button } = useStyles();
const {
setErrorMessage,
Expand Down Expand Up @@ -153,26 +153,17 @@ const NewMessage = ({ openModal, handleClose }) => {

log.debug('...update threads after postMessageSend');
// update the full set of threads
setThreads((prev) => {
const updated = prev
.reduce(
(threads, thread) => {
if (thread.userName === recipient) {
thread.messages.push(newMessage);
}
return threads;
},
[...prev]
)
.sort(
(a, b) =>
new Date(b?.messages?.at(-1).composed_at) -
new Date(a?.messages?.at(-1).composed_at)
);
return updated;
});
setThreads((prev) =>
[...prev, { userName: recipient, messages: [newMessage] }].sort(
(a, b) =>
new Date(b?.messages?.at(-1).composed_at) -
new Date(a?.messages?.at(-1).composed_at)
)
);
}

setMessageRecipient(recipient);

handleClose();
}
};
Expand Down

0 comments on commit c98c63d

Please sign in to comment.