From fd83ca07e5544a1d7b4a91e4c5f68a7f5f2189a6 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 1 Jul 2022 18:59:47 -0700 Subject: Fix push token duplication --- modern/src/common/components/NativeInterface.js | 34 +++++++++++++------------ 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'modern') diff --git a/modern/src/common/components/NativeInterface.js b/modern/src/common/components/NativeInterface.js index aee4c042..b36a3d26 100644 --- a/modern/src/common/components/NativeInterface.js +++ b/modern/src/common/components/NativeInterface.js @@ -36,24 +36,26 @@ const NativeInterface = () => { setToken(null); const tokens = user.attributes.notificationTokens?.split(',') || []; - const updatedUser = { - ...user, - attributes: { - ...user.attributes, - notificationTokens: [...tokens.slice(-2), token].join(','), - }, - }; + if (!tokens.includes(token)) { + const updatedUser = { + ...user, + attributes: { + ...user.attributes, + notificationTokens: [...tokens.slice(-2), token].join(','), + }, + }; - const response = await fetch(`/api/users/${user.id}`, { - method: 'PUT', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(updatedUser), - }); + const response = await fetch(`/api/users/${user.id}`, { + method: 'PUT', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(updatedUser), + }); - if (response.ok) { - dispatch(sessionActions.updateUser(await response.json())); - } else { - throw Error(await response.text()); + if (response.ok) { + dispatch(sessionActions.updateUser(await response.json())); + } else { + throw Error(await response.text()); + } } } }, [user, token, setToken]); -- cgit v1.2.3