-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhotoUnlockerRCv2.jsx
49 lines (42 loc) · 1.49 KB
/
PhotoUnlockerRCv2.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import PropTypes from 'prop-types';
import { useState } from 'react';
import Reaptcha from 'reaptcha';
import ConfettiAnimation from './ConfettiAnimation';
import { getBlurredPhotoUrl, getRandomPhotoId } from './LoremIpsumPhotos';
import { requestUnlockPhoto } from './Requests';
PhotoUnlockerRCv2.propTypes = {
reCaptchaAction: PropTypes.string.isRequired,
reCaptchaKey: PropTypes.string.isRequired,
};
function PhotoUnlockerRCv2(props) {
const [photoId, setPhotoId] = useState(getRandomPhotoId());
const [unlockedPhotoSrc, setUnlockedPhotoSrc] = useState('');
const lockAgain = () => {
setUnlockedPhotoSrc('');
setPhotoId(getRandomPhotoId);
};
const unlockPhoto = async (photoId, captchaToken) => {
await requestUnlockPhoto({
recaptchaVersion: 'v2',
photoId,
captchaToken,
onUnlock: setUnlockedPhotoSrc,
onError: (e) => { lockAgain(); console.log(e); },
});
};
return (
<>
{unlockedPhotoSrc && <ConfettiAnimation onComplete={lockAgain} />}
<div className='locked-photo d-flex align-items-center justify-content-center user-select-none'
onClick={lockAgain}
style={{ backgroundImage: `url(${unlockedPhotoSrc || getBlurredPhotoUrl(photoId)})` }}
>
{unlockedPhotoSrc ? '' :
<Reaptcha onVerify={(captchaToken) => unlockPhoto(photoId, captchaToken)}
sitekey={props.reCaptchaKey} />
}
</div>
</>
);
}
export default PhotoUnlockerRCv2;