From 24678464efc9bc2facd8ac191d3171aa42e1c3d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Truite=20S=C3=A9ch=C3=A9e?= Date: Thu, 12 Sep 2024 23:00:38 +0200 Subject: [PATCH] refactor(Inbox): clean mark as unread feature --- src/components/app/Messaging/Inbox.jsx | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/components/app/Messaging/Inbox.jsx b/src/components/app/Messaging/Inbox.jsx index 010c453..f064bd7 100644 --- a/src/components/app/Messaging/Inbox.jsx +++ b/src/components/app/Messaging/Inbox.jsx @@ -14,8 +14,7 @@ export default function Inbox({ selectedMessage, setSelectedMessage, fetchMessag // States const { useUserData } = useContext(AppContext); const [search, setSearch] = useState(""); - const messages = useUserData("sortedMessages").get(); - const messages2 = useUserData("sortedMessages"); + const messages = useUserData("sortedMessages"); // behavior const handleClick = (message) => { @@ -31,17 +30,20 @@ export default function Inbox({ selectedMessage, setSelectedMessage, fetchMessag const handleMarkAsUnread = (event, msg) => { event.preventDefault(); event.stopPropagation(); - const oldMsg = messages2.get(); const controller = new AbortController(); fetchMessageMarkAsUnread([msg.id], controller); + + if (msg.id === selectedMessage) { + setSelectedMessage(null); + } + + // mark as unread locally and kick the content so as to trigger a refetch the next reading (as the "mark as read" feature is trigger when fetching the message) + const oldMsg = messages.get(); const msgIdx = oldMsg.findIndex((item) => item.id === msg.id); oldMsg[msgIdx].read = false; oldMsg[msgIdx].content = null; - messages2.set(oldMsg); + messages.set(oldMsg); - if (msg.id === selectedMessage) { - setSelectedMessage(null); - } } const handleChange = (event) => { @@ -69,11 +71,11 @@ export default function Inbox({ selectedMessage, setSelectedMessage, fetchMessag return (
- {messages !== undefined - ? (messages.length > 0 + {messages.get() !== undefined + ? (messages.get().length > 0 ?
    - {messages.filter(filterResearch).map((message) =>
  • handleClick(message)} onKeyDown={(event) => handleKeyDown(event, message)} key={message.id} role="button" tabIndex={0}> + {messages.get().filter(filterResearch).map((message) =>
  • handleClick(message)} onKeyDown={(event) => handleKeyDown(event, message)} key={message.id} role="button" tabIndex={0}>

    {message.from.name} {message.files?.length > 0 && }

    {message.subject}

    {(new Date(message.date)).toLocaleDateString("fr-FR", {