From cc223a1025f7ced39a1b3fef2022506f459b482a Mon Sep 17 00:00:00 2001 From: manubo Date: Thu, 3 Oct 2019 14:56:59 +0200 Subject: [PATCH] Close TimerView if timer stopped for current service --- src/js/background.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/js/background.js b/src/js/background.js index 1e59c51..7a72a09 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -3,10 +3,15 @@ import ApiClient from "api/Client" import { isChrome, getCurrentTab, getSettings, isBrowserTab } from "utils/browser" import { BackgroundMessenger } from "utils/messaging" import { tabUpdated, settingsChanged, togglePopup, openPopup } from "utils/messageHandlers" +import { isNil } from "lodash" const messenger = new BackgroundMessenger() -function resetBubble({ tab, apiClient, service }, closePopup = true) { +function timerStoppedForCurrentService(service, timedActivity) { + return timedActivity.service_id && timedActivity.service_id === service?.id +} + +function resetBubble({ tab, apiClient, service, timedActivity }) { apiClient .activitiesStatus(service) .then(({ data }) => { @@ -20,7 +25,7 @@ function resetBubble({ tab, apiClient, service }, closePopup = true) { }) }) .then(() => { - if (closePopup) { + if (isNil(timedActivity) || timerStoppedForCurrentService(service, timedActivity)) { messenger.postMessage(tab, { type: "closePopup" }) } else { openPopup(tab, { service, messenger }) @@ -73,7 +78,7 @@ chrome.runtime.onMessage.addListener(action => { const apiClient = new ApiClient(settings) apiClient .stopTimer(timedActivity) - .then(() => resetBubble({ tab, apiClient, service }, false)) + .then(() => resetBubble({ tab, apiClient, service, timedActivity })) .catch(() => null) }) })