| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- import React from 'react';
- import Icon from './Common/Icon'
- export default class BookmarkButton extends React.Component {
- constructor(props) {
- super(props);
- this.state = {
- bookmarked: false,
- token: null,
- };
- this.handleClick = this.handleClick.bind(this);
- }
- componentWillMount() {
- // FIXME(property?)
- this.setState({
- token: $('#bookmark-button').data('csrftoken'),
- });
- }
- componentDidMount() {
- this.props.crowi.apiGet('/bookmarks.get', {page_id: this.props.pageId})
- .then(res => {
- if (res.bookmark) {
- this.markBookmarked();
- }
- });
- }
- handleClick(event) {
- event.preventDefault();
- const token = this.state.token;
- const pageId = this.props.pageId;
- if (!this.state.bookmarked) {
- this.props.crowi.apiPost('/bookmarks.add', {_csrf: token, page_id: pageId})
- .then(res => {
- this.markBookmarked();
- });
- } else {
- this.props.crowi.apiPost('/bookmarks.remove', {_csrf: token, page_id: pageId})
- .then(res => {
- this.markUnBookmarked();
- });
- }
- }
- markBookmarked() {
- this.setState({bookmarked: true});
- }
- markUnBookmarked() {
- this.setState({bookmarked: false});
- }
- render() {
- const iconName = this.state.bookmarked ? 'star' : 'star-o';
- return (
- <a href="#" title="Bookmark" className="bookmark-link" onClick={this.handleClick}>
- <Icon name={iconName} />
- </a>
- );
- }
- }
- BookmarkButton.propTypes = {
- pageId: React.PropTypes.string,
- crowi: React.PropTypes.object.isRequired,
- };
|