Refresh client filter options reliably
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 56s

This commit is contained in:
Cauê Faleiros
2026-06-23 14:56:36 -03:00
parent 7d60073757
commit 06faadab5f
2 changed files with 16 additions and 3 deletions

View File

@@ -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
});
};

View File

@@ -141,7 +141,7 @@ const Clients = () => {
setRfmAnalytics(cachedRfm || null);
}
const filterOptionsPromise = fetchClientFilterOptions(dateRange);
const filterOptionsPromise = fetchClientFilterOptions(dateRange, { force: true });
const clientsPromise = fetchClientAnalytics(dateRange, metadataFilters);
const rfmPromise = fetchRfmAnalytics(dateRange, metadataFilters);
const [options, clientsData] = await Promise.all([filterOptionsPromise, clientsPromise]);