mirror of
https://github.com/christianvidalwolf-prog/CrazeAnalytix.git
synced 2026-08-03 12:25:22 +02:00
feat: automatic ads data loading and attributed sales line in chart
This commit is contained in:
@@ -3,6 +3,7 @@ import { SalesRecord } from '../types';
|
||||
|
||||
const DB_NAME = 'CrazeAnalytixDB';
|
||||
const STORE_NAME = 'salesData';
|
||||
const ADS_STORE_NAME = 'adsData';
|
||||
const DB_VERSION = 1;
|
||||
// Increment this when data processing logic changes (e.g., field mapping changes)
|
||||
// This forces cache invalidation and re-processing of CSV data
|
||||
@@ -17,6 +18,9 @@ const initDB = (): Promise<IDBDatabase> => {
|
||||
if (!db.objectStoreNames.contains(STORE_NAME)) {
|
||||
db.createObjectStore(STORE_NAME);
|
||||
}
|
||||
if (!db.objectStoreNames.contains(ADS_STORE_NAME)) {
|
||||
db.createObjectStore(ADS_STORE_NAME);
|
||||
}
|
||||
};
|
||||
|
||||
request.onsuccess = () => resolve(request.result);
|
||||
@@ -99,3 +103,63 @@ export const clearSalesData = async (): Promise<void> => {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
// --- ADS STORAGE ---
|
||||
|
||||
export const saveAdsData = async (data: any[]): Promise<void> => {
|
||||
try {
|
||||
const db = await initDB();
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = db.transaction(ADS_STORE_NAME, 'readwrite');
|
||||
const store = transaction.objectStore(ADS_STORE_NAME);
|
||||
|
||||
store.put(data, 'currentData');
|
||||
store.put(new Date().toISOString(), 'lastUpdated');
|
||||
|
||||
transaction.oncomplete = () => resolve();
|
||||
transaction.onerror = () => reject(transaction.error);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error saving ads to IndexedDB:", error);
|
||||
}
|
||||
};
|
||||
|
||||
export const loadAdsData = async (): Promise<{ data: any[]; lastUpdated: string | null }> => {
|
||||
try {
|
||||
const db = await initDB();
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = db.transaction(ADS_STORE_NAME, 'readonly');
|
||||
const store = transaction.objectStore(ADS_STORE_NAME);
|
||||
|
||||
const dataReq = store.get('currentData');
|
||||
const dateReq = store.get('lastUpdated');
|
||||
|
||||
transaction.oncomplete = () => {
|
||||
resolve({
|
||||
data: dataReq.result || [],
|
||||
lastUpdated: dateReq.result || null
|
||||
});
|
||||
};
|
||||
|
||||
transaction.onerror = () => reject(transaction.error);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error loading ads from IndexedDB:", error);
|
||||
return { data: [], lastUpdated: null };
|
||||
}
|
||||
};
|
||||
|
||||
export const clearAdsData = async (): Promise<void> => {
|
||||
try {
|
||||
const db = await initDB();
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = db.transaction(ADS_STORE_NAME, 'readwrite');
|
||||
const store = transaction.objectStore(ADS_STORE_NAME);
|
||||
store.clear();
|
||||
transaction.oncomplete = () => resolve();
|
||||
transaction.onerror = () => reject(transaction.error);
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user