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

App highlight alert could never show if related app was not installed #2051

Merged
merged 3 commits into from
Dec 18, 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
26 changes: 8 additions & 18 deletions src/components/AppHighlightAlert/AppHighlightAlertWrapper.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useEffect, useState } from 'react'

import { getBackupAppHighlightAlert } from 'components/AppHighlightAlert/BackupAppHighlightAlert'
import { getGeolocationTrackingAppHighlightAlert } from 'components/AppHighlightAlert/GeolocationTrackingAppHighlightAlert'
import { getAvailableAppHighlightAlerts } from 'components/AppHighlightAlert/helpers'
import { useClient } from 'cozy-client'

const AppHighlightAlertWrapper = ({ apps }) => {
Expand All @@ -10,27 +9,18 @@ const AppHighlightAlertWrapper = ({ apps }) => {

useEffect(() => {
const getAppHighlightAlerts = async () => {
const appHighlightAlerts = [
getBackupAppHighlightAlert(),
await getGeolocationTrackingAppHighlightAlert(client)
]

const availableAppHighlightAlerts = appHighlightAlerts.filter(
status => status.available
const availableAppHighlightAlerts = await getAvailableAppHighlightAlerts(
client,
apps
)

const selectedIndex = availableAppHighlightAlerts.findIndex(
status => status.displayable
)
if (selectedIndex !== -1) {
availableAppHighlightAlerts[selectedIndex].displayed = true
}

setAppHighlightAlerts(availableAppHighlightAlerts)
}

getAppHighlightAlerts()
}, [client])
if (apps && appHighlightAlerts.length === 0) {
getAppHighlightAlerts()
}
}, [client, apps, appHighlightAlerts.length])

useEffect(() => {
appHighlightAlerts.forEach(component => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ const APP_START_COUNT_KEY = 'BackupAppHighlightAlert__appStartCount'
const MAX_COUNT_VALUE = 2
const DISABLED_COUNT_VALUE = -1

const isAvailable = () => {
const isAvailable = installedApps => {
return (
installedApps.find(app => app.slug === 'photos') &&
isFlagshipApp() &&
flag('flagship.backup.enabled') &&
flag('flagship.backup.homeHighlightEnabled')
Expand All @@ -26,11 +27,11 @@ const isDisplayable = () => {
return appStartCount >= MAX_COUNT_VALUE - 1
}

export const getBackupAppHighlightAlert = () => {
export const getBackupAppHighlightAlert = installedApps => {
return {
name: 'BackupAppHighlightAlert',
Component: BackupAppHighlightAlert,
available: isAvailable(),
available: isAvailable(installedApps),
displayable: isDisplayable(),
onNotDisplayed: onNotDisplayed,
onDisplayed: onDisplayed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ const hasNoTimeseriesGeojson = async client => {
return timeseries.length === 0
}

const isAvailable = async client => {
const isAvailable = async (client, installedApps) => {
const bikegoalSettings = flag('coachco2.bikegoal.settings')

return (
installedApps.find(app => app.slug === 'coachco2') &&
flag('home.push.coachco2.opencount') &&
flag('home.push.coachco2.opencount') >= 0 &&
(!bikegoalSettings ||
Expand All @@ -41,11 +42,14 @@ const isDisplayable = () => {
return appStartCount >= flag('home.push.coachco2.opencount') - 1
}

export const getGeolocationTrackingAppHighlightAlert = async client => {
export const getGeolocationTrackingAppHighlightAlert = async (
client,
installedApps
) => {
return {
name: 'GeolocationTrackingAppHighlightAlert',
Component: GeolocationTrackingAppHighlightAlert,
available: await isAvailable(client),
available: await isAvailable(client, installedApps),
displayable: isDisplayable(),
onNotDisplayed: onNotDisplayed,
onDisplayed: onDisplayed
Expand Down
22 changes: 22 additions & 0 deletions src/components/AppHighlightAlert/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { getBackupAppHighlightAlert } from 'components/AppHighlightAlert/BackupAppHighlightAlert'
import { getGeolocationTrackingAppHighlightAlert } from 'components/AppHighlightAlert/GeolocationTrackingAppHighlightAlert'

export const getAvailableAppHighlightAlerts = async (client, installedApps) => {
const appHighlightAlerts = [
getBackupAppHighlightAlert(installedApps),
await getGeolocationTrackingAppHighlightAlert(client, installedApps)
]

const availableAppHighlightAlerts = appHighlightAlerts.filter(
status => status.available
)

const selectedIndex = availableAppHighlightAlerts.findIndex(
status => status.displayable
)
if (selectedIndex !== -1) {
availableAppHighlightAlerts[selectedIndex].displayed = true
}

return availableAppHighlightAlerts
}
106 changes: 106 additions & 0 deletions src/components/AppHighlightAlert/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { getAvailableAppHighlightAlerts } from 'components/AppHighlightAlert/helpers'
import { getBackupAppHighlightAlert } from 'components/AppHighlightAlert/BackupAppHighlightAlert'
import { getGeolocationTrackingAppHighlightAlert } from 'components/AppHighlightAlert/GeolocationTrackingAppHighlightAlert'

jest.mock('components/AppHighlightAlert/BackupAppHighlightAlert')
jest.mock('components/AppHighlightAlert/GeolocationTrackingAppHighlightAlert')

describe('getAvailableAppHighlightAlerts', () => {
it('should set the only displayable alert as displayed if only one displayable alert found', async () => {
getBackupAppHighlightAlert.mockReturnValue({
name: 'BackupAppHighlightAlert',
available: false,
displayable: false
})
getGeolocationTrackingAppHighlightAlert.mockReturnValue({
name: 'GeolocationTrackingAppHighlightAlert',
available: true,
displayable: true
})

const availableAppHighlightAlerts = await getAvailableAppHighlightAlerts()

expect(availableAppHighlightAlerts).toEqual([
{
name: 'GeolocationTrackingAppHighlightAlert',
available: true,
displayable: true,
displayed: true
}
])
})

it('should set the first displayable alert as displayed if multiple displayable alerts found', async () => {
getBackupAppHighlightAlert.mockReturnValue({
name: 'BackupAppHighlightAlert',
available: true,
displayable: true
})
getGeolocationTrackingAppHighlightAlert.mockReturnValue({
name: 'GeolocationTrackingAppHighlightAlert',
available: true,
displayable: true
})

const availableAppHighlightAlerts = await getAvailableAppHighlightAlerts()

expect(availableAppHighlightAlerts).toEqual([
{
name: 'BackupAppHighlightAlert',
available: true,
displayable: true,
displayed: true
},
{
name: 'GeolocationTrackingAppHighlightAlert',
available: true,
displayable: true
}
])
})

it('should not return not available alerts', async () => {
getBackupAppHighlightAlert.mockReturnValue({
name: 'BackupAppHighlightAlert',
available: false,
displayable: false
})
getGeolocationTrackingAppHighlightAlert.mockReturnValue({
name: 'GeolocationTrackingAppHighlightAlert',
available: false,
displayable: false
})

const availableAppHighlightAlerts = await getAvailableAppHighlightAlerts()

expect(availableAppHighlightAlerts).toEqual([])
})

it('should do nothing with not displayable alerts', async () => {
getBackupAppHighlightAlert.mockReturnValue({
name: 'BackupAppHighlightAlert',
available: true,
displayable: false
})
getGeolocationTrackingAppHighlightAlert.mockReturnValue({
name: 'GeolocationTrackingAppHighlightAlert',
available: true,
displayable: false
})

const availableAppHighlightAlerts = await getAvailableAppHighlightAlerts()

expect(availableAppHighlightAlerts).toEqual([
{
name: 'BackupAppHighlightAlert',
available: true,
displayable: false
},
{
name: 'GeolocationTrackingAppHighlightAlert',
available: true,
displayable: false
}
])
})
})
Loading