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

[VO-1064 & VO-1067] feat: Take into account feedback on announcements #2184

Merged
merged 3 commits into from
Sep 18, 2024
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
29 changes: 23 additions & 6 deletions src/components/Announcements/Announcements.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useState } from 'react'
import React, { FC, useEffect, useState } from 'react'
import { differenceInHours } from 'date-fns'

import flag from 'cozy-flags'
Expand All @@ -11,20 +11,37 @@ import { useAnnouncementsSettings } from 'hooks/useAnnouncementsSettings'
const Announcements: FC = () => {
const config = flag<AnnouncementsConfigFlag>('home.announcements')
const [hasBeenDismissed, setBeenDismissed] = useState(false)
const { values, save } = useAnnouncementsSettings()
const { fetchStatus, values, save } = useAnnouncementsSettings()
const [hasBeenActivated, setBeenActivated] = useState(false)

useEffect(() => {
if (
!values.firstActivatedAt &&
!hasBeenActivated &&
fetchStatus === 'loaded'
) {
save({
firstActivatedAt: new Date().toISOString()
})
setBeenActivated(true)
}
}, [hasBeenActivated, save, values.firstActivatedAt, fetchStatus])

const handleDismiss = (): void => {
save({
dismissedAt: new Date().toISOString()
})
setBeenDismissed(true)
}

const hasBeenActivatedForMoreThanAHour = values.firstActivatedAt
? differenceInHours(new Date(), Date.parse(values.firstActivatedAt)) >= 1
: false
const moreThan = config?.delayAfterDismiss ?? 24
const hasBeenDismissedForMoreThan = values.dismissedAt
? differenceInHours(new Date(), Date.parse(values.dismissedAt)) >= moreThan
: true
const canBeDisplayed = !hasBeenDismissed && hasBeenDismissedForMoreThan
const canBeDisplayed =
!hasBeenDismissed &&
hasBeenDismissedForMoreThan &&
hasBeenActivatedForMoreThanAHour
const announcements = useAnnouncements({
canBeDisplayed
})
Expand Down
20 changes: 18 additions & 2 deletions src/components/Announcements/AnnouncementsDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,29 @@ const AnnouncementsDialog: FC<AnnouncementsDialogProps> = ({
setActiveStep(index)
}

const handleLast = (): void => {
const uuid = announcements[activeStep].attributes.uuid
save({
dismissedAt: new Date().toISOString(),
seen: [...(values?.seen ?? []), uuid]
})
onDismiss()
}

const handleDismiss = (): void => {
save({
dismissedAt: new Date().toISOString()
})
onDismiss()
}

const maxSteps = announcements.length

return (
<CozyTheme variant="normal">
<FixedActionsDialog
open
onClose={onDismiss}
onClose={handleDismiss}
content={
<SwipeableViews
index={activeStep}
Expand All @@ -64,8 +80,8 @@ const AnnouncementsDialog: FC<AnnouncementsDialogProps> = ({
key={index}
isLast={index === maxSteps - 1}
announcement={announcement}
onDismiss={onDismiss}
onNext={handleNext}
onLast={handleLast}
/>
))}
</SwipeableViews>
Expand Down
8 changes: 4 additions & 4 deletions src/components/Announcements/AnnouncementsDialogContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import { useAnnouncementsImage } from 'hooks/useAnnouncementsImage'
interface AnnouncementsDialogContentProps {
isLast: boolean
announcement: Announcement
onDismiss: () => void
onNext: () => void
onLast: () => void
}

const AnnouncementsDialogContent: FC<AnnouncementsDialogContentProps> = ({
isLast,
announcement,
onDismiss,
onNext
onNext,
onLast
}) => {
const { t, f } = useI18n()
const primaryImage = useAnnouncementsImage(
Expand Down Expand Up @@ -87,7 +87,7 @@ const AnnouncementsDialogContent: FC<AnnouncementsDialogContentProps> = ({
: 'AnnouncementsDialogContent.next'
)}
variant="secondary"
onClick={isLast ? onDismiss : onNext}
onClick={isLast ? onLast : onNext}
/>
{secondaryImage ? (
<img
Expand Down
6 changes: 1 addition & 5 deletions src/hooks/useAnnouncements.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,9 @@ const useAnnouncements = ({
)
const unseenData = getUnseenAnnouncements(rawData, uuidsInCommon)

// we consider the first post to have been seen as soon as it is displayed
if (unseenData.length > 0) {
uuidsInCommon.push(unseenData[0].attributes.uuid)
}
save({ seen: uuidsInCommon })

setUnseenData(unseenData)
setUnseenData(unseenData.slice(0, 5))
}
}, [hasStartedFiltering, rawData, values, save])

Expand Down
20 changes: 17 additions & 3 deletions src/hooks/useAnnouncementsSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@ import { useSettings } from 'cozy-client'

const defaultAnnouncements = {
dismissedAt: undefined,
seen: []
seen: [],
firstActivatedAt: undefined
}

const useAnnouncementsSettings = (): {
fetchStatus: string
values: {
dismissedAt?: string
seen: string[]
firstActivatedAt?: string
}
save: (data: { dismissedAt?: string; seen?: string[] }) => void
save: (data: {
dismissedAt?: string
seen?: string[]
firstActivatedAt?: string
}) => void
} => {
const { values, save } = useSettings('home', [
const { query, values, save } = useSettings('home', [
'announcements'
]) as unknown as UseSettingsType

const saveAnnouncement = (data: {
dismissedAt?: string
seen?: string[]
firstActivatedAt?: string
}): void => {
const announcements = {
...(values?.announcements ?? defaultAnnouncements),
Expand All @@ -30,22 +38,28 @@ const useAnnouncementsSettings = (): {
}

return {
fetchStatus: query.fetchStatus,
values: values?.announcements ?? defaultAnnouncements,
save: saveAnnouncement
}
}

interface UseSettingsType {
query: {
fetchStatus: string
}
values?: {
announcements: {
dismissedAt: string
seen: string[]
firstActivatedAt: string
}
}
save: (data: {
announcements: {
dismissedAt?: string
seen: string[]
firstActivatedAt?: string
}
}) => void
}
Expand Down
Loading