From 51e15eabc73754d579a787efc13de7dccf6b6ecc Mon Sep 17 00:00:00 2001 From: Yuwen Memon Date: Tue, 6 Dec 2022 14:40:08 -0800 Subject: [PATCH 01/11] Preserve offline state when signing out --- src/libs/actions/SignInRedirect.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index 9dc63952b5dd..ad5a45c695ef 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -41,7 +41,11 @@ function clearStorageAndRedirect(errorMessage) { const shouldForceOffline = currentShouldForceOffline; // Clearing storage discards the authToken. This causes a redirect to the SignIn screen - Onyx.clear() + Onyx.clear({ + [ONYXKEYS.NETWORK]: { + isOffline: currentIsOffline, + }, + }) .then(() => { if (preferredLocale) { Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, preferredLocale); From 3f696ada252f5ba67db604a326f27bac0f55a52f Mon Sep 17 00:00:00 2001 From: Yuwen Memon Date: Tue, 6 Dec 2022 15:16:50 -0800 Subject: [PATCH 02/11] Use keyStatesToPreserve instead --- src/libs/actions/SignInRedirect.js | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index ad5a45c695ef..77144c96567f 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -40,26 +40,24 @@ function clearStorageAndRedirect(errorMessage) { const isOffline = currentIsOffline; const shouldForceOffline = currentShouldForceOffline; + const keyStatesToPreserve = []; + + // After signing out, set ourselves as offline if we were offline before logging out and we are not forcing it. + // If we are forcing offline, ignore it while signed out, otherwise it would require a refresh because there's no way to toggle the switch to go back online while signed out. + if (isOffline && !shouldForceOffline) { + keyStatesToPreserve.push(ONYXKEYS.NETWORK); + } + if (preferredLocale) { + keyStatesToPreserve.push(ONYXKEYS.NVP_PREFERRED_LOCALE) + } + // Clearing storage discards the authToken. This causes a redirect to the SignIn screen - Onyx.clear({ - [ONYXKEYS.NETWORK]: { - isOffline: currentIsOffline, - }, - }) + Onyx.clear(keyStatesToPreserve) .then(() => { - if (preferredLocale) { - Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, preferredLocale); - } if (activeClients && activeClients.length > 0) { Onyx.set(ONYXKEYS.ACTIVE_CLIENTS, activeClients); } - // After signing out, set ourselves as offline if we were offline before logging out and we are not forcing it. - // If we are forcing offline, ignore it while signed out, otherwise it would require a refresh because there's no way to toggle the switch to go back online while signed out. - if (isOffline && !shouldForceOffline) { - Onyx.set(ONYXKEYS.NETWORK, {isOffline}); - } - // `Onyx.clear` reinitialize the Onyx instance with initial values so use `Onyx.merge` instead of `Onyx.set` if (errorMessage) { Onyx.merge(ONYXKEYS.SESSION, {errors: {[DateUtils.getMicroseconds()]: Localize.translateLocal(errorMessage)}}); From 9fef987ecce5ba03fbdf930701f5257a44b0c700 Mon Sep 17 00:00:00 2001 From: Yuwen Memon Date: Tue, 6 Dec 2022 15:58:26 -0800 Subject: [PATCH 03/11] Semi colon --- src/libs/actions/SignInRedirect.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index 77144c96567f..9c6364100277 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -48,7 +48,7 @@ function clearStorageAndRedirect(errorMessage) { keyStatesToPreserve.push(ONYXKEYS.NETWORK); } if (preferredLocale) { - keyStatesToPreserve.push(ONYXKEYS.NVP_PREFERRED_LOCALE) + keyStatesToPreserve.push(ONYXKEYS.NVP_PREFERRED_LOCALE); } // Clearing storage discards the authToken. This causes a redirect to the SignIn screen From e968a1683c5942a492a0d606f84d62ed039683e5 Mon Sep 17 00:00:00 2001 From: Yuwen Memon Date: Thu, 8 Dec 2022 17:26:20 -0800 Subject: [PATCH 04/11] keyStatesToPreserve -> keysToPreserve --- src/libs/actions/SignInRedirect.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index 578a08327708..08d873b19013 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -42,19 +42,19 @@ function clearStorageAndRedirect(errorMessage) { const isOffline = currentIsOffline; const shouldForceOffline = currentShouldForceOffline; - const keyStatesToPreserve = []; + const keysToPreserve = []; // After signing out, set ourselves as offline if we were offline before logging out and we are not forcing it. // If we are forcing offline, ignore it while signed out, otherwise it would require a refresh because there's no way to toggle the switch to go back online while signed out. if (isOffline && !shouldForceOffline) { - keyStatesToPreserve.push(ONYXKEYS.NETWORK); + keysToPreserve.push(ONYXKEYS.NETWORK); } if (preferredLocale) { - keyStatesToPreserve.push(ONYXKEYS.NVP_PREFERRED_LOCALE); + keysToPreserve.push(ONYXKEYS.NVP_PREFERRED_LOCALE); } // Clearing storage discards the authToken. This causes a redirect to the SignIn screen - Onyx.clear(keyStatesToPreserve) + Onyx.clear(keysToPreserve) .then(() => { if (activeClients && activeClients.length > 0) { Onyx.set(ONYXKEYS.ACTIVE_CLIENTS, activeClients); From 748c7a702216d483d82e8eb4e4310392d0a421fa Mon Sep 17 00:00:00 2001 From: Yuwen Memon Date: Fri, 9 Dec 2022 12:28:44 -0800 Subject: [PATCH 05/11] Also preserve active clients --- src/libs/actions/SignInRedirect.js | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index 08d873b19013..40219a72d2aa 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -37,33 +37,29 @@ Onyx.connect({ * @param {String} errorMessage */ function clearStorageAndRedirect(errorMessage) { - const activeClients = currentActiveClients; - const preferredLocale = currentPreferredLocale; - const isOffline = currentIsOffline; - const shouldForceOffline = currentShouldForceOffline; - const keysToPreserve = []; // After signing out, set ourselves as offline if we were offline before logging out and we are not forcing it. // If we are forcing offline, ignore it while signed out, otherwise it would require a refresh because there's no way to toggle the switch to go back online while signed out. - if (isOffline && !shouldForceOffline) { + if (currentIsOffline && !currentShouldForceOffline) { keysToPreserve.push(ONYXKEYS.NETWORK); } - if (preferredLocale) { + if (currentPreferredLocale) { keysToPreserve.push(ONYXKEYS.NVP_PREFERRED_LOCALE); } + if (currentActiveClients && currentActiveClients.length > 0) { + keysToPreserve.push(ONYXKEYS.ACTIVE_CLIENTS); + } // Clearing storage discards the authToken. This causes a redirect to the SignIn screen Onyx.clear(keysToPreserve) .then(() => { - if (activeClients && activeClients.length > 0) { - Onyx.set(ONYXKEYS.ACTIVE_CLIENTS, activeClients); + if (!errorMessage) { + return; } - // `Onyx.clear` reinitialize the Onyx instance with initial values so use `Onyx.merge` instead of `Onyx.set` - if (errorMessage) { - Onyx.merge(ONYXKEYS.SESSION, {errors: {[DateUtils.getMicroseconds()]: Localize.translateLocal(errorMessage)}}); - } + // `Onyx.clear` reinitializes the Onyx instance with initial values so use `Onyx.merge` instead of `Onyx.set` + Onyx.merge(ONYXKEYS.SESSION, {errors: {[DateUtils.getMicroseconds()]: Localize.translateLocal(errorMessage)}}); }); } From f405f1f81ee373266c20c6dd24b6d4482c59ac47 Mon Sep 17 00:00:00 2001 From: Yuwen Memon Date: Fri, 9 Dec 2022 14:39:49 -0800 Subject: [PATCH 06/11] Bump Onyx version --- package-lock.json | 14 +++++++------- package.json | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9e22793766ff..7ff5e08707da 100644 --- a/package-lock.json +++ b/package-lock.json @@ -69,7 +69,7 @@ "react-native-image-picker": "^4.10.2", "react-native-image-size": "git+https://github.com/Expensify/react-native-image-size#6b5ab5110dc3ed554f8eafbc38d7d87c17147972", "react-native-modal": "^13.0.0", - "react-native-onyx": "1.0.29", + "react-native-onyx": "1.0.30", "react-native-pdf": "^6.6.2", "react-native-performance": "^2.0.0", "react-native-permissions": "^3.0.1", @@ -35456,9 +35456,9 @@ } }, "node_modules/react-native-onyx": { - "version": "1.0.29", - "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-1.0.29.tgz", - "integrity": "sha512-Zg7v6bimkRPqaAM5C/CqMswSObPGX+2BK2Gr7r4P1htSMjNlvn+fZ9gD4mwJBPgnoEUqMdFGLUshksHbE+GSMg==", + "version": "1.0.30", + "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-1.0.30.tgz", + "integrity": "sha512-WNNIUfPIlUc/a9rKPUSt2m/YMCK0SL0rJTpvCbMPvAUooB2Unpm2wlI3rS0tU9eNvQFGv9Si49VyIelgoJI5dw==", "dependencies": { "ascii-table": "0.0.9", "fast-equals": "^4.0.3", @@ -69820,9 +69820,9 @@ } }, "react-native-onyx": { - "version": "1.0.29", - "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-1.0.29.tgz", - "integrity": "sha512-Zg7v6bimkRPqaAM5C/CqMswSObPGX+2BK2Gr7r4P1htSMjNlvn+fZ9gD4mwJBPgnoEUqMdFGLUshksHbE+GSMg==", + "version": "1.0.30", + "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-1.0.30.tgz", + "integrity": "sha512-WNNIUfPIlUc/a9rKPUSt2m/YMCK0SL0rJTpvCbMPvAUooB2Unpm2wlI3rS0tU9eNvQFGv9Si49VyIelgoJI5dw==", "requires": { "ascii-table": "0.0.9", "fast-equals": "^4.0.3", diff --git a/package.json b/package.json index 400c52db75aa..69e010e851d8 100644 --- a/package.json +++ b/package.json @@ -100,7 +100,7 @@ "react-native-image-picker": "^4.10.2", "react-native-image-size": "git+https://github.com/Expensify/react-native-image-size#6b5ab5110dc3ed554f8eafbc38d7d87c17147972", "react-native-modal": "^13.0.0", - "react-native-onyx": "1.0.29", + "react-native-onyx": "1.0.30", "react-native-pdf": "^6.6.2", "react-native-performance": "^2.0.0", "react-native-permissions": "^3.0.1", From ab14ca4a3871d81495816cc327c19b11155a1124 Mon Sep 17 00:00:00 2001 From: Yuwen Memon Date: Fri, 9 Dec 2022 15:41:35 -0800 Subject: [PATCH 07/11] Make sure Network test uses Onyx call that would set keysToPreserve as default param --- tests/ui/UnreadIndicatorsTest.js | 2 +- tests/unit/NetworkTest.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ui/UnreadIndicatorsTest.js b/tests/ui/UnreadIndicatorsTest.js index cb777bcd61e2..9883c1bf04fc 100644 --- a/tests/ui/UnreadIndicatorsTest.js +++ b/tests/ui/UnreadIndicatorsTest.js @@ -184,7 +184,7 @@ function signInAndGetAppWithUnreadChat() { } describe('Unread Indicators', () => { - afterEach(Onyx.clear); + afterEach(() => Onyx.clear()); it('Display bold in the LHN for unread chat and new line indicator above the chat message when we navigate to it', () => { let renderedApp; diff --git a/tests/unit/NetworkTest.js b/tests/unit/NetworkTest.js index f0cb04f7cd5f..6b725d94626b 100644 --- a/tests/unit/NetworkTest.js +++ b/tests/unit/NetworkTest.js @@ -36,7 +36,7 @@ beforeEach(() => { // Wait for any Log command to finish and Onyx to fully clear jest.advanceTimersByTime(CONST.NETWORK.PROCESS_REQUEST_DELAY_MS); return waitForPromisesToResolve() - .then(Onyx.clear) + .then(() => Onyx.clear()) .then(waitForPromisesToResolve); }); From 3a49c3fea24c3f1182ff6ce574ef13c187872dc5 Mon Sep 17 00:00:00 2001 From: Yuwen Memon Date: Mon, 12 Dec 2022 11:37:32 -0800 Subject: [PATCH 08/11] Add comment --- src/libs/actions/SignInRedirect.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index 40219a72d2aa..e0d9bf2deea9 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -37,6 +37,9 @@ Onyx.connect({ * @param {String} errorMessage */ function clearStorageAndRedirect(errorMessage) { + // Under certain conditions, there are key-values we'd like to keep in storage even when a user is logged out. + // We pass these into the clear() method in order to avoid having to reset them on a delayed tick and getting + // flashes of unwanted default state. const keysToPreserve = []; // After signing out, set ourselves as offline if we were offline before logging out and we are not forcing it. From 390ea5fff03dd4233e16406084b6b956f368781c Mon Sep 17 00:00:00 2001 From: Yuwen Memon Date: Tue, 13 Dec 2022 09:35:20 -0800 Subject: [PATCH 09/11] Preserve certain keystates always --- src/libs/actions/SignInRedirect.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index e0d9bf2deea9..9bcd80b1c63c 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -41,18 +41,14 @@ function clearStorageAndRedirect(errorMessage) { // We pass these into the clear() method in order to avoid having to reset them on a delayed tick and getting // flashes of unwanted default state. const keysToPreserve = []; + keysToPreserve.push(ONYXKEYS.NVP_PREFERRED_LOCALE); + keysToPreserve.push(ONYXKEYS.ACTIVE_CLIENTS); // After signing out, set ourselves as offline if we were offline before logging out and we are not forcing it. // If we are forcing offline, ignore it while signed out, otherwise it would require a refresh because there's no way to toggle the switch to go back online while signed out. if (currentIsOffline && !currentShouldForceOffline) { keysToPreserve.push(ONYXKEYS.NETWORK); } - if (currentPreferredLocale) { - keysToPreserve.push(ONYXKEYS.NVP_PREFERRED_LOCALE); - } - if (currentActiveClients && currentActiveClients.length > 0) { - keysToPreserve.push(ONYXKEYS.ACTIVE_CLIENTS); - } // Clearing storage discards the authToken. This causes a redirect to the SignIn screen Onyx.clear(keysToPreserve) From 7d0a7e79fc97fb3dffa46a707a7aa8e09779a6eb Mon Sep 17 00:00:00 2001 From: Yuwen Memon Date: Tue, 13 Dec 2022 09:40:10 -0800 Subject: [PATCH 10/11] Remove unneeded ONYX connections --- src/libs/actions/SignInRedirect.js | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/libs/actions/SignInRedirect.js b/src/libs/actions/SignInRedirect.js index 9bcd80b1c63c..0ae54436e15b 100644 --- a/src/libs/actions/SignInRedirect.js +++ b/src/libs/actions/SignInRedirect.js @@ -6,20 +6,6 @@ import * as Localize from '../Localize'; import * as PersistedRequests from './PersistedRequests'; import NetworkConnection from '../NetworkConnection'; -let currentActiveClients; -Onyx.connect({ - key: ONYXKEYS.ACTIVE_CLIENTS, - callback: (val) => { - currentActiveClients = !val ? [] : val; - }, -}); - -let currentPreferredLocale; -Onyx.connect({ - key: ONYXKEYS.NVP_PREFERRED_LOCALE, - callback: val => currentPreferredLocale = val, -}); - let currentIsOffline; let currentShouldForceOffline; Onyx.connect({ From 6227a80e009c8c2f49f883fbff8f0b0284a03b45 Mon Sep 17 00:00:00 2001 From: Yuwen Memon Date: Wed, 14 Dec 2022 12:58:22 -0800 Subject: [PATCH 11/11] Upgrade to onyx version 1.0.31 --- package-lock.json | 14 +++++++------- package.json | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 622c0c92fa5a..b77e514e11b8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -69,7 +69,7 @@ "react-native-image-picker": "^4.10.2", "react-native-image-size": "git+https://github.com/Expensify/react-native-image-size#6b5ab5110dc3ed554f8eafbc38d7d87c17147972", "react-native-modal": "^13.0.0", - "react-native-onyx": "1.0.30", + "react-native-onyx": "1.0.31", "react-native-pdf": "^6.6.2", "react-native-performance": "^2.0.0", "react-native-permissions": "^3.0.1", @@ -35456,9 +35456,9 @@ } }, "node_modules/react-native-onyx": { - "version": "1.0.30", - "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-1.0.30.tgz", - "integrity": "sha512-WNNIUfPIlUc/a9rKPUSt2m/YMCK0SL0rJTpvCbMPvAUooB2Unpm2wlI3rS0tU9eNvQFGv9Si49VyIelgoJI5dw==", + "version": "1.0.31", + "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-1.0.31.tgz", + "integrity": "sha512-WaX6dD6II7sZoiiNnifCh7X+V2AjwOtMx5mmrGYBuspPUpLOIj3Lt3GM66YRe5T9d5PmyoKUl0ULuYip/7uBTQ==", "dependencies": { "ascii-table": "0.0.9", "fast-equals": "^4.0.3", @@ -69828,9 +69828,9 @@ } }, "react-native-onyx": { - "version": "1.0.30", - "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-1.0.30.tgz", - "integrity": "sha512-WNNIUfPIlUc/a9rKPUSt2m/YMCK0SL0rJTpvCbMPvAUooB2Unpm2wlI3rS0tU9eNvQFGv9Si49VyIelgoJI5dw==", + "version": "1.0.31", + "resolved": "https://registry.npmjs.org/react-native-onyx/-/react-native-onyx-1.0.31.tgz", + "integrity": "sha512-WaX6dD6II7sZoiiNnifCh7X+V2AjwOtMx5mmrGYBuspPUpLOIj3Lt3GM66YRe5T9d5PmyoKUl0ULuYip/7uBTQ==", "requires": { "ascii-table": "0.0.9", "fast-equals": "^4.0.3", diff --git a/package.json b/package.json index 3ca6619d099a..4c3c624dc868 100644 --- a/package.json +++ b/package.json @@ -100,7 +100,7 @@ "react-native-image-picker": "^4.10.2", "react-native-image-size": "git+https://github.com/Expensify/react-native-image-size#6b5ab5110dc3ed554f8eafbc38d7d87c17147972", "react-native-modal": "^13.0.0", - "react-native-onyx": "1.0.30", + "react-native-onyx": "1.0.31", "react-native-pdf": "^6.6.2", "react-native-performance": "^2.0.0", "react-native-permissions": "^3.0.1",