Refresh client filter options reliably
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 56s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 56s
This commit is contained in:
@@ -7,6 +7,7 @@ const IN_FLIGHT_CACHE_TTL_MS = 15 * 1000;
|
||||
|
||||
type CacheOptions = {
|
||||
force?: boolean;
|
||||
shouldCache?: (data: unknown) => boolean;
|
||||
};
|
||||
|
||||
type ApiCacheEntry<T> = {
|
||||
@@ -37,7 +38,7 @@ const getCachedAnalytics = async <T>(key: string, loader: () => Promise<T>, opti
|
||||
|
||||
try {
|
||||
const data = await promise;
|
||||
if (data === null) {
|
||||
if (data === null || options.shouldCache?.(data) === false) {
|
||||
analyticsCache.delete(key);
|
||||
} else {
|
||||
analyticsCache.set(key, { data, expiresAt: Date.now() + ANALYTICS_CACHE_TTL_MS });
|
||||
@@ -209,7 +210,16 @@ const appendClientMetadataFilterParams = (params: URLSearchParams, filters?: Par
|
||||
if (filters.seller) params.set('seller', filters.seller);
|
||||
};
|
||||
|
||||
export const fetchClientFilterOptions = async (dateRange: DateRange): Promise<ClientFilterOptions> => {
|
||||
const hasClientFilterOptions = (data: unknown) => {
|
||||
const options = data as ClientFilterOptions | null;
|
||||
return Boolean(
|
||||
options?.marketplaces?.length ||
|
||||
options?.salesChannels?.length ||
|
||||
options?.sellers?.length
|
||||
);
|
||||
};
|
||||
|
||||
export const fetchClientFilterOptions = async (dateRange: DateRange, options?: CacheOptions): Promise<ClientFilterOptions> => {
|
||||
const path = `/analytics/clients/filters?${buildDateRangeParams(dateRange).toString()}`;
|
||||
return getCachedAnalytics(path, async () => {
|
||||
try {
|
||||
@@ -220,6 +230,9 @@ export const fetchClientFilterOptions = async (dateRange: DateRange): Promise<Cl
|
||||
console.error('Fetch client filter options failed', error);
|
||||
return { marketplaces: [], salesChannels: [], sellers: [] };
|
||||
}
|
||||
}, {
|
||||
...options,
|
||||
shouldCache: hasClientFilterOptions
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user