From 59872056eb45ca0b7977f10e7652a8741b928ad6 Mon Sep 17 00:00:00 2001 From: Sean Yesmunt Date: Wed, 18 Sep 2019 14:41:20 -0400 Subject: [PATCH] fix: ensure reward claim happens after file view request --- src/ui/analytics.js | 40 +++++++++++++++------------- src/ui/component/fileViewer/view.jsx | 11 ++++---- src/ui/redux/actions/app.js | 2 +- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/ui/analytics.js b/src/ui/analytics.js index 64e60a86c..add8400c9 100644 --- a/src/ui/analytics.js +++ b/src/ui/analytics.js @@ -14,7 +14,7 @@ type Analytics = { pageView: string => void, setUser: Object => void, toggle: (boolean, ?boolean) => void, - apiLogView: (string, string, string, ?number, ?() => void) => void, + apiLogView: (string, string, string, ?number, ?() => void) => Promise, apiLogPublish: () => void, tagFollowEvent: (string, boolean, string) => void, emailProvidedEvent: () => void, @@ -49,25 +49,29 @@ const analytics: Analytics = { // @endif }, apiLogView: (uri, outpoint, claimId, timeToStart) => { - if (analyticsEnabled && (isProduction || devInternalApis)) { - const params: { - uri: string, - outpoint: string, - claim_id: string, - time_to_start?: number, - } = { - uri, - outpoint, - claim_id: claimId, - }; + return new Promise((resolve, reject) => { + if (analyticsEnabled && (isProduction || devInternalApis)) { + const params: { + uri: string, + outpoint: string, + claim_id: string, + time_to_start?: number, + } = { + uri, + outpoint, + claim_id: claimId, + }; - // lbry.tv streams from AWS so we don't care about the time to start - if (timeToStart && !IS_WEB) { - params.time_to_start = timeToStart; + // lbry.tv streams from AWS so we don't care about the time to start + if (timeToStart && !IS_WEB) { + params.time_to_start = timeToStart; + } + + resolve(Lbryio.call('file', 'view', params)); + } else { + resolve(); } - - Lbryio.call('file', 'view', params); - } + }); }, apiLogSearch: () => { if (analyticsEnabled && isProduction) { diff --git a/src/ui/component/fileViewer/view.jsx b/src/ui/component/fileViewer/view.jsx index a635c60b4..29f85ef2a 100644 --- a/src/ui/component/fileViewer/view.jsx +++ b/src/ui/component/fileViewer/view.jsx @@ -28,7 +28,7 @@ type Props = { title: ?string, floatingPlayerEnabled: boolean, clearPlayingUri: () => void, - triggerAnalyticsView: (string, number) => void, + triggerAnalyticsView: (string, number) => Promise, claimRewards: () => void, }; @@ -74,10 +74,11 @@ export default function FileViewer(props: Props) { useEffect(() => { if (playTime && isReadyToPlay && !hasRecordedView) { const timeToStart = Date.now() - playTime; - triggerAnalyticsView(uri, timeToStart); - claimRewards(); - setPlayTime(null); - setHasRecordedView(false); + triggerAnalyticsView(uri, timeToStart).then(() => { + claimRewards(); + setHasRecordedView(false); + setPlayTime(null); + }); } }, [setPlayTime, triggerAnalyticsView, isReadyToPlay, hasRecordedView, playTime, uri, claimRewards]); diff --git a/src/ui/redux/actions/app.js b/src/ui/redux/actions/app.js index ee5114926..83e8dbbc3 100644 --- a/src/ui/redux/actions/app.js +++ b/src/ui/redux/actions/app.js @@ -430,6 +430,6 @@ export function doAnalyticsView(uri, timeToStart) { return; } - analytics.apiLogView(uri, outpoint, claimId, timeToStart); + return analytics.apiLogView(uri, outpoint, claimId, timeToStart); }; }