2020-11-27 20:51:31 -09:00
|
|
|
import React, { useCallback } from 'react'
|
2020-08-22 19:41:25 -08:00
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
|
import StarIcon from '@material-ui/icons/Star'
|
|
|
|
|
import StarBorderIcon from '@material-ui/icons/StarBorder'
|
|
|
|
|
import IconButton from '@material-ui/core/IconButton'
|
|
|
|
|
import { makeStyles } from '@material-ui/core/styles'
|
2020-11-27 20:51:31 -09:00
|
|
|
import { useToggleStar } from './useToggleStar'
|
2020-08-22 19:41:25 -08:00
|
|
|
|
|
|
|
|
const useStyles = makeStyles({
|
|
|
|
|
star: {
|
|
|
|
|
color: (props) => props.color,
|
2020-11-26 11:08:26 -09:00
|
|
|
visibility: (props) =>
|
2020-11-27 12:24:22 -09:00
|
|
|
props.visible === false
|
2020-11-26 11:08:26 -09:00
|
|
|
? 'hidden'
|
2020-11-27 12:24:22 -09:00
|
|
|
: props.starred
|
|
|
|
|
? 'visible'
|
2020-11-26 11:08:26 -09:00
|
|
|
: 'inherit',
|
2020-08-22 19:41:25 -08:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
2020-11-13 17:11:24 -09:00
|
|
|
export const StarButton = ({
|
|
|
|
|
resource,
|
|
|
|
|
record,
|
|
|
|
|
color,
|
|
|
|
|
visible,
|
|
|
|
|
size,
|
|
|
|
|
component: Button,
|
|
|
|
|
addLabel,
|
2020-11-27 20:51:31 -09:00
|
|
|
disabled,
|
2020-11-13 17:11:24 -09:00
|
|
|
...rest
|
|
|
|
|
}) => {
|
2020-08-22 19:41:25 -08:00
|
|
|
const classes = useStyles({ color, visible, starred: record.starred })
|
2020-11-27 20:51:31 -09:00
|
|
|
const [toggleStar, loading] = useToggleStar(resource, record)
|
2020-08-22 19:41:25 -08:00
|
|
|
|
2020-11-27 20:51:31 -09:00
|
|
|
const handleToggleStar = useCallback(
|
|
|
|
|
(e) => {
|
|
|
|
|
e.preventDefault()
|
|
|
|
|
toggleStar()
|
|
|
|
|
e.stopPropagation()
|
|
|
|
|
},
|
|
|
|
|
[toggleStar]
|
|
|
|
|
)
|
2020-08-22 19:41:25 -08:00
|
|
|
|
|
|
|
|
return (
|
2020-11-13 17:11:24 -09:00
|
|
|
<Button
|
2020-08-22 19:41:25 -08:00
|
|
|
onClick={handleToggleStar}
|
|
|
|
|
size={'small'}
|
2020-11-27 20:51:31 -09:00
|
|
|
disabled={disabled || loading}
|
2020-08-22 19:41:25 -08:00
|
|
|
className={classes.star}
|
2020-11-13 17:11:24 -09:00
|
|
|
{...rest}
|
2020-08-22 19:41:25 -08:00
|
|
|
>
|
|
|
|
|
{record.starred ? (
|
|
|
|
|
<StarIcon fontSize={size} />
|
|
|
|
|
) : (
|
|
|
|
|
<StarBorderIcon fontSize={size} />
|
|
|
|
|
)}
|
2020-11-13 17:11:24 -09:00
|
|
|
</Button>
|
2020-08-22 19:41:25 -08:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StarButton.propTypes = {
|
|
|
|
|
resource: PropTypes.string.isRequired,
|
|
|
|
|
record: PropTypes.object.isRequired,
|
|
|
|
|
visible: PropTypes.bool,
|
|
|
|
|
color: PropTypes.string,
|
|
|
|
|
size: PropTypes.string,
|
2020-11-13 17:11:24 -09:00
|
|
|
component: PropTypes.object,
|
2020-11-27 20:51:31 -09:00
|
|
|
disabled: PropTypes.bool,
|
2020-08-22 19:41:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StarButton.defaultProps = {
|
2020-08-25 15:07:51 -08:00
|
|
|
addLabel: true,
|
2020-08-24 13:50:33 -08:00
|
|
|
record: {},
|
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,
|
2020-11-27 20:51:31 -09:00
|
|
|
disabled: false,
|
2020-08-22 19:41:25 -08:00
|
|
|
}
|