diff --git a/src/components/AppHighlightAlert/AppHighlightAlertWrapper.jsx b/src/components/AppHighlightAlert/AppHighlightAlertWrapper.jsx index ae335d255e..84e976fdb4 100644 --- a/src/components/AppHighlightAlert/AppHighlightAlertWrapper.jsx +++ b/src/components/AppHighlightAlert/AppHighlightAlertWrapper.jsx @@ -2,29 +2,35 @@ import React, { useEffect, useState } from 'react' import { getBackupAppHighlightAlert } from 'components/AppHighlightAlert/BackupAppHighlightAlert' import { getGeolocationTrackingAppHighlightAlert } from 'components/AppHighlightAlert/GeolocationTrackingAppHighlightAlert' +import { useClient } from 'cozy-client' const AppHighlightAlertWrapper = ({ apps }) => { const [appHighlightAlerts, setAppHighlightAlerts] = useState([]) + const client = useClient() useEffect(() => { - const appHighlightAlerts = [ - getBackupAppHighlightAlert(), - getGeolocationTrackingAppHighlightAlert() - ] - - const availableAppHighlightAlerts = appHighlightAlerts.filter( - status => status.available - ) - - const selectedIndex = availableAppHighlightAlerts.findIndex( - status => status.displayable - ) - if (selectedIndex !== -1) { - availableAppHighlightAlerts[selectedIndex].displayed = true + const getAppHighlightAlerts = async () => { + const appHighlightAlerts = [ + getBackupAppHighlightAlert(), + await getGeolocationTrackingAppHighlightAlert(client) + ] + + const availableAppHighlightAlerts = appHighlightAlerts.filter( + status => status.available + ) + + const selectedIndex = availableAppHighlightAlerts.findIndex( + status => status.displayable + ) + if (selectedIndex !== -1) { + availableAppHighlightAlerts[selectedIndex].displayed = true + } + + setAppHighlightAlerts(availableAppHighlightAlerts) } - setAppHighlightAlerts(availableAppHighlightAlerts) - }, []) + getAppHighlightAlerts() + }, [client]) useEffect(() => { appHighlightAlerts.forEach(component => { diff --git a/src/components/AppHighlightAlert/GeolocationTrackingAppHighlightAlert.jsx b/src/components/AppHighlightAlert/GeolocationTrackingAppHighlightAlert.jsx index 604e5c8398..66b56a9476 100644 --- a/src/components/AppHighlightAlert/GeolocationTrackingAppHighlightAlert.jsx +++ b/src/components/AppHighlightAlert/GeolocationTrackingAppHighlightAlert.jsx @@ -4,13 +4,23 @@ import flag from 'cozy-flags' import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n' import AppHighlightAlert from 'components/AppHighlightAlert/AppHighlightAlert' +import { buildExistingTimeseriesGeojsonQuery } from 'queries' const APP_START_COUNT_KEY = 'GeolocationTrackingAppHighlightAlert__appStartCount' const DISABLED_COUNT_VALUE = -1 -const isAvailable = () => { +const hasAtLeastOneTimeseriesGeojson = async client => { + const existingTimeseriesGeojsonQuery = buildExistingTimeseriesGeojsonQuery() + const { data: timeseries } = await client.fetchQueryAndGetFromState( + existingTimeseriesGeojsonQuery + ) + + return timeseries.length >= 1 +} + +const isAvailable = async client => { const bikegoalSettings = flag('coachco2.bikegoal.settings') return ( @@ -19,7 +29,8 @@ const isAvailable = () => { (!bikegoalSettings || bikegoalSettings.sourceOffer === null || (bikegoalSettings.sourceOffer !== null && - flag('coachco2.bikegoal.enabled'))) + flag('coachco2.bikegoal.enabled'))) && + (await hasAtLeastOneTimeseriesGeojson(client)) ) } @@ -30,11 +41,11 @@ const isDisplayable = () => { return appStartCount >= flag('home.push.coachco2.opencount') - 1 } -export const getGeolocationTrackingAppHighlightAlert = () => { +export const getGeolocationTrackingAppHighlightAlert = async client => { return { name: 'GeolocationTrackingAppHighlightAlert', Component: GeolocationTrackingAppHighlightAlert, - available: isAvailable(), + available: await isAvailable(client), displayable: isDisplayable(), onNotDisplayed: onNotDisplayed, onDisplayed: onDisplayed diff --git a/src/queries.js b/src/queries.js index 80b6242790..7711389fcd 100644 --- a/src/queries.js +++ b/src/queries.js @@ -115,3 +115,14 @@ export const buildContextQuery = () => ({ singleDocData: true } }) + +export const buildExistingTimeseriesGeojsonQuery = () => ({ + definition: Q('io.cozy.timeseries.geojson') + .where({ _id: { $gt: null } }) + .select(['_id']) + .limitBy(1), + options: { + as: 'io.cozy.timeseries.geojson/existing-timeseries-geojson', + fetchPolicy: CozyClient.fetchPolicies.olderThan(60 * 60 * 24 * 365 * 1000) + } +})