update screener

This commit is contained in:
MuslemRahimi 2025-02-10 12:41:11 +01:00
parent 6b67e98a10
commit f717fe45a3

View File

@ -1817,26 +1817,39 @@
// Find the index of the rule to be deleted or updated // Find the index of the rule to be deleted or updated
const index = ruleOfList?.findIndex((rule) => rule.name === state); const index = ruleOfList?.findIndex((rule) => rule.name === state);
if (index !== -1) { if (index !== -1) {
// Get the rule // Get the rule and its default values
const rule = ruleOfList[index]; const rule = ruleOfList[index];
const defaultCondition = allRules[state].defaultCondition;
const defaultValue = allRules[state].defaultValue;
// Check if the value is "any" // Check if current values differ from defaults
if (Array?.isArray(rule.value)) { const isAtDefaultValues =
ruleOfList.splice(index, 1); ruleCondition[state] === defaultCondition &&
} else { (Array.isArray(valueMappings[state]) && Array.isArray(defaultValue)
// For single string values ? JSON.stringify(valueMappings[state]) ===
if (rule.value !== "any") { JSON.stringify(defaultValue)
// Set value to "any" : valueMappings[state] === defaultValue);
ruleOfList[index].value = "any";
} else {
// Remove the rule if its value is already "any"
ruleOfList.splice(index, 1);
}
}
// Update the stock screener data if (!isAtDefaultValues) {
ruleName = ruleOfList[index]?.name || ""; // If not at defaults, reset to defaults
await handleChangeValue(ruleOfList[index]?.value); ruleCondition[state] = defaultCondition;
valueMappings[state] = defaultValue;
// Update the rule in ruleOfList
ruleOfList[index] = {
...rule,
condition: defaultCondition,
value: defaultValue,
};
ruleOfList = [...ruleOfList]; // Trigger reactivity
} else {
// If already at defaults, remove the rule
ruleOfList.splice(index, 1);
ruleOfList = [...ruleOfList];
// Reset checkedItems for multi-select rules
if (checkedItems.has(state)) {
checkedItems.delete(state);
} }
// Handle cases when the list is empty or matches the current rule name // Handle cases when the list is empty or matches the current rule name
@ -1847,12 +1860,10 @@
} else if (state === ruleName) { } else if (state === ruleName) {
ruleName = ""; ruleName = "";
} }
}
// Ensure the array reference is updated
ruleOfList = [...ruleOfList];
await updateStockScreenerData(); await updateStockScreenerData();
// await handleSave(false); }
} }
async function handleScroll() { async function handleScroll() {
@ -1976,13 +1987,19 @@ const handleKeyDown = (event) => {
function changeRuleCondition(name: string, state: string) { function changeRuleCondition(name: string, state: string) {
ruleName = name; ruleName = name;
if ( const newState = state?.toLowerCase();
// Initialize array for "between" condition
if (newState === "between") {
valueMappings[ruleName] = ["", ""];
} else if (
ruleCondition[ruleName] === "between" && ruleCondition[ruleName] === "between" &&
["over", "under", "exactly"]?.includes(state?.toLowerCase()) ["over", "under", "exactly"].includes(newState)
) { ) {
valueMappings[ruleName] = ""; valueMappings[ruleName] = "any";
} }
ruleCondition[ruleName] = state?.toLowerCase();
ruleCondition[ruleName] = newState;
} }
let checkedItems = new Map( let checkedItems = new Map(
@ -2038,6 +2055,23 @@ const handleKeyDown = (event) => {
} }
async function handleChangeValue(value, { shouldSort = true } = {}) { async function handleChangeValue(value, { shouldSort = true } = {}) {
// Add this check at the beginning of the function
if (ruleCondition[ruleName] === "between") {
// Ensure valueMappings[ruleName] is always an array for "between" condition
if (!Array.isArray(valueMappings[ruleName])) {
valueMappings[ruleName] = ["", ""];
}
// If value is a single value (from input), update only the specified index
if (!Array.isArray(value) && typeof currentIndex === "number") {
valueMappings[ruleName][currentIndex] = value;
value = valueMappings[ruleName];
} else if (Array.isArray(value)) {
// Only for preset ranges from dropdown
valueMappings[ruleName] = value;
}
}
if (checkedItems.has(ruleName)) { if (checkedItems.has(ruleName)) {
const itemsSet = checkedItems.get(ruleName); const itemsSet = checkedItems.get(ruleName);
@ -2121,6 +2155,11 @@ const handleKeyDown = (event) => {
} else { } else {
console.warn(`Unhandled rule: ${ruleName}`); console.warn(`Unhandled rule: ${ruleName}`);
} }
// Add this at the end of the function to ensure the filter is applied
if (ruleCondition[ruleName] === "between" && value.some((v) => v !== "")) {
await updateStockScreenerData();
}
} }
async function stepSizeValue(value, condition) { async function stepSizeValue(value, condition) {
@ -2143,22 +2182,41 @@ const handleKeyDown = (event) => {
await handleChangeValue(newValue); await handleChangeValue(newValue);
} }
let currentIndex = null;
async function handleValueInput(event, ruleName, index = null) { async function handleValueInput(event, ruleName, index = null) {
const newValue = event.target.value; const newValue = event.target.value;
if (newValue?.length === 0) {
const index = ruleOfList?.findIndex((rule) => rule.name === ruleName);
if (index !== -1) {
ruleOfList[index].value = "any";
}
}
if (ruleCondition[ruleName] === "between") { if (ruleCondition[ruleName] === "between") {
const currentValues = valueMappings[ruleName] || ["", ""]; // Ensure valueMappings[ruleName] is initialized as an array
currentValues[index] = newValue; if (!Array.isArray(valueMappings[ruleName])) {
await handleChangeValue(currentValues, { shouldSort: false }); valueMappings[ruleName] = ["", ""];
}
// Store the current index being modified
currentIndex = index;
if (newValue?.length === 0) {
valueMappings[ruleName][index] = "";
}
await handleChangeValue(newValue, { shouldSort: false });
// Reset currentIndex after handling the value
currentIndex = null;
} else { } else {
if (newValue?.length === 0) {
const ruleIndex = ruleOfList?.findIndex(
(rule) => rule.name === ruleName,
);
if (ruleIndex !== -1) {
ruleOfList[ruleIndex].value = "any";
}
}
await handleChangeValue(newValue); await handleChangeValue(newValue);
} }
} }
async function popularStrategy(state: string) { async function popularStrategy(state: string) {
ruleOfList = []; ruleOfList = [];
const strategies = { const strategies = {