-
Notifications
You must be signed in to change notification settings - Fork 26
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
Add an AppHighlightAlert targeting coachco2 #2022
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/components/AppHighlightAlert/AppHighlightAlertWrapper.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import React, { useEffect, useState } from 'react' | ||
|
||
import { getBackupAppHighlightAlert } from 'components/AppHighlightAlert/BackupAppHighlightAlert' | ||
import { getGeolocationTrackingAppHighlightAlert } from 'components/AppHighlightAlert/GeolocationTrackingAppHighlightAlert' | ||
|
||
const AppHighlightAlertWrapper = ({ apps }) => { | ||
const [appHighlightAlerts, setAppHighlightAlerts] = useState([]) | ||
|
||
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 | ||
} | ||
|
||
setAppHighlightAlerts(availableAppHighlightAlerts) | ||
}, []) | ||
|
||
useEffect(() => { | ||
appHighlightAlerts.forEach(component => { | ||
if (component.displayed) { | ||
component.onDisplayed() | ||
} else { | ||
component.onNotDisplayed() | ||
} | ||
}) | ||
}, [appHighlightAlerts]) | ||
|
||
return ( | ||
<> | ||
{appHighlightAlerts.map(({ Component, name, displayed }) => ( | ||
<Component key={name} apps={apps} displayed={displayed} /> | ||
))} | ||
</> | ||
) | ||
} | ||
|
||
export default AppHighlightAlertWrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/components/AppHighlightAlert/GeolocationTrackingAppHighlightAlert.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import React, { useState } from 'react' | ||
|
||
import flag from 'cozy-flags' | ||
import { isFlagshipApp } from 'cozy-device-helper' | ||
import { useI18n } from 'cozy-ui/transpiled/react/providers/I18n' | ||
|
||
import AppHighlightAlert from 'components/AppHighlightAlert/AppHighlightAlert' | ||
|
||
const APP_START_COUNT_KEY = | ||
'GeolocationTrackingAppHighlightAlert__appStartCount' | ||
|
||
const DISABLED_COUNT_VALUE = -1 | ||
|
||
const isAvailable = () => { | ||
const bikegoalSettings = flag('coachco2.bikegoal.settings') | ||
|
||
return ( | ||
isFlagshipApp() && | ||
flag('home.push.coachco2.opencount') && | ||
flag('home.push.coachco2.opencount') >= 0 && | ||
(!bikegoalSettings || | ||
bikegoalSettings.sourceId === null || | ||
(bikegoalSettings.sourceId !== null && flag('coachco2.bikegoal.enabled'))) | ||
) | ||
} | ||
|
||
const isDisplayable = () => { | ||
const appStartCount = | ||
parseInt(localStorage.getItem(APP_START_COUNT_KEY), 10) || 0 | ||
|
||
return appStartCount >= flag('home.push.coachco2.opencount') - 1 | ||
} | ||
|
||
export const getGeolocationTrackingAppHighlightAlert = () => { | ||
return { | ||
name: 'GeolocationTrackingAppHighlightAlert', | ||
Component: GeolocationTrackingAppHighlightAlert, | ||
available: isAvailable(), | ||
displayable: isDisplayable(), | ||
onNotDisplayed: onNotDisplayed, | ||
onDisplayed: onDisplayed | ||
} | ||
} | ||
|
||
const onNotDisplayed = () => { | ||
const appStartCount = parseInt(localStorage.getItem(APP_START_COUNT_KEY), 10) | ||
|
||
let newAppStartCount | ||
|
||
if (appStartCount === DISABLED_COUNT_VALUE) { | ||
return | ||
} | ||
|
||
if (isNaN(appStartCount)) { | ||
newAppStartCount = 1 | ||
} else { | ||
newAppStartCount = appStartCount + 1 | ||
} | ||
|
||
localStorage.setItem(APP_START_COUNT_KEY, newAppStartCount.toString()) | ||
} | ||
|
||
const onDisplayed = () => { | ||
localStorage.setItem(APP_START_COUNT_KEY, DISABLED_COUNT_VALUE) | ||
} | ||
|
||
const getAlertDescription = t => { | ||
const bikegoalSettings = flag('coachco2.bikegoal.settings') | ||
|
||
if (bikegoalSettings?.sourceId) { | ||
if (bikegoalSettings.sourceId === 'employer') { | ||
return t( | ||
'appHighlightAlert.geolocationTracking.bikegoalSourceEmployerDescription', | ||
{ | ||
sourceType: bikegoalSettings.sourceType, | ||
sourceIdentity: bikegoalSettings.sourceIdentity | ||
} | ||
) | ||
} else { | ||
return t( | ||
'appHighlightAlert.geolocationTracking.bikegoalSourceDefaultDescription', | ||
{ | ||
sourceType: bikegoalSettings.sourceType, | ||
sourceIdentity: bikegoalSettings.sourceIdentity | ||
} | ||
) | ||
} | ||
} | ||
|
||
return t('appHighlightAlert.geolocationTracking.defaultDescription') | ||
} | ||
|
||
export const GeolocationTrackingAppHighlightAlert = ({ apps, displayed }) => { | ||
const { t } = useI18n() | ||
const [ | ||
shouldShowGeolocationTrackingAppHighlightAlert, | ||
setShouldShowGeolocationTrackingAppHighlightAlert | ||
] = useState(displayed) | ||
|
||
const onClose = () => { | ||
setShouldShowGeolocationTrackingAppHighlightAlert(false) | ||
} | ||
|
||
if (!shouldShowGeolocationTrackingAppHighlightAlert) { | ||
return null | ||
} | ||
|
||
const description = getAlertDescription(t) | ||
|
||
return ( | ||
<AppHighlightAlert | ||
apps={apps} | ||
appToHighlightSlug="coachco2" | ||
onClose={onClose} | ||
description={description} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the first in that array has the priority, and so on?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. We did not chose any way to set a priority for the moment.