public inbox for [email protected]
 help / color / mirror / Atom feed
From: Muhammad Rizki <[email protected]>
To: Ammar Faizi <[email protected]>
Cc: Muhammad Rizki <[email protected]>,
	Alviro Iskandar Setiawan <[email protected]>,
	Irvan Malik Azantha <[email protected]>,
	Memet Zx <[email protected]>,
	GNU/Weeb Mailing List <[email protected]>
Subject: [PATCH v2 2/2] feat(page): add error message for recent messages
Date: Wed, 27 Sep 2023 20:09:34 +0700	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

Show error message to the recent messages UI when API connection is
failure.

Signed-off-by: Muhammad Rizki <[email protected]>
---
 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


  parent reply	other threads:[~2023-09-27 13:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-27 13:09 [PATCH v2 0/2] Fix getFixedRandomColor() function Muhammad Rizki
2023-09-27 13:09 ` [PATCH v2 1/2] fix(page): fix " Muhammad Rizki
2023-09-27 13:09 ` Muhammad Rizki [this message]
2023-09-27 13:14 ` [PATCH v2 0/2] Fix " Ammar Faizi
2023-09-27 14:54   ` Muhammad Rizki
2023-09-27 14:54     ` Ammar Faizi
2023-09-27 14:56       ` Muhammad Rizki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox