simplify updating ruleOfList

This commit is contained in:
MuslemRahimi 2024-10-15 13:40:05 +02:00
parent 771c54d5d9
commit 576ff332c6

View File

@ -11,8 +11,6 @@ import { Combobox } from "bits-ui";
export let data; export let data;
let currentWatchlistLocalStorage = '1.1'; // Increment this whenever the structure of allRows changes
let searchQuery = ''; let searchQuery = '';
let editMode = false; let editMode = false;
let numberOfChecked = 0; let numberOfChecked = 0;
@ -69,6 +67,7 @@ let allRows = [
{ name: 'Employees', rule: 'employees', type: 'int'}, { name: 'Employees', rule: 'employees', type: 'int'},
{ name: 'Debt Ratio', rule: 'debtRatio', type: 'float'}, { name: 'Debt Ratio', rule: 'debtRatio', type: 'float'},
{ name: 'Debt / Equity', rule: 'debtEquityRatio', type: 'int'}, { name: 'Debt / Equity', rule: 'debtEquityRatio', type: 'int'},
{ name: 'Profit Margin', rule: 'netProfitMargin', type: 'percent'},
]; ];
@ -404,7 +403,6 @@ function saveRules() {
try { try {
// Save the version along with the rules // Save the version along with the rules
localStorage?.setItem('watchlist-ruleOfList', JSON?.stringify(ruleOfList)); localStorage?.setItem('watchlist-ruleOfList', JSON?.stringify(ruleOfList));
localStorage?.setItem('watchlist-ruleOfList-version', currentWatchlistLocalStorage); // Save the current version
} catch (e) { } catch (e) {
console.log('Failed saving indicator rules: ', e); console.log('Failed saving indicator rules: ', e);
} }
@ -413,20 +411,29 @@ function saveRules() {
onMount(async () => { onMount(async () => {
try { try {
const savedRules = localStorage?.getItem('watchlist-ruleOfList'); const savedRules = localStorage?.getItem('watchlist-ruleOfList');
const savedVersion = localStorage?.getItem('watchlist-ruleOfList-version');
// If the version doesn't match, reset the local storage if (savedRules) {
if (savedVersion !== currentWatchlistLocalStorage) { const parsedRules = JSON.parse(savedRules);
localStorage?.removeItem('watchlist-ruleOfList'); // Clear old data
localStorage?.setItem('watchlist-ruleOfList-version', currentWatchlistLocalStorage); // Save new version // Compare and update ruleOfList based on allRows
localStorage?.setItem('watchlist-ruleOfList', JSON?.stringify(ruleOfList)); // Save new rules ruleOfList = parsedRules.map(rule => {
} else if (savedRules) { const matchingRow = allRows.find(row => row.name === rule.name);
ruleOfList = JSON.parse(savedRules); if (matchingRow && matchingRow.type !== rule.type) {
return { ...rule, type: matchingRow.type };
}
return rule;
});
// Check for the user's tier and filter out paywalled features // Check for the user's tier and filter out paywalled features
if (data?.user?.tier !== 'Pro') { if (data?.user?.tier !== 'Pro') {
ruleOfList = ruleOfList.filter(item => excludedRules.has(item?.rule)); // Use Set to filter ruleOfList = ruleOfList.filter(item => excludedRules.has(item?.rule));
} }
// Save the updated ruleOfList back to localStorage
localStorage?.setItem('watchlist-ruleOfList', JSON.stringify(ruleOfList));
} else {
// If no saved rules, initialize with the current ruleOfList
localStorage?.setItem('watchlist-ruleOfList', JSON.stringify(ruleOfList));
} }
// Update checked items and sort the indicators // Update checked items and sort the indicators
@ -441,6 +448,7 @@ onMount(async () => {
} }
await getWatchlistData(); await getWatchlistData();
// Initialize the download worker if not already done // Initialize the download worker if not already done
if (!downloadWorker) { if (!downloadWorker) {
const DownloadWorker = await import('./workers/downloadWorker?worker'); const DownloadWorker = await import('./workers/downloadWorker?worker');
@ -449,7 +457,6 @@ onMount(async () => {
} }
isLoaded = true; isLoaded = true;
} catch (e) { } catch (e) {
console.log(e); console.log(e);
} }