2017-09-10 05:51:22 +00:00
|
|
|
|
// @flow
|
2020-06-20 20:59:15 +00:00
|
|
|
|
import * as React from "react";
|
|
|
|
|
import { withRouter, type RouterHistory } from "react-router-dom";
|
|
|
|
|
import { observable } from "mobx";
|
|
|
|
|
import { inject, observer } from "mobx-react";
|
|
|
|
|
import Button from "components/Button";
|
|
|
|
|
import Flex from "shared/components/Flex";
|
|
|
|
|
import HelpText from "components/HelpText";
|
|
|
|
|
import Document from "models/Document";
|
|
|
|
|
import DocumentsStore from "stores/DocumentsStore";
|
|
|
|
|
import UiStore from "stores/UiStore";
|
|
|
|
|
import { collectionUrl } from "utils/routeHelpers";
|
2017-09-10 05:51:22 +00:00
|
|
|
|
|
|
|
|
|
type Props = {
|
2019-08-09 06:09:09 +00:00
|
|
|
|
history: RouterHistory,
|
2017-09-10 05:51:22 +00:00
|
|
|
|
document: Document,
|
|
|
|
|
documents: DocumentsStore,
|
2018-12-05 06:24:30 +00:00
|
|
|
|
ui: UiStore,
|
2017-09-10 05:51:22 +00:00
|
|
|
|
onSubmit: () => void,
|
|
|
|
|
};
|
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
|
@observer
|
2018-05-05 23:16:08 +00:00
|
|
|
|
class DocumentDelete extends React.Component<Props> {
|
2017-09-10 05:51:22 +00:00
|
|
|
|
@observable isDeleting: boolean;
|
|
|
|
|
|
2019-08-09 06:09:09 +00:00
|
|
|
|
handleSubmit = async (ev: SyntheticEvent<>) => {
|
2017-09-10 05:51:22 +00:00
|
|
|
|
ev.preventDefault();
|
|
|
|
|
this.isDeleting = true;
|
|
|
|
|
|
2018-12-05 06:24:30 +00:00
|
|
|
|
try {
|
|
|
|
|
await this.props.document.delete();
|
2019-04-06 23:20:27 +00:00
|
|
|
|
if (this.props.ui.activeDocumentId === this.props.document.id) {
|
2019-04-18 02:11:23 +00:00
|
|
|
|
this.props.history.push(
|
|
|
|
|
collectionUrl(this.props.document.collectionId)
|
|
|
|
|
);
|
2019-04-06 23:20:27 +00:00
|
|
|
|
}
|
2017-09-10 05:51:22 +00:00
|
|
|
|
this.props.onSubmit();
|
2018-12-05 06:24:30 +00:00
|
|
|
|
} catch (err) {
|
|
|
|
|
this.props.ui.showToast(err.message);
|
|
|
|
|
} finally {
|
|
|
|
|
this.isDeleting = false;
|
2017-09-10 05:51:22 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { document } = this.props;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Flex column>
|
|
|
|
|
<form onSubmit={this.handleSubmit}>
|
|
|
|
|
<HelpText>
|
2020-06-20 20:59:15 +00:00
|
|
|
|
Are you sure about that? Deleting the{" "}
|
2019-11-19 02:51:32 +00:00
|
|
|
|
<strong>{document.title}</strong> document will delete all of its
|
2019-12-19 05:00:36 +00:00
|
|
|
|
history, and any nested documents.
|
2017-09-10 05:51:22 +00:00
|
|
|
|
</HelpText>
|
2019-04-06 23:20:27 +00:00
|
|
|
|
{!document.isDraft &&
|
|
|
|
|
!document.isArchived && (
|
|
|
|
|
<HelpText>
|
|
|
|
|
If you’d like the option of referencing or restoring this
|
|
|
|
|
document in the future, consider archiving it instead.
|
|
|
|
|
</HelpText>
|
|
|
|
|
)}
|
2017-09-10 05:51:22 +00:00
|
|
|
|
<Button type="submit" danger>
|
2020-06-20 20:59:15 +00:00
|
|
|
|
{this.isDeleting ? "Deleting…" : "I’m sure – Delete"}
|
2017-09-10 05:51:22 +00:00
|
|
|
|
</Button>
|
|
|
|
|
</form>
|
|
|
|
|
</Flex>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-20 20:59:15 +00:00
|
|
|
|
export default inject("documents", "ui")(withRouter(DocumentDelete));
|