From 7ec8dea0bb1f7fa7f0db620dc00e6ff115b545f8 Mon Sep 17 00:00:00 2001 From: Ayobami Date: Tue, 1 Jul 2025 18:27:50 +0100 Subject: [PATCH] ISSUE 8: add API logic for archiving and unarchiving a room --- src/pages/Common/Messages/MessagesPage.jsx | 94 ++++++++++++++++++++-- 1 file changed, 87 insertions(+), 7 deletions(-) diff --git a/src/pages/Common/Messages/MessagesPage.jsx b/src/pages/Common/Messages/MessagesPage.jsx index 5c7cda1..6d35ca5 100644 --- a/src/pages/Common/Messages/MessagesPage.jsx +++ b/src/pages/Common/Messages/MessagesPage.jsx @@ -382,16 +382,96 @@ const MessagesPage = () => { async function archiveRoom(id) { sdk.setTable("room"); - // call API - callRestAPI (You can see callRestAPI implementation in other functions) here to archive room chat. Method is PUT - // update archived state of selected room chat without refreshing the page and toast a success message - //Also switch to the archive tab on success of the API, with the archived room chat showing under there + try { + const result = await sdk.callRestAPI( + { id, is_archive: ARCHIVE_STATUS.IS_ARCHIVE }, + "PUT" + ); + if (!result.error) { + // Update the room in the local state + setRooms((prev) => + prev.map((room) => + room.id === id + ? { ...room, is_archive: ARCHIVE_STATUS.IS_ARCHIVE } + : room + ) + ); + // Switch to archive tab and show the archived room + setSearchParams((params) => { + params.set("message_tab", "archive"); + return params; + }); + showToast( + globalDispatch, + "Chat archived successfully!", + 4000, + "SUCCESS" + ); + // Optionally, refresh archived rooms list + getArchivedRooms(); + } else { + showToast( + globalDispatch, + result.message || "Failed to archive chat.", + 4000, + "ERROR" + ); + } + } catch (err) { + showToast( + globalDispatch, + err.message || "Failed to archive chat.", + 4000, + "ERROR" + ); + } } async function unArchiveRoom(id) { sdk.setTable("room"); - // call API - callRestAPI (You can see callRestAPI implementation in other functions) here to unarchive room chat. Method is PUT - // update unarchived state of selected room chat without refreshing the page and toast a success message - //Also switch to the inbox tab on success of the API, with the unarchived room chat showing under there + try { + const result = await sdk.callRestAPI( + { id, is_archive: ARCHIVE_STATUS.NOT_ARCHIVE }, + "PUT" + ); + if (!result.error) { + // Update the room in the local state + setRooms((prev) => + prev.map((room) => + room.id === id + ? { ...room, is_archive: ARCHIVE_STATUS.NOT_ARCHIVE } + : room + ) + ); + // Switch to inbox tab and show the unarchived room + setSearchParams((params) => { + params.set("message_tab", "inbox"); + return params; + }); + showToast( + globalDispatch, + "Chat unarchived successfully!", + 4000, + "SUCCESS" + ); + // Optionally, refresh rooms list + getRooms(); + } else { + showToast( + globalDispatch, + result.message || "Failed to unarchive chat.", + 4000, + "ERROR" + ); + } + } catch (err) { + showToast( + globalDispatch, + err.message || "Failed to unarchive chat.", + 4000, + "ERROR" + ); + } } async function fetchExtraBookingDetails() { @@ -782,7 +862,7 @@ const MessagesPage = () => { setSearchParams(searchParams); }} > - Archive + Archive ({archivedCount}) {roomsFetched && (