11import { Form } from 'antd' ;
2- import React , { useState } from 'react' ;
2+ import React , { useState , useEffect } from 'react' ;
33import { useTranslation } from 'react-i18next' ;
44import { useSelector } from 'react-redux' ;
55import { useNavigate } from 'react-router-dom' ;
66
77import { Layout , Button } from 'components/index' ;
88import { TimeSetSuccessfully } from 'components/Modal/WarningBaseModal' ;
99import { useController } from 'hooks/useController' ;
10+ import { startInactivityTimer } from 'scripts/Background/events/InactivityTimer' ;
1011import { RootState } from 'state/store' ;
1112
12- const AutolockView = ( ) => {
13+ const AutoLockView = ( ) => {
1314 const { isTimerEnabled } = useSelector ( ( state : RootState ) => state . vault ) ;
1415 const timer = useSelector ( ( state : RootState ) => state . vault . timer ) ;
1516 const { t } = useTranslation ( ) ;
16- const [ confirmed , setConfirmed ] = useState < boolean > ( false ) ;
17- const [ isEnabled , setIsEnabled ] = useState < boolean > ( isTimerEnabled ) ;
18- const [ loading , setLoading ] = useState < boolean > ( false ) ;
17+ const [ confirmed , setConfirmed ] = useState ( false ) ;
18+ const [ isEnabled , setIsEnabled ] = useState ( isTimerEnabled ) ;
19+ const [ loading , setLoading ] = useState ( false ) ;
1920 const [ form ] = Form . useForm ( ) ;
2021 const [ inputValue , setInputValue ] = useState ( timer ) ;
22+ const [ hasError , setHasError ] = useState ( false ) ;
23+
24+ const { controllerEmitter } = useController ( ) ;
25+ const navigate = useNavigate ( ) ;
26+
27+ useEffect ( ( ) => {
28+ form . setFieldsValue ( { minutes : timer } ) ;
29+ if ( isEnabled ) {
30+ startInactivityTimer ( timer ) ;
31+ }
32+ } , [ timer , form , isEnabled ] ) ;
2133
2234 const handleMaxClick = ( ) => {
2335 setInputValue ( 120 ) ;
@@ -29,25 +41,18 @@ const AutolockView = () => {
2941 form . setFieldsValue ( { minutes : 5 } ) ;
3042 } ;
3143
32- const { controllerEmitter } = useController ( ) ;
33- const navigate = useNavigate ( ) ;
34-
3544 const onSubmit = ( data : any ) => {
45+ setLoading ( true ) ;
3646 try {
37- setLoading ( true ) ;
38- const minimumMinutesPossible = 5 ;
39- const maximumMinutesPossible = 120 ;
4047 const autolockMinutes = + data . minutes ;
41-
42- if (
43- autolockMinutes < minimumMinutesPossible ||
44- autolockMinutes > maximumMinutesPossible
45- ) {
46- throw new Error ( 'Value must be between 5 and 120' ) ;
48+ if ( autolockMinutes < 5 || autolockMinutes > 120 ) {
49+ setHasError ( true ) ;
50+ return ;
51+ } else {
52+ setHasError ( false ) ;
4753 }
48- controllerEmitter ( [ 'wallet' , 'setAutolockTimer' ] , [ + data . minutes ] ) ;
54+ controllerEmitter ( [ 'wallet' , 'setAutolockTimer' ] , [ autolockMinutes ] ) ;
4955 controllerEmitter ( [ 'wallet' , 'setIsAutolockEnabled' ] , [ isEnabled ] ) ;
50-
5156 setConfirmed ( true ) ;
5257 } catch ( error ) {
5358 console . error ( 'Error:' , error ) ;
@@ -56,8 +61,8 @@ const AutolockView = () => {
5661 }
5762 } ;
5863
59- const handleInputChange = ( e ) => {
60- setInputValue ( e . target . value ) ;
64+ const handleInputChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
65+ setInputValue ( + e . target . value ) ;
6166 } ;
6267
6368 return (
@@ -89,7 +94,7 @@ const AutolockView = () => {
8994 initialValues = { { minutes : inputValue } }
9095 autoComplete = "off"
9196 >
92- < Form . Item name = "minutes" className = " bg-transparent" hasFeedback >
97+ < Form . Item name = "minutes" className = "bg-transparent" hasFeedback >
9398 < div className = "relative" >
9499 < span
95100 onClick = { handleMaxClick }
@@ -112,6 +117,11 @@ const AutolockView = () => {
112117 </ p >
113118 </ div >
114119 </ Form . Item >
120+ < div className = "flex flex-col items-center justify-center m-3" >
121+ { hasError && (
122+ < p className = "text-xs text-red-500" > { t ( 'settings.errorMinutes' ) } </ p >
123+ ) }
124+ </ div >
115125
116126 < div className = "flex flex-col justify-start items-start my-6 text-sm text-brand-gray200" >
117127 < p > { t ( 'settings.defaultMinutes' ) } </ p >
@@ -122,12 +132,6 @@ const AutolockView = () => {
122132 id = "verify-address-switch"
123133 name = "verify"
124134 className = "flex flex-col w-full text-center"
125- rules = { [
126- {
127- required : false ,
128- message : '' ,
129- } ,
130- ] }
131135 >
132136 < div className = "flex flex-row gap-2 align-center justify-between w-full" >
133137 < span className = "text-sm" > { t ( 'settings.enableAutolock' ) } </ span >
@@ -145,7 +149,6 @@ const AutolockView = () => {
145149 </ div >
146150 </ div >
147151 </ Form . Item >
148-
149152 < div className = "relative bottom-[-11rem] md:static" >
150153 < Button
151154 className = "flex items-center justify-center w-full h-10 bg-white text-brand-blue400 text-base font-medium rounded-[100px]"
@@ -160,4 +163,4 @@ const AutolockView = () => {
160163 ) ;
161164} ;
162165
163- export default AutolockView ;
166+ export default AutoLockView ;
0 commit comments