Skip to content

Commit

Permalink
feat: Display coachco2 CTA only if no data in coachco2
Browse files Browse the repository at this point in the history
  • Loading branch information
zatteo committed Nov 8, 2023
1 parent 0492ee8 commit 1a32aa1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
38 changes: 22 additions & 16 deletions src/components/AppHighlightAlert/AppHighlightAlertWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -19,7 +29,8 @@ const isAvailable = () => {
(!bikegoalSettings ||
bikegoalSettings.sourceOffer === null ||
(bikegoalSettings.sourceOffer !== null &&
flag('coachco2.bikegoal.enabled')))
flag('coachco2.bikegoal.enabled'))) &&
(await hasAtLeastOneTimeseriesGeojson(client))
)
}

Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})

0 comments on commit 1a32aa1

Please sign in to comment.