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

2897: Refresh cities every day #2921

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 10 additions & 4 deletions native/src/hooks/useLoadCityContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import {
LocalNewsModel,
PoiModel,
ReturnType,
createCitiesEndpoint,
} from 'shared/api'

import dataContainer from '../utils/DefaultDataContainer'
import loadResourceCache from '../utils/loadResourceCache'
import { reportError } from '../utils/sentry'
import useLoadCities from './useLoadCities'
import useLoadWithCache from './useLoadWithCache'
import usePreviousProp from './usePreviousProp'
import useSnackbar from './useSnackbar'
Expand Down Expand Up @@ -51,10 +51,16 @@ export type CityContentReturn = Omit<Omit<ReturnType<CityContentData>, 'error'>,
*/
const useLoadCityContent = ({ cityCode, languageCode, refreshLocalNews }: Params): CityContentReturn => {
const showSnackbar = useSnackbar()
const citiesReturn = useLoadCities()
const previousLanguageCode = usePreviousProp({ prop: languageCode })
const params = { cityCode, languageCode, showSnackbar }

const citiesReturn = useLoadWithCache({
...params,
isAvailable: dataContainer.citiesAvailable,
createEndpoint: createCitiesEndpoint,
getFromDataContainer: dataContainer.getCities,
setToDataContainer: (_, __, cities) => dataContainer.setCities(cities),
})
const categoriesReturn = useLoadWithCache({
...params,
isAvailable: dataContainer.categoriesAvailable,
Expand Down Expand Up @@ -86,7 +92,7 @@ const useLoadCityContent = ({ cityCode, languageCode, refreshLocalNews }: Params
})

useEffect(() => {
if (categoriesReturn.data && eventsReturn.data && poisReturn.data) {
if (citiesReturn.data && categoriesReturn.data && eventsReturn.data && poisReturn.data && localNewsReturn.data) {
// Load the resource cache in the background once a day and do not wait for it
dataContainer.getLastUpdate(cityCode, languageCode).then(lastUpdate => {
if (!lastUpdate || lastUpdate < DateTime.utc().startOf('day')) {
Expand All @@ -104,7 +110,7 @@ const useLoadCityContent = ({ cityCode, languageCode, refreshLocalNews }: Params
// WARNING: This also means that the last update is updated if everything is just loaded from the cache.
dataContainer.setLastUpdate(cityCode, languageCode, DateTime.utc()).catch(reportError)
}
}, [categoriesReturn, eventsReturn, poisReturn, cityCode, languageCode])
}, [citiesReturn, categoriesReturn, eventsReturn, poisReturn, localNewsReturn, cityCode, languageCode])

const city = citiesReturn.data?.find(it => it.code === cityCode)
const language = city?.languages.find(it => it.code === languageCode)
Expand Down
6 changes: 6 additions & 0 deletions release-notes/unreleased/2897-refresh-cities.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
issue_key: 2897
show_in_stores: false
platforms:
- ios
- android
en: Cities and enabled features are now updated daily.
4 changes: 2 additions & 2 deletions shared/api/endpoints/createCitiesEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { JsonCityType } from '../types'

export const CITIES_ENDPOINT_NAME = 'cities'

export default (baseUrl: string): Endpoint<void, CityModel[]> =>
new EndpointBuilder<void, CityModel[]>(CITIES_ENDPOINT_NAME)
export default (baseUrl: string): Endpoint<Record<string, unknown> | void, CityModel[]> =>
new EndpointBuilder<Record<string, unknown> | void, CityModel[]>(CITIES_ENDPOINT_NAME)
.withParamsToUrlMapper(() => `${baseUrl}/api/${API_VERSION}/regions/`)
.withMapper((json: JsonCityType[]) =>
json.map(mapCityJson).sort((city1, city2) => city1.sortingName.localeCompare(city2.sortingName)),
Expand Down
Loading