quintodrome/ui/src/common/LoveButton.jsx

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

80 lines
1.8 KiB
React
Raw Normal View History

import React, { useCallback } from 'react'
2020-08-22 19:41:25 -08:00
import PropTypes from 'prop-types'
import FavoriteIcon from '@material-ui/icons/Favorite'
import FavoriteBorderIcon from '@material-ui/icons/FavoriteBorder'
2020-08-22 19:41:25 -08:00
import IconButton from '@material-ui/core/IconButton'
import { makeStyles } from '@material-ui/core/styles'
import { useToggleLove } from './useToggleLove'
import { useRecordContext } from 'react-admin'
import config from '../config'
2020-08-22 19:41:25 -08:00
const useStyles = makeStyles({
love: {
2020-08-22 19:41:25 -08:00
color: (props) => props.color,
visibility: (props) =>
props.visible === false ? 'hidden' : props.loved ? 'visible' : 'inherit',
2020-08-22 19:41:25 -08:00
},
})
export const LoveButton = ({
2020-11-13 17:11:24 -09:00
resource,
color,
visible,
size,
component: Button,
addLabel,
disabled,
2020-11-13 17:11:24 -09:00
...rest
}) => {
const record = useRecordContext(rest) || {}
const classes = useStyles({ color, visible, loved: record.starred })
const [toggleLove, loading] = useToggleLove(resource, record)
2020-08-22 19:41:25 -08:00
const handleToggleLove = useCallback(
(e) => {
e.preventDefault()
toggleLove()
e.stopPropagation()
},
[toggleLove],
)
2020-08-22 19:41:25 -08:00
if (!config.enableFavourites) {
return <></>
}
2020-08-22 19:41:25 -08:00
return (
2020-11-13 17:11:24 -09:00
<Button
onClick={handleToggleLove}
2020-08-22 19:41:25 -08:00
size={'small'}
disabled={disabled || loading}
className={classes.love}
2020-11-13 17:11:24 -09:00
{...rest}
2020-08-22 19:41:25 -08:00
>
{record.starred ? (
<FavoriteIcon fontSize={size} />
2020-08-22 19:41:25 -08:00
) : (
<FavoriteBorderIcon fontSize={size} />
2020-08-22 19:41:25 -08:00
)}
2020-11-13 17:11:24 -09:00
</Button>
2020-08-22 19:41:25 -08:00
)
}
LoveButton.propTypes = {
2020-08-22 19:41:25 -08:00
resource: PropTypes.string.isRequired,
record: PropTypes.object,
2020-08-22 19:41:25 -08:00
visible: PropTypes.bool,
color: PropTypes.string,
size: PropTypes.string,
2020-11-13 17:11:24 -09:00
component: PropTypes.object,
disabled: PropTypes.bool,
2020-08-22 19:41:25 -08:00
}
LoveButton.defaultProps = {
2020-08-25 15:07:51 -08:00
addLabel: true,
2020-08-22 19:41:25 -08:00
visible: true,
size: 'small',
2020-08-24 15:38:29 -08:00
color: 'inherit',
2020-11-13 17:11:24 -09:00
component: IconButton,
disabled: false,
2020-08-22 19:41:25 -08:00
}