Skip to content
Snippets Groups Projects

Issue #415 - Refactor Events to prevent continous state changes causing slowness

Merged Issue #415 - Refactor Events to prevent continous state changes causing slowness
All threads resolved!
Merged Dima Rifai requested to merge delrifai/#415_handle_slowness_search_range into develop
All threads resolved!
1 file
+ 56
56
Compare changes
  • Side-by-side
  • Inline
  • 29ee43af
    Issue #415 - Refactor Events to prevent continous state changes causing slowness · 29ee43af
    Dima Rifai authored
@@ -39,33 +39,26 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
@@ -39,33 +39,26 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
// Validate and update numeric values
// Validate and update numeric values
const handleNumericInput = (
const handleNumericInput = (
value: string,
e: React.ChangeEvent<HTMLInputElement>,
fieldSetter: Function,
allowDecimal: boolean = false
allowDecimal: boolean = false
) => {
) => {
// If decimals are allowed, preserve the decimal point in the input value
let cleanedValue = e.target.value;
let cleanedValue = allowDecimal
? value.replace(/[^0-9.]/g, "") // Allow numbers and decimal point
// Clean the value based on the logic
: value.replace(/[^0-9]/g, ""); // Only allow numbers
cleanedValue = allowDecimal
? cleanedValue.replace(/[^0-9.]/g, "") // Allow numbers and decimal point
// If the decimal is allowed, ensure there is only one decimal point
: cleanedValue.replace(/[^0-9]/g, ""); // Only allow numbers
 
if (allowDecimal && cleanedValue.split(".").length > 2) {
if (allowDecimal && cleanedValue.split(".").length > 2) {
// Remove extra decimals, keeping only the first one
cleanedValue = cleanedValue.slice(0, cleanedValue.indexOf(".") + 1) +
cleanedValue = cleanedValue.slice(0, cleanedValue.indexOf(".") + 1) +
cleanedValue.split(".").slice(1).join(""); // Remove all decimals after the first one
cleanedValue.split(".").slice(1).join(""); // Remove extra decimals
}
}
// Limit the value to a maximum of 15 digits
if (cleanedValue.length > 15) {
if (cleanedValue.length > 15) {
cleanedValue = cleanedValue.slice(0, 15);
cleanedValue = cleanedValue.slice(0, 15); // Limit to 15 digits
}
}
// If the cleaned value is empty, set it to undefined
e.target.value = cleanedValue;
if (cleanedValue === "") {
fieldSetter(undefined);
} else {
fieldSetter(cleanedValue); // Invalid input, set to undefined
}
};
};
useEffect(() => {
useEffect(() => {
@@ -114,10 +107,13 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
@@ -114,10 +107,13 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
<Input
<Input
className="w-1/2 border-0 border-b-2 bg-theme"
className="w-1/2 border-0 border-b-2 bg-theme"
type="text" // Use type="text" to allow custom validation
type="text" // Use type="text" to allow custom validation
value={lastBlocksValue || ""}
defaultValue={lastBlocksValue || ""}
onChange={(e) =>
onChange={(e) => handleNumericInput(e)}
handleNumericInput(e.target.value,setLastBlocksValue)
onBlur={(e) => {
}
const value = e.target.value;
 
const numericValue = value ? Number(value) : undefined;
 
setLastBlocksValue(numericValue);
 
}}
placeholder={"Last"}
placeholder={"Last"}
/>
/>
</div>
</div>
@@ -131,10 +127,13 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
@@ -131,10 +127,13 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
<Input
<Input
type="text"
type="text"
className="bg-theme border-0 border-b-2 text-text"
className="bg-theme border-0 border-b-2 text-text"
value={lastTimeUnitValue || ""}
defaultValue={lastTimeUnitValue || ""}
onChange={(e) =>
onChange={(e) => handleNumericInput(e,true)}
handleNumericInput(e.target.value,setLastTimeUnitValue,true)
onBlur={(e) => {
}
const value = e.target.value;
 
const numericValue = value ? Number(value) : undefined;
 
setLastTimeUnitValue(numericValue);
 
}}
placeholder={"Last"}
placeholder={"Last"}
/>
/>
</div>
</div>
@@ -173,10 +172,13 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
@@ -173,10 +172,13 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
type="text"
type="text"
className="bg-theme border-0 border-b-2"
className="bg-theme border-0 border-b-2"
data-testid="from-block-input"
data-testid="from-block-input"
value={fromBlock || ""}
defaultValue={fromBlock || ""}
onChange={(e) =>
onChange={(e) => handleNumericInput(e)}
handleNumericInput(e.target.value,setFromBlock)
onBlur={(e) => {
}
const value = e.target.value;
 
const numericValue = value ? Number(value) : undefined;
 
setFromBlock(numericValue);
 
}}
placeholder="From"
placeholder="From"
/>
/>
</div>
</div>
@@ -185,32 +187,30 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
@@ -185,32 +187,30 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
className="bg-theme border-0 border-b-2"
className="bg-theme border-0 border-b-2"
data-testid="headblock-number"
data-testid="headblock-number"
type="text"
type="text"
value={toBlock || ""}
defaultValue={toBlock || ""}
onChange={(e) =>
onChange={(e) => handleNumericInput(e)} // Keep cleaning logic here on change
handleNumericInput(e.target.value, setToBlock)
}
placeholder={"To"}
placeholder={"To"}
onBlur={() => {
onBlur={(e: React.FocusEvent<HTMLInputElement>) => {
if (
const value = e.target.value;
Number(toBlock) &&
const numericValue = value ? Number(value) : undefined; // Default to NaN if empty string
Number(fromBlock) &&
if (
!isNaN(Number(toBlock)) &&
numericValue &&
!isNaN(Number(fromBlock)) &&
fromBlock &&
Number(toBlock) < Number(fromBlock)
!isNaN(numericValue) &&
) {
!isNaN(Number(fromBlock)) &&
setBlockRangeError(
numericValue < Number(fromBlock)
"To block must be greater than From block"
) {
);
setBlockRangeError("To block must be greater than From block");
setToBlock(undefined); // Clear the 'toBlock' field
e.target.value = ""; // Clear the 'toBlock' field if validation fails
} else if (Number(toBlock) <= 0 && Number(fromBlock)) {
} else if (numericValue !=undefined && numericValue <= 0 && fromBlock) {
setBlockRangeError(
setBlockRangeError("To block must be greater than From block");
"To block must be greater than From block"
e.target.value = ""; // Clear the 'toBlock' field if validation fails
);
} else {
setToBlock(undefined); // Clear the 'toBlock' field
setToBlock(numericValue); // Set the state only when validation passes
} else {
setBlockRangeError(null); // Clear error if valid
setBlockRangeError(null); // Clear the error message immediately if 'toBlock' is valid
}
}
}
}}
}
/>
/>
</div>
</div>
</div>
</div>
@@ -247,4 +247,4 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
@@ -247,4 +247,4 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
);
);
};
};
export default SearchRanges;
export default SearchRanges;
 
\ No newline at end of file
Loading