$(document).ready(function () {
$('form').submit(function(e) {
const $submitedForm = $(this)
e.preventDefault();
$.ajax({
url: './php/send.php',
type: 'GET',
data: $submitedForm.serialize(),
success: function(response) {
$('.filter_data').html(response);
}
});
});
});
const [ reCaptchaState, setReCaptchaState ] = useState( {
callback: 'not fired',
value: false,
load: false,
expired: false,
recaptchaLoaded: false,
siteKey: getCaptchaSiteKey(),
delay: 300
} )
const reCaptchaRef = useRef();
const reCaptchaHandler = {
onChange: ( e ) => {
console.log( 'reCaptcha onChange', e );
},
asyncScriptOnLoad: ( e ) => {
setReCaptchaState( ( prev ) => {
return {
...prev,
callback: "called!",
recaptchaLoaded: true,
}
} )
console.log( "scriptLoad - reCaptcha Ref-", reCaptchaRef );
console.log( "scriptLoad - reCaptcha e", e );
},
execute: () => {
// reCaptchaRef.current.execute();
},
executeAsync: ( submitFormCallback ) => {
reCaptchaRef.current.executeAsync()
.then( value => {
if ( value ){
setReCaptchaState( ( prev ) => {
return {
...prev,
value: value
}
} )
submitFormCallback( value )
console.log( "executeAsync promise - Captcha value:", value );
} //
else{
console.log( "executeAsync promise error:", value );
}
} )
.catch( err => {
console.log( "executeAsync error", err );
} );
},
onExpired: ( e ) => {
setReCaptchaState( ( prev ) => {
return {
...prev,
expired: true
}
} )
console.log( 'reCaptcha expire ' );
},
onErrored: ( e ) => {
setReCaptchaState( ( prev ) => {
return {
...prev,
error: true
}
} )
console.log( 'reCaptcha error' );
}
}
...
useEffect( () => {
let isMounted = true
setTimeout( () => {
if ( isMounted ){
setReCaptchaState( ( prev ) => {
return {
...prev,
load: true
}
} )
}
}, reCaptchaState.delay )
return () => {
isMounted = false
}
}, [] )
...
{ !reCaptchaState.recaptchaLoaded && <Preloader/> }
...
{ reCaptchaState.load &&
<div className='recaptchaContainer'>
<ReCAPTCHA
ref={ reCaptchaRef }
theme="dark"
size="invisible"
sitekey={ reCaptchaState.siteKey }
onChange={ reCaptchaHandler.onChange }
onExpired={ reCaptchaHandler.onExpired }
onErrored={ reCaptchaHandler.onErrored }
asyncScriptOnLoad={ reCaptchaHandler.asyncScriptOnLoad }
/>
</div>
}
export const fetchDataNew = (id:string) =>{ return async(dispatch: Dispatch<DataActionNew>)=>{ try { dispatch({type:DataActionTypes.FETCH_DATA}) const url = `http://localhost:5000/api/new/${id}` const response = await axios.get(url) dispatch({type:DataActionTypes.FETCH_DATA_SUCCESS,payload:response.data}) } catch (error) { return dispatch({ type:DataActionTypes.FETCH_DATA_ERROR, payload:"Произошла ошибка загрузки данных"}) } } }
export function foo( ) {
return dispatch => {
API().then( responseData => {
dispatch( ... )
} ).catch( errorData => {
dispatch( ... )
} )
}
}
const addOneComponent = (newComponent) => {
setComponents(prev => (
const newItem = {
id: prev.length+1,
Component: newComponent
}
return [...prev, newItem]
)};
{components.map((item => (
<item.Component key={item.id} id={item.id} onRemove={removeComponent} />
))}
function removeComponent(id) {
setComponents(components.filter((p) => p.id !== id);
}
<img
src={removeComponent}
onClick={()=>props.onRemove(props.id)}
/>
import React, { useLayoutEffect, useState } from 'react';
export const useWindowSize = () => {
const [ size, setSize ] = useState( {
innerWidth: 0,
innerHeight: 0,
outerWidth: 0,
outerHeight: 0
} );
useLayoutEffect( () => {
const updateSize = () => {
setSize( () => {
return {
innerWidth: window.innerWidth,
innerHeight: window.innerHeight,
outerWidth: window.outerWidth,
outerHeight: window.outerHeight,
}
} );
}
window.addEventListener( 'resize', updateSize );
updateSize();
return () => window.removeEventListener( 'resize', updateSize );
}, [] );
return size;
}