forked from reactplay/react-play
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayHeaderActions.jsx
136 lines (125 loc) · 4.33 KB
/
PlayHeaderActions.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import React, { useCallback, useLayoutEffect, useState } from "react";
import { useAuthenticated, useUserId } from "@nhost/react";
import { BsGithub } from "react-icons/bs";
import { IoLogoYoutube } from "react-icons/io";
import { AiOutlineRead } from "react-icons/ai";
import { BiComment } from "react-icons/bi";
import { MdClose } from "react-icons/md";
import Like from "common/components/Like/Like";
import Comment from "common/components/Comment";
import useLikePlays from "common/hooks/useLikePlays";
import { NHOST } from "common/const";
import countByProp from "common/utils/countByProp";
const initialLikeObject = {
liked: false,
number: null,
interation: false,
};
const PlayHeaderActions = ({ play }) => {
const { play_like } = play;
const userId = useUserId();
const { likePlay, unLikePlay } = useLikePlays();
const [showComment, setShowComment] = useState(false);
const [likeObj, setLikeObj] = useState({ ...initialLikeObject });
const [loading, setLoading] = useState(false);
const isAuthenticated = useAuthenticated();
const constructLikeData = useCallback(
(userId) => {
if (!play_like?.length) return { liked: false, number: 0, interation: false };
const numberOfLikes = countByProp(play_like, "liked", true);
console.log("number of likes are", numberOfLikes);
const ifLiked = play_like.find((obj) => obj.user_id === userId);
return ifLiked
? ifLiked?.liked
? { liked: true, number: numberOfLikes, interation: true }
: { liked: false, number: numberOfLikes, interation: true }
: { liked: false, number: numberOfLikes, interation: false };
},
[play_like]
);
useLayoutEffect(() => {
setLikeObj(constructLikeData(userId));
}, [userId, constructLikeData]);
const handleLogin = (value) => {
return (window.location = NHOST.AUTH_URL(window.location.href, value));
};
const onLikeClick = async () => {
if (!isAuthenticated) return handleLogin("github");
try {
setLoading(true);
const mutationObj = { play_id: play.id, user_id: userId };
if (!likeObj.liked && !likeObj.interation) {
await likePlay(mutationObj);
setLikeObj((pre) => ({
liked: true,
number: pre.number + 1,
interation: true,
}));
} else {
await unLikePlay({ ...mutationObj, liked: !likeObj.liked });
setLikeObj((pre) => ({
liked: !pre.liked,
number: !pre.liked ? pre.number + 1 : pre.number - 1,
interation: true,
}));
}
} catch (err) {
console.log(err);
} finally {
return setLoading(false);
}
};
return (
<>
<Like onLikeClick={!loading ? onLikeClick : null} likeObj={likeObj} />
<button className='action badged' onClick={() => setShowComment(true)}>
<BiComment className='icon' size='24px' />
<span className='sr-only'>Comments</span>
</button>
{play.path && (
<a
target='_blank'
rel='noopener noreferrer'
className='action'
href={`https://github.com/reactplay/react-play/tree/main/src${play.path}`}
>
<BsGithub className='icon' size='24px' />
<span className='sr-only'>GitHub</span>
</a>
)}
<a
target='_blank'
rel='noopener noreferrer'
className='action'
href={
play.blog
? play.blog
: `https://github.com/reactplay/react-play/tree/main/src${play.path}/Readme.md`
}
>
<AiOutlineRead className='icon' size='24px' />
<span className='sr-only'>Blog</span>
</a>
{play.video && (
<a target='_blank' rel='noopener noreferrer' className='action' href={play.video}>
<IoLogoYoutube className='icon' size='24px' />
<span className='sr-only'>Video</span>
</a>
)}
{showComment && (
<div className='play-details-comments'>
<div className='comments-header'>
<h3 className='header-title'>Comments</h3>
<button className='header-action' onClick={() => setShowComment(false)}>
<MdClose size={24} className='icon' />
</button>
</div>
<div className='comments-body'>
<Comment />
</div>
</div>
)}
</>
);
};
export default PlayHeaderActions;