mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 10:34:17 +00:00
compute state like by a function
This commit is contained in:
parent
8c3e9a504b
commit
ec6003558d
@ -4,8 +4,32 @@ import classnames from "classnames"
|
|||||||
import "./index.less"
|
import "./index.less"
|
||||||
|
|
||||||
export default (props) => {
|
export default (props) => {
|
||||||
|
const [liked, setLiked] = React.useState(props.liked)
|
||||||
const [clicked, setClicked] = React.useState(false)
|
const [clicked, setClicked] = React.useState(false)
|
||||||
|
|
||||||
|
// TODO: Support handle like change on websocket event
|
||||||
|
if (typeof props.watchWs === "object") {
|
||||||
|
// useWsEvents({
|
||||||
|
// [props.watchWs.event]: (data) => {
|
||||||
|
// handleUpdateTrackLike(data.track_id, data.action === "liked")
|
||||||
|
// }
|
||||||
|
// }, {
|
||||||
|
// socketName: props.watchWs.socket,
|
||||||
|
// })
|
||||||
|
}
|
||||||
|
|
||||||
|
async function computeLikedState() {
|
||||||
|
if (typeof props.liked === "function") {
|
||||||
|
let result = await props.liked()
|
||||||
|
|
||||||
|
result = result.liked ?? result
|
||||||
|
|
||||||
|
return setLiked(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
return setLiked(props.liked)
|
||||||
|
}
|
||||||
|
|
||||||
const handleClick = () => {
|
const handleClick = () => {
|
||||||
setClicked(true)
|
setClicked(true)
|
||||||
|
|
||||||
@ -16,13 +40,19 @@ export default (props) => {
|
|||||||
if (typeof props.onClick === "function") {
|
if (typeof props.onClick === "function") {
|
||||||
props.onClick()
|
props.onClick()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setLiked(!liked)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
computeLikedState()
|
||||||
|
}, [props.liked])
|
||||||
|
|
||||||
return <button
|
return <button
|
||||||
className={classnames(
|
className={classnames(
|
||||||
"likeButton",
|
"likeButton",
|
||||||
{
|
{
|
||||||
["liked"]: props.liked,
|
["liked"]: liked,
|
||||||
["clicked"]: clicked,
|
["clicked"]: clicked,
|
||||||
}
|
}
|
||||||
)}
|
)}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user