From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,URIBL_BLOCKED, URIBL_DBL_BLOCKED_OPENDNS autolearn=ham autolearn_force=no version=3.4.6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1695820210; bh=5t4t+CkdK1EZjpxAUmfQ9/f1XMwYvYBX6cLgZGVoqrc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=Kd2a++9rIPEe+MIh16+QnMI31lhUB67kV1vm/+ZO5061u+DktsfG34VqNVANCRSF8 lryCCMrh0H/sIb74FldfNCWfMRJHJZfGFkdWrGtpy5oJe7NSt2jOmM1bXi11uPcT4o nNv1+hy3wnLZfX7ZM7p/uH0CoNqoqFnCtwqSN8WtX4psTw9PWk2E5JJ2TK78asw83g DYpOeYmjTBLrkJdqUVd8lUEmqHpR5LjdhTuKmX9F1NNT6GB7oiUssl6DGwruS1oeeL ZkDwcVwL6+fsQm2CzyDrxr2Ub5EMJYm6kEefjBKaykq/mgFFjtyr8fGJpsysNOIjYW aXPkijmBcQr+g== Received: from localhost.localdomain (unknown [175.158.50.50]) by gnuweeb.org (Postfix) with ESMTPSA id B8C4C24B79E; Wed, 27 Sep 2023 20:10:08 +0700 (WIB) From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , Irvan Malik Azantha , Memet Zx , GNU/Weeb Mailing List Subject: [PATCH v2 2/2] feat(page): add error message for recent messages Date: Wed, 27 Sep 2023 20:09:34 +0700 Message-Id: <20230927130934.1237-3-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20230927130934.1237-1-kiizuha@gnuweeb.org> References: <20230927130934.1237-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Show error message to the recent messages UI when API connection is failure. Signed-off-by: Muhammad Rizki --- index.html | 52 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/index.html b/index.html index 81162db..5c6eef3 100644 --- a/index.html +++ b/index.html @@ -361,27 +361,31 @@ } async function getRecentMessages() { - const response = await fetch(API_URL + "/zxc.php?action=get_messages&chat_id=-1001483770714&limit=50"); - const { data } = (await response.json()).result; - - const transformedData = data.map(item => { - return { - user_id: item[0], - username: item[1], - first_name: item[2], - last_name: item[3], - user_photo: item[4], - message_id: item[5], - reply_to_message_id: item[6], - message_type: item[7], - text: item[8], - text_entities: item[9] ? JSON.parse(item[9]) : null, - file: item[10], - date: item[11] - }; - }); + try { + const response = await fetch(API_URL + "/zxc.php?action=get_messages&chat_id=-1001483770714&limit=50"); + const { data } = (await response.json()).result; + + const transformedData = data.map(item => { + return { + user_id: item[0], + username: item[1], + first_name: item[2], + last_name: item[3], + user_photo: item[4], + message_id: item[5], + reply_to_message_id: item[6], + message_type: item[7], + text: item[8], + text_entities: item[9] ? JSON.parse(item[9]) : null, + file: item[10], + date: item[11] + }; + }); - return transformedData.reverse(); + return transformedData.reverse(); + } catch { + return []; + } } function hashCode(name) { @@ -434,6 +438,14 @@ const messageData = await getRecentMessages(); const recentMessages = document.getElementById("recent-messages"); + if (messageData.length === 0) { + const connectionErrEl = document.createElement("div"); + connectionErrEl.classList.add("text-red-500", "italic", "text-center", "p-3"); + connectionErrEl.innerText = "Cannot Connect to the API.\nAPI Connection failure."; + recentMessages.appendChild(connectionErrEl); + return; + } + messageData.forEach(message => { const getColor = m => getFixedRandomColor(`${m.first_name}`); -- Muhammad Rizki