-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplause-button.js
66 lines (55 loc) · 1.56 KB
/
applause-button.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import React, {useState, useEffect} from "react"
import axios from "axios"
const API = "https://api.applause-button.com"
const VERSION = "3.0.0"
const mainUrl = "https://pat.chormai.org"
const HEADERS = {
"Content-Type": "text/plain"
}
const getClaps = async (url) => {
const query = url ? `?url=${url}` : "";
return await axios.get(`${API}/get-claps${query}`, {
headers: HEADERS
});
}
const updateClaps = async (url, claps=1) => {
console.log(claps)
const query = url ? `?url=${url}` : "";
return await axios.post(`${API}/update-claps${query}`,
JSON.stringify(`${claps},${VERSION}`), {
headers: HEADERS
});
}
const ApplauseButton = ({url=mainUrl}) => {
const [count, setCount] = useState(0)
const [isClapped, setIsClapped] = useState(false)
const doApplause = () => {
if(!isClapped){
console.log('do clapping')
const callBackend = async () => {
const result = await updateClaps(url, 1)
setCount(result.data)
setIsClapped(true)
}
callBackend()
}
}
useEffect(() => {
const fetchData = async () => {
const result = await getClaps(url)
console.log(result)
setCount(result.data)
}
fetchData()
}, []);
return <span style={{
cursor: "pointer",
padding: "10px",
}} onClick={doApplause}>
{
isClapped ? "🤟" : "👏"
}
{` ${count}`}
</span>
}
export default ApplauseButton