Skip to content

Commit

Permalink
(PC-34054) fix(cookies): revert usage of query for cookies up to date (
Browse files Browse the repository at this point in the history
  • Loading branch information
bpeyrou-pass authored Jan 28, 2025
1 parent 588ea2a commit e3be799
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import mockdate from 'mockdate'
import { onlineManager } from 'react-query'

import {
CookiesLastUpdate,
Expand Down Expand Up @@ -160,19 +159,6 @@ describe('isCookiesListUpToDate', () => {
})
})
})

it('should not fetch data when connection is disabled', async () => {
onlineManager.setOnline(false)

const { result } = renderUseIsCookiesListUpToDate()
await waitFor(() => {
expect(result.current).toEqual({
cookiesLastUpdate: undefined,
isCookiesListUpToDate: true,
isLoading: false,
})
})
})
})

const renderUseIsCookiesListUpToDate = () =>
Expand Down
37 changes: 22 additions & 15 deletions src/features/cookies/helpers/useIsCookiesListUpToDate.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
import { onlineManager, useQuery } from 'react-query'
import { useEffect, useState } from 'react'

import { getCookiesChoice } from 'features/cookies/helpers/useCookies'
import { getCookiesLastUpdate } from 'libs/firebase/firestore/getCookiesLastUpdate'
import { getAppBuildVersion } from 'libs/packageJson'
import { QueryKeys } from 'libs/queryKeys'

export interface CookiesLastUpdate {
lastUpdated: Date
lastUpdateBuildVersion: number
}

export const useIsCookiesListUpToDate = () => {
const fetchCookiesData = async () => {
const [consent, lastUpdate] = await Promise.all([getCookiesChoice(), getCookiesLastUpdate()])
return { consent, lastUpdate }
}
const [cookiesLastUpdate, setCookiesLastUpdate] = useState<CookiesLastUpdate>()
const [consentBuildVersion, setConsentBuildVersion] = useState<number>()
const [consentChoiceDatetime, setConsentChoiceDatetime] = useState<string>()
const [isLoading, setIsLoading] = useState<boolean>(false)

// TODO(PC-34248): refacto cookies management
useEffect(() => {
const fetchData = async () => {
setIsLoading(true)
const [consent, lastUpdate] = await Promise.all([getCookiesChoice(), getCookiesLastUpdate()])
setIsLoading(false)

const { data, isLoading } = useQuery(QueryKeys.COOKIES_DATA, fetchCookiesData, {
staleTime: 1000 * 30,
cacheTime: 1000 * 30,
enabled: onlineManager.isOnline(),
})
if (consent) {
setConsentBuildVersion(consent.buildVersion)
setConsentChoiceDatetime(consent.choiceDatetime)
}
if (lastUpdate) {
setCookiesLastUpdate(lastUpdate)
}
}

const cookiesLastUpdate = data?.lastUpdate
const consentBuildVersion = data?.consent?.buildVersion
const consentChoiceDatetime = data?.consent?.choiceDatetime
fetchData()
}, [])

const isUpToDate = () => {
// If no data from Firestore, consider that the cookie list is up to date
Expand All @@ -51,7 +59,6 @@ export const useIsCookiesListUpToDate = () => {
return choiceTime >= lastUpdated
}
}

return true
}

Expand Down
7 changes: 7 additions & 0 deletions src/features/home/pages/Home.web.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'

import { SubcategoriesResponseModelv2 } from 'api/gen'
import * as CookiesUpToDate from 'features/cookies/helpers/useIsCookiesListUpToDate'
import { useHomepageData } from 'features/home/api/useHomepageData'
import { formattedBusinessModule } from 'features/home/fixtures/homepage.fixture'
import { setFeatureFlags } from 'libs/firebase/firestore/featureFlags/__tests__/setFeatureFlags'
Expand Down Expand Up @@ -29,6 +30,12 @@ jest.mock('libs/firebase/firestore/featureFlags/useFeatureFlag')
jest.mock('libs/firebase/analytics/analytics')
jest.mock('libs/firebase/remoteConfig/remoteConfig.services')

jest.spyOn(CookiesUpToDate, 'useIsCookiesListUpToDate').mockReturnValue({
isCookiesListUpToDate: true,
cookiesLastUpdate: { lastUpdated: new Date('10/12/2022'), lastUpdateBuildVersion: 10208002 },
isLoading: false,
})

describe('<Home/>', () => {
beforeEach(() => {
setFeatureFlags([RemoteStoreFeatureFlags.ENABLE_PACIFIC_FRANC_CURRENCY])
Expand Down

0 comments on commit e3be799

Please sign in to comment.