delete button working (#274)

Fixes #274.
This commit is contained in:
americool 2017-03-11 13:47:36 -05:00 committed by David Calavera
parent 8ffe6730fa
commit 3c7b8d2322
6 changed files with 68 additions and 2 deletions

View File

@ -271,6 +271,27 @@ export function updateUnpublishedEntryStatus(collection, slug, oldStatus, newSta
};
}
export function deleteUnpublishedEntry(collection, slug) {
return (dispatch, getState) => {
const state = getState();
const backend = currentBackend(state.config);
const transactionID = uuid.v4();
dispatch(unpublishedEntryPublishRequest(collection, slug, transactionID));
backend.deleteUnpublishedEntry(collection, slug)
.then(() => {
dispatch(unpublishedEntryPublished(collection, slug, transactionID));
})
.catch((error) => {
dispatch(notifSend({
message: `Failed to close PR: ${ error }`,
kind: 'danger',
dismissAfter: 8000,
}));
dispatch(unpublishedEntryPublishError(collection, slug, transactionID));
});
};
}
export function publishUnpublishedEntry(collection, slug) {
return (dispatch, getState) => {
const state = getState();

View File

@ -200,6 +200,9 @@ class Backend {
return this.implementation.publishUnpublishedEntry(collection, slug);
}
deleteUnpublishedEntry(collection, slug) {
return this.implementation.deleteUnpublishedEntry(collection, slug);
}
entryToRaw(collection, entry) {
const format = resolveFormat(collection, entry.toJS());

View File

@ -314,6 +314,14 @@ export default class API {
.then(updatedMetadata => this.storeMetadata(contentKey, updatedMetadata));
}
deleteUnpublishedEntry(collection, slug) {
const contentKey = slug;
let prNumber;
return this.retrieveMetadata(contentKey)
.then(metadata => this.closePR(metadata.pr, metadata.objects))
.then(() => this.deleteBranch(`cms/${ contentKey }`));
}
publishUnpublishedEntry(collection, slug) {
const contentKey = slug;
let prNumber;
@ -367,6 +375,18 @@ export default class API {
});
}
closePR(pullrequest, objects) {
const headSha = pullrequest.head;
const prNumber = pullrequest.number;
console.log("%c Deleting PR", "line-height: 30px;text-align: center;font-weight: bold"); // eslint-disable-line
return this.request(`${ this.repoURL }/pulls/${ prNumber }`, {
method: "PATCH",
body: JSON.stringify({
state: closed,
}),
});
}
mergePR(pullrequest, objects) {
const headSha = pullrequest.head;
const prNumber = pullrequest.number;

View File

@ -135,6 +135,9 @@ export default class GitHub {
return this.api.updateUnpublishedEntryStatus(collection, slug, newStatus);
}
deleteUnpublishedEntry(collection, slug) {
return this.api.deleteUnpublishedEntry(collection, slug);
}
publishUnpublishedEntry(collection, slug) {
return this.api.publishUnpublishedEntry(collection, slug);
}

View File

@ -13,6 +13,7 @@ class UnpublishedListing extends React.Component {
entries: ImmutablePropTypes.orderedMap,
handleChangeStatus: PropTypes.func.isRequired,
handlePublish: PropTypes.func.isRequired,
handleDelete: PropTypes.func.isRequired,
};
handleChangeStatus = (newStatus, dragProps) => {
@ -22,6 +23,11 @@ class UnpublishedListing extends React.Component {
this.props.handleChangeStatus(collection, slug, oldStatus, newStatus);
};
requestDelete = (collection, slug, ownStatus) => {
if (window.confirm('Are you sure you want to delete this entry?')) {
this.props.handleDelete(collection, slug, ownStatus);
}
};
requestPublish = (collection, slug, ownStatus) => {
if (ownStatus !== status.last()) return;
if (window.confirm('Are you sure you want to publish this entry?')) {
@ -82,6 +88,10 @@ class UnpublishedListing extends React.Component {
<Link to={link}>
<Button>Edit</Button>
</Link>
<Button
onClick={this.requestDelete.bind(this, collection, slug, ownStatus)}>
Delete
</Button>
{
(ownStatus === status.last() && !entry.get('isPersisting', false)) &&
<Button

View File

@ -2,7 +2,12 @@ import React, { Component, PropTypes } from 'react';
import ImmutablePropTypes from 'react-immutable-proptypes';
import { OrderedMap } from 'immutable';
import { connect } from 'react-redux';
import { loadUnpublishedEntries, updateUnpublishedEntryStatus, publishUnpublishedEntry } from '../../actions/editorialWorkflow';
import {
loadUnpublishedEntries,
updateUnpublishedEntryStatus,
publishUnpublishedEntry,
deleteUnpublishedEntry
} from '../../actions/editorialWorkflow';
import { selectUnpublishedEntriesByStatus } from '../../reducers';
import { EDITORIAL_WORKFLOW, status } from '../../constants/publishModes';
import UnpublishedListing from '../../components/UnpublishedListing/UnpublishedListing';
@ -16,6 +21,7 @@ class unpublishedEntriesPanel extends Component {
loadUnpublishedEntries: PropTypes.func.isRequired,
updateUnpublishedEntryStatus: PropTypes.func.isRequired,
publishUnpublishedEntry: PropTypes.func.isRequired,
deleteUnpublishedEntry: PropTypes.func.isRequired,
};
componentDidMount() {
@ -26,14 +32,16 @@ class unpublishedEntriesPanel extends Component {
}
render() {
const { isEditorialWorkflow, isFetching, unpublishedEntries, updateUnpublishedEntryStatus, publishUnpublishedEntry } = this.props;
const { isEditorialWorkflow, isFetching, unpublishedEntries, updateUnpublishedEntryStatus, publishUnpublishedEntry, deleteUnpublishedEntry } = this.props;
if (!isEditorialWorkflow) return null;
if (isFetching) return <Loader active>Loading Editorial Workflow Entries</Loader>;
return (
<UnpublishedListing
entries={unpublishedEntries}
handleChangeStatus={updateUnpublishedEntryStatus}
handlePublish={publishUnpublishedEntry}
handleDelete={deleteUnpublishedEntry}
/>
);
}
@ -62,4 +70,5 @@ export default connect(mapStateToProps, {
loadUnpublishedEntries,
updateUnpublishedEntryStatus,
publishUnpublishedEntry,
deleteUnpublishedEntry,
})(unpublishedEntriesPanel);