Files
Iris/src/js/components/Fields/PinButton.js

48 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-08-08 22:17:31 +12:00
import React from 'react';
2020-08-09 21:04:07 +12:00
import { useSelector, useDispatch } from 'react-redux'
2020-08-19 08:35:43 +12:00
import * as pusherActions from '../../services/pusher/actions';
2020-08-08 22:17:31 +12:00
import Icon from '../Icon';
import Button from '../Button';
2020-08-08 22:17:31 +12:00
const PinButton = ({ item }) => {
if (!item || !item.name) return null;
2020-08-08 22:17:31 +12:00
2020-08-09 21:04:07 +12:00
const {
addPinned,
2020-08-19 08:35:43 +12:00
removePinned,
} = pusherActions;
2020-08-08 22:17:31 +12:00
2020-08-09 21:04:07 +12:00
const dispatch = useDispatch();
const remove = () => dispatch(removePinned(item.uri));
const add = () => dispatch(addPinned(item));
const isPinned = useSelector((state) => (
2020-08-19 08:35:43 +12:00
state.pusher.pinned
? state.pusher.pinned.find((pinnedItem) => pinnedItem.uri === item.uri)
: false
));
2020-08-08 22:17:31 +12:00
2020-08-09 21:04:07 +12:00
if (isPinned) {
2020-08-08 22:17:31 +12:00
return (
<Button
icon
destructiveHover
2020-08-09 21:04:07 +12:00
onClick={remove}
tracking={{ category: 'PinButton', action: 'Pin' }}
2020-08-08 22:17:31 +12:00
>
2020-08-09 21:04:07 +12:00
<Icon name="star" />
</Button>
2020-08-08 22:17:31 +12:00
);
}
2020-08-09 21:04:07 +12:00
return (
<Button
icon
2020-08-09 21:04:07 +12:00
onClick={add}
tracking={{ category: 'PinButton', action: 'Unpin' }}
2020-08-09 21:04:07 +12:00
>
<Icon name="star_border" />
</Button>
2020-08-09 21:04:07 +12:00
);
};
export default PinButton;