@@ -24,6 +24,7 @@ const Buy = ({ lists, updateLists, onSeeOptions, onLoading }: BuyProps) => {
2424 const [ loading , setLoading ] = useState ( false ) ;
2525 const [ creatingAd , setCreatingAd ] = useState ( false ) ;
2626 const router = useRouter ( ) ;
27+ let changeInSelector = false ;
2728
2829 const updateLoading = ( l : boolean ) => {
2930 setLoading ( l ) ;
@@ -38,6 +39,7 @@ const Buy = ({ lists, updateLists, onSeeOptions, onLoading }: BuyProps) => {
3839 fiatValue : number | undefined ;
3940 } ) => {
4041 if ( ! token || ! currency || ( ! tokenValue && ! fiatValue ) ) return ;
42+ if ( changeInSelector === false && ( ( fiatValue && fiatAmount === fiatValue ) || ( tokenValue && tokenAmount === tokenValue ) ) ) return ;
4143 updateLoading ( true ) ;
4244 try {
4345 const params = {
@@ -63,9 +65,14 @@ const Buy = ({ lists, updateLists, onSeeOptions, onLoading }: BuyProps) => {
6365 const [ list ] = searchLists ;
6466 const { price } = list || { } ;
6567 if ( price ) {
66- if ( tokenValue ) setFiatAmount ( tokenValue * price ) ;
67- if ( fiatValue ) setTokenAmount ( truncate ( fiatValue / price , token . decimals ) ) ;
68+ if ( tokenValue ) {
69+ setFiatAmount ( tokenValue * price ) ;
70+ }
71+ if ( fiatValue ) {
72+ setTokenAmount ( truncate ( fiatValue / price , token . decimals ) ) ;
73+ }
6874 } else {
75+
6976 // @ts -ignore
7077 if ( fiatValue ) setTokenAmount ( '' ) ;
7178 // @ts -ignore
@@ -74,25 +81,32 @@ const Buy = ({ lists, updateLists, onSeeOptions, onLoading }: BuyProps) => {
7481 } catch ( error ) {
7582 console . error ( error ) ;
7683 }
84+ changeInSelector = false ;
7785 updateLoading ( false ) ;
7886 } ;
7987
8088 useEffect ( ( ) => {
89+ changeInSelector = true ;
8190 search ( { fiatValue : fiatAmount , tokenValue : undefined } ) ;
8291 } , [ currency ] ) ;
8392
8493 useEffect ( ( ) => {
94+ changeInSelector = true ;
8595 search ( { fiatValue : undefined , tokenValue : tokenAmount } ) ;
8696 } , [ token ] ) ;
8797
8898 const onChangeFiat = ( val : number | undefined ) => {
99+ console . log ( "Fiat Change" ) ;
100+ if ( fiatAmount === val ) return ;
89101 setFiatAmount ( val ) ;
90102 if ( val && token && currency ) {
91103 search ( { fiatValue : val , tokenValue : undefined } ) ;
92104 }
93105 } ;
94106
95107 const onChangeToken = ( val : number | undefined ) => {
108+ console . log ( "Token Change" ) ;
109+ if ( tokenAmount === val ) return ;
96110 setTokenAmount ( val ) ;
97111 if ( val && token && currency ) {
98112 search ( { fiatValue : undefined , tokenValue : val } ) ;
@@ -194,4 +208,4 @@ const Buy = ({ lists, updateLists, onSeeOptions, onLoading }: BuyProps) => {
194208 ) ;
195209} ;
196210
197- export default Buy ;
211+ export default Buy ;
0 commit comments