Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
Block Explorer UI
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hive
Block Explorer UI
Commits
d09122af
Commit
d09122af
authored
3 months ago
by
Dima Rifai
Committed by
mcfarhat
3 months ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#413
- Refactor onBlur functionality
parent
1cf39566
Branches
Branches containing commit
No related tags found
1 merge request
!521
Issue #413 - Add validation/fallback for payloadStartDate and payloadToBlock...
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
components/searchRanges/SearchRanges.tsx
+70
-50
70 additions, 50 deletions
components/searchRanges/SearchRanges.tsx
with
70 additions
and
50 deletions
components/searchRanges/SearchRanges.tsx
+
70
−
50
View file @
d09122af
import
React
,
{
useEffect
,
useState
}
from
"
react
"
;
import
React
,
{
useEffect
,
useState
}
from
"
react
"
;
import
moment
from
"
moment
"
;
import
moment
from
"
moment
"
;
import
{
SearchRangesResult
}
from
"
../../hooks/common/useSearchRanges
"
;
import
{
SearchRangesResult
}
from
"
../../hooks/common/useSearchRanges
"
;
import
{
Select
,
SelectContent
,
SelectTrigger
,
SelectItem
}
from
"
../ui/select
"
;
import
{
Select
,
SelectContent
,
SelectTrigger
,
SelectItem
}
from
"
../ui/select
"
;
import
{
Input
}
from
"
../ui/input
"
;
import
{
Input
}
from
"
../ui/input
"
;
import
DateTimePicker
from
"
../DateTimePicker
"
;
import
DateTimePicker
from
"
../DateTimePicker
"
;
import
ErrorMessage
from
"
../ErrorMessage
"
;
// Import the ErrorMessage component
import
ErrorMessage
from
"
../ErrorMessage
"
;
// Import the ErrorMessage component
interface
SearchRangesProps
{
interface
SearchRangesProps
{
rangesProps
:
SearchRangesResult
;
rangesProps
:
SearchRangesResult
;
safeTimeRangeDisplay
?:
boolean
;
safeTimeRangeDisplay
?:
boolean
;
...
@@ -37,23 +36,77 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
...
@@ -37,23 +36,77 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
setLastTimeUnitValue
,
setLastTimeUnitValue
,
}
=
rangesProps
;
}
=
rangesProps
;
// Validate and update numeric values
const
[
rangeError
,
setRangeError
]
=
useState
<
string
|
null
>
(
null
);
const
handleOnBlur
=
(
e
:
React
.
FocusEvent
<
HTMLInputElement
>
,
fieldSetter
:
Function
,
validateField
:
Function
|
null
)
=>
{
const
value
=
e
.
target
.
value
;
const
numericValue
=
value
?
Number
(
value
)
:
undefined
;
// Fetch the latest block number dynamically
let
validated
=
true
;
if
(
validateField
)
{
validated
=
validateField
(
e
,
numericValue
);
}
validated
?
fieldSetter
(
numericValue
)
:
fieldSetter
(
null
);
};
const
validateToBlock
=
(
e
:
React
.
FocusEvent
<
HTMLInputElement
>
,
value
:
number
|
undefined
,
)
=>
{
if
(
value
!==
undefined
&&
value
<=
0
)
{
setRangeError
(
"
Block Number must be a positive number
"
);
e
.
target
.
value
=
""
;
return
false
;
}
if
(
value
&&
fromBlock
&&
!
isNaN
(
value
)
&&
value
<
fromBlock
)
{
setRangeError
(
"
To block must be greater than From block
"
);
e
.
target
.
value
=
""
;
return
false
;
}
return
true
;
};
const
validateFromBlock
=
(
e
:
React
.
FocusEvent
<
HTMLInputElement
>
,
value
:
number
|
undefined
,
)
=>
{
if
(
value
!==
undefined
&&
value
<=
0
)
{
setRangeError
(
"
Block Number must be a positive number
"
);
e
.
target
.
value
=
""
;
return
false
;
}
if
(
value
&&
toBlock
&&
!
isNaN
(
value
)
&&
value
>
toBlock
)
{
setRangeError
(
"
From block must be less than To block
"
);
e
.
target
.
value
=
""
;
return
false
;
}
return
true
;
};
const
handleNumericInput
=
(
const
handleNumericInput
=
(
e
:
React
.
ChangeEvent
<
HTMLInputElement
>
,
e
:
React
.
ChangeEvent
<
HTMLInputElement
>
,
allowDecimal
:
boolean
=
false
allowDecimal
:
boolean
=
false
)
=>
{
)
=>
{
let
cleanedValue
=
e
.
target
.
value
;
let
cleanedValue
=
e
.
target
.
value
;
// Clean the value based on the logic
// Clean the value based on the logic
cleanedValue
=
allowDecimal
cleanedValue
=
allowDecimal
?
cleanedValue
.
replace
(
/
[^
0-9.
]
/g
,
""
)
// Allow numbers and decimal point
?
cleanedValue
.
replace
(
/
[^
0-9.
]
/g
,
""
)
// Allow numbers and decimal point
:
cleanedValue
.
replace
(
/
[^
0-9
]
/g
,
""
);
// Only allow numbers
:
cleanedValue
.
replace
(
/
[^
0-9
]
/g
,
""
);
// Only allow numbers
if
(
allowDecimal
&&
cleanedValue
.
split
(
"
.
"
).
length
>
2
)
{
if
(
allowDecimal
&&
cleanedValue
.
split
(
"
.
"
).
length
>
2
)
{
cleanedValue
=
cleanedValue
.
slice
(
0
,
cleanedValue
.
indexOf
(
"
.
"
)
+
1
)
+
cleanedValue
=
cleanedValue
.
split
(
"
.
"
).
slice
(
1
).
join
(
""
);
// Remove extra decimals
cleanedValue
.
slice
(
0
,
cleanedValue
.
indexOf
(
"
.
"
)
+
1
)
+
cleanedValue
.
split
(
"
.
"
).
slice
(
1
).
join
(
""
);
// Remove extra decimals
}
}
if
(
cleanedValue
.
length
>
15
)
{
if
(
cleanedValue
.
length
>
15
)
{
cleanedValue
=
cleanedValue
.
slice
(
0
,
15
);
// Limit to 15 digits
cleanedValue
=
cleanedValue
.
slice
(
0
,
15
);
// Limit to 15 digits
}
}
...
@@ -68,10 +121,9 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
...
@@ -68,10 +121,9 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
)
{
)
{
setStartDate
(
moment
(
startDate
).
subtract
(
1
,
"
hours
"
).
toDate
());
setStartDate
(
moment
(
startDate
).
subtract
(
1
,
"
hours
"
).
toDate
());
}
}
//eslint-disable-next-line react-hooks/exhaustive-deps
//
eslint-disable-next-line react-hooks/exhaustive-deps
},
[
startDate
,
endDate
]);
},
[
startDate
,
endDate
]);
const
[
blockRangeError
,
setBlockRangeError
]
=
useState
<
string
|
null
>
(
null
);
// Error state for block range validation
return
(
return
(
<
div
className
=
"py-2 flex flex-col gap-y-2"
>
<
div
className
=
"py-2 flex flex-col gap-y-2"
>
...
@@ -109,11 +161,7 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
...
@@ -109,11 +161,7 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
type
=
"text"
// Use type="text" to allow custom validation
type
=
"text"
// Use type="text" to allow custom validation
defaultValue
=
{
lastBlocksValue
||
""
}
defaultValue
=
{
lastBlocksValue
||
""
}
onChange
=
{
(
e
)
=>
handleNumericInput
(
e
)
}
onChange
=
{
(
e
)
=>
handleNumericInput
(
e
)
}
onBlur
=
{
(
e
)
=>
{
onBlur
=
{
(
e
)
=>
handleOnBlur
(
e
,
setLastBlocksValue
,
null
)
}
const
value
=
e
.
target
.
value
;
const
numericValue
=
value
?
Number
(
value
)
:
undefined
;
setLastBlocksValue
(
numericValue
);
}
}
placeholder
=
{
"
Last
"
}
placeholder
=
{
"
Last
"
}
/>
/>
</
div
>
</
div
>
...
@@ -129,11 +177,7 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
...
@@ -129,11 +177,7 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
className
=
"bg-theme border-0 border-b-2 text-text"
className
=
"bg-theme border-0 border-b-2 text-text"
defaultValue
=
{
lastTimeUnitValue
||
""
}
defaultValue
=
{
lastTimeUnitValue
||
""
}
onChange
=
{
(
e
)
=>
handleNumericInput
(
e
,
true
)
}
onChange
=
{
(
e
)
=>
handleNumericInput
(
e
,
true
)
}
onBlur
=
{
(
e
)
=>
{
onBlur
=
{
(
e
)
=>
handleOnBlur
(
e
,
setLastTimeUnitValue
,
null
)
}
const
value
=
e
.
target
.
value
;
const
numericValue
=
value
?
Number
(
value
)
:
undefined
;
setLastTimeUnitValue
(
numericValue
);
}
}
placeholder
=
{
"
Last
"
}
placeholder
=
{
"
Last
"
}
/>
/>
</
div
>
</
div
>
...
@@ -174,11 +218,7 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
...
@@ -174,11 +218,7 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
data-testid
=
"from-block-input"
data-testid
=
"from-block-input"
defaultValue
=
{
fromBlock
||
""
}
defaultValue
=
{
fromBlock
||
""
}
onChange
=
{
(
e
)
=>
handleNumericInput
(
e
)
}
onChange
=
{
(
e
)
=>
handleNumericInput
(
e
)
}
onBlur
=
{
(
e
)
=>
{
onBlur
=
{
(
e
)
=>
handleOnBlur
(
e
,
setFromBlock
,
validateFromBlock
)
}
const
value
=
e
.
target
.
value
;
const
numericValue
=
value
?
Number
(
value
)
:
undefined
;
setFromBlock
(
numericValue
);
}
}
placeholder
=
"From"
placeholder
=
"From"
/>
/>
</
div
>
</
div
>
...
@@ -190,35 +230,15 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
...
@@ -190,35 +230,15 @@ const SearchRanges: React.FC<SearchRangesProps> = ({
defaultValue
=
{
toBlock
||
""
}
defaultValue
=
{
toBlock
||
""
}
onChange
=
{
(
e
)
=>
handleNumericInput
(
e
)
}
onChange
=
{
(
e
)
=>
handleNumericInput
(
e
)
}
placeholder
=
{
"
To
"
}
placeholder
=
{
"
To
"
}
onBlur
=
{
(
e
:
React
.
FocusEvent
<
HTMLInputElement
>
)
=>
{
onBlur
=
{
(
e
)
=>
handleOnBlur
(
e
,
setToBlock
,
validateToBlock
)
}
const
value
=
e
.
target
.
value
;
const
numericValue
=
value
?
Number
(
value
)
:
undefined
;
if
(
numericValue
&&
fromBlock
&&
!
isNaN
(
numericValue
)
&&
!
isNaN
(
Number
(
fromBlock
))
&&
numericValue
<
Number
(
fromBlock
)
)
{
setBlockRangeError
(
"
To block must be greater than From block
"
);
e
.
target
.
value
=
""
;
}
else
if
(
numericValue
!=
undefined
&&
numericValue
<=
0
&&
fromBlock
)
{
setBlockRangeError
(
"
To block must be greater than From block
"
);
e
.
target
.
value
=
""
;
}
else
{
setToBlock
(
numericValue
);
setBlockRangeError
(
null
);
}
}
}
/>
/>
</
div
>
</
div
>
</
div
>
</
div
>
)
}
)
}
{
blockR
angeError
&&
(
{
r
angeError
&&
(
<
ErrorMessage
<
ErrorMessage
message
=
{
blockR
angeError
}
message
=
{
r
angeError
}
onClose
=
{
()
=>
set
Block
RangeError
(
null
)
}
// Close the error message
onClose
=
{
()
=>
setRangeError
(
null
)
}
// Close the error message
timeout
=
{
3000
}
timeout
=
{
3000
}
/>
/>
)
}
)
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment