From aefdc8dcd6f6b57ab0f00d6c6ddc67143d1c0830 Mon Sep 17 00:00:00 2001
From: COURTES Guillaume <guillaumecourtes88@gmail.com>
Date: Sun, 31 Jan 2021 18:37:12 +0100
Subject: [PATCH] fix API fetch on screen focus for rooms list & messages

---
 react-native/ChatApp/components/ListScreen.js    | 16 +++++++++++++++-
 .../ChatApp/components/MessagesScreen.js         | 16 ++++++++++++++--
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/react-native/ChatApp/components/ListScreen.js b/react-native/ChatApp/components/ListScreen.js
index 4645b4332..3f4089442 100644
--- a/react-native/ChatApp/components/ListScreen.js
+++ b/react-native/ChatApp/components/ListScreen.js
@@ -18,7 +18,8 @@ class ListScreen extends React.Component {
 
   }
 
-  async componentDidMount() {
+  async getRooms() {
+
     let status = null;
 
     //get rooms from API
@@ -72,6 +73,19 @@ class ListScreen extends React.Component {
     }
   }
 
+  componentDidMount() {
+
+    this.getRooms();
+
+    const { navigation } = this.props;
+
+    this.focusListener = navigation.addListener("focus", async () => {
+
+      this.getRooms();
+
+    });
+  }
+
   keyExtractor = (item, index) => index.toString();
 
   render() {
diff --git a/react-native/ChatApp/components/MessagesScreen.js b/react-native/ChatApp/components/MessagesScreen.js
index b71dac0a2..c5d801264 100644
--- a/react-native/ChatApp/components/MessagesScreen.js
+++ b/react-native/ChatApp/components/MessagesScreen.js
@@ -22,8 +22,7 @@ class MessagesScreen extends React.Component {
     this.onSend = this.onSend.bind(this);
   }
 
-  async componentDidMount() {
-
+  async getMessages() {
     let status = null;
 
     //get user from asyncStorage
@@ -109,6 +108,19 @@ class MessagesScreen extends React.Component {
     this.setState({ isLoading: false })
   }
 
+  componentDidMount() {
+
+    this.getMessages();
+
+    const { navigation } = this.props;
+
+    this.focusListener = navigation.addListener("focus", async () => {
+
+      this.getMessages();
+
+    });
+  }
+
   async onSend(messages = []) {
     let status = null;
 
-- 
GitLab