Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Display coachco2 CTA only if no data in coachco2 #2035

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
})
Loading