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 = {
|
type CacheOptions = {
|
||||||
force?: boolean;
|
force?: boolean;
|
||||||
|
shouldCache?: (data: unknown) => boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
type ApiCacheEntry<T> = {
|
type ApiCacheEntry<T> = {
|
||||||
@@ -37,7 +38,7 @@ const getCachedAnalytics = async <T>(key: string, loader: () => Promise<T>, opti
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const data = await promise;
|
const data = await promise;
|
||||||
if (data === null) {
|
if (data === null || options.shouldCache?.(data) === false) {
|
||||||
analyticsCache.delete(key);
|
analyticsCache.delete(key);
|
||||||
} else {
|
} else {
|
||||||
analyticsCache.set(key, { data, expiresAt: Date.now() + ANALYTICS_CACHE_TTL_MS });
|
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);
|
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()}`;
|
const path = `/analytics/clients/filters?${buildDateRangeParams(dateRange).toString()}`;
|
||||||
return getCachedAnalytics(path, async () => {
|
return getCachedAnalytics(path, async () => {
|
||||||
try {
|
try {
|
||||||
@@ -220,6 +230,9 @@ export const fetchClientFilterOptions = async (dateRange: DateRange): Promise<Cl
|
|||||||
console.error('Fetch client filter options failed', error);
|
console.error('Fetch client filter options failed', error);
|
||||||
return { marketplaces: [], salesChannels: [], sellers: [] };
|
return { marketplaces: [], salesChannels: [], sellers: [] };
|
||||||
}
|
}
|
||||||
|
}, {
|
||||||
|
...options,
|
||||||
|
shouldCache: hasClientFilterOptions
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -141,7 +141,7 @@ const Clients = () => {
|
|||||||
setRfmAnalytics(cachedRfm || null);
|
setRfmAnalytics(cachedRfm || null);
|
||||||
}
|
}
|
||||||
|
|
||||||
const filterOptionsPromise = fetchClientFilterOptions(dateRange);
|
const filterOptionsPromise = fetchClientFilterOptions(dateRange, { force: true });
|
||||||
const clientsPromise = fetchClientAnalytics(dateRange, metadataFilters);
|
const clientsPromise = fetchClientAnalytics(dateRange, metadataFilters);
|
||||||
const rfmPromise = fetchRfmAnalytics(dateRange, metadataFilters);
|
const rfmPromise = fetchRfmAnalytics(dateRange, metadataFilters);
|
||||||
const [options, clientsData] = await Promise.all([filterOptionsPromise, clientsPromise]);
|
const [options, clientsData] = await Promise.all([filterOptionsPromise, clientsPromise]);
|
||||||
|
|||||||
Reference in New Issue
Block a user