todos-contra-el-fuego-web/imports/ui/pages/ViewDocument/ViewDocument.js
2017-05-25 16:31:01 -05:00

54 lines
1.7 KiB
JavaScript

import React from 'react';
import PropTypes from 'prop-types';
import { ButtonToolbar, ButtonGroup, Button } from 'react-bootstrap';
import { createContainer } from 'meteor/react-meteor-data';
import { Meteor } from 'meteor/meteor';
import { Bert } from 'meteor/themeteorchef:bert';
import Documents from '../../../api/Documents/Documents';
import NotFound from '../NotFound/NotFound';
const handleRemove = (documentId, history) => {
if (confirm('Are you sure? This is permanent!')) {
Meteor.call('documents.remove', documentId, (error) => {
if (error) {
Bert.alert(error.reason, 'danger');
} else {
Bert.alert('Document deleted!', 'success');
history.push('/documents');
}
});
}
};
const ViewDocument = ({ doc, match, history }) => (doc ? (
<div className="ViewDocument">
<div className="page-header clearfix">
<h4 className="pull-left">{ doc && doc.title }</h4>
<ButtonToolbar className="pull-right">
<ButtonGroup bsSize="small">
<Button onClick={() => history.push(`${match.url}/edit`)}>Edit</Button>
<Button onClick={() => handleRemove(doc._id, history)} className="text-danger">
Delete
</Button>
</ButtonGroup>
</ButtonToolbar>
</div>
{ doc && doc.body }
</div>
) : <NotFound />);
ViewDocument.propTypes = {
doc: PropTypes.object.isRequired,
match: PropTypes.object.isRequired,
history: PropTypes.object.isRequired,
};
export default createContainer(({ match }) => {
const documentId = match.params._id;
const subscription = Meteor.subscribe('documents.view', documentId);
return {
loading: !subscription.ready(),
doc: Documents.findOne(documentId),
};
}, ViewDocument);