Commit da333b79 authored by Lyza Danger Gardner's avatar Lyza Danger Gardner

Make `AnnotationActionBar` create a new draft on edit

Let `AnnotationActionBar` handle “entering edit mode” by way of
creating a new draft for the relevant annotation. Remove this logic
from `Annotation` and the associated `prop`
parent d77ca41f
...@@ -3,7 +3,7 @@ import propTypes from 'prop-types'; ...@@ -3,7 +3,7 @@ import propTypes from 'prop-types';
import useStore from '../store/use-store'; import useStore from '../store/use-store';
import { isShareable, shareURI } from '../util/annotation-sharing'; import { isShareable, shareURI } from '../util/annotation-sharing';
import { permits } from '../util/permissions'; import { isPrivate, permits } from '../util/permissions';
import { withServices } from '../util/service-context'; import { withServices } from '../util/service-context';
import AnnotationShareControl from './annotation-share-control'; import AnnotationShareControl from './annotation-share-control';
...@@ -17,7 +17,6 @@ function AnnotationActionBar({ ...@@ -17,7 +17,6 @@ function AnnotationActionBar({
annotation, annotation,
annotationMapper, annotationMapper,
flash, flash,
onEdit,
onReply, onReply,
settings, settings,
}) { }) {
...@@ -37,6 +36,7 @@ function AnnotationActionBar({ ...@@ -37,6 +36,7 @@ function AnnotationActionBar({
const showFlagAction = userProfile.userid !== annotation.user; const showFlagAction = userProfile.userid !== annotation.user;
const showShareAction = isShareable(annotation, settings); const showShareAction = isShareable(annotation, settings);
const createDraft = useStore(store => store.createDraft);
const updateFlagFn = useStore(store => store.updateFlagStatus); const updateFlagFn = useStore(store => store.updateFlagStatus);
const updateFlag = () => { const updateFlag = () => {
...@@ -51,6 +51,14 @@ function AnnotationActionBar({ ...@@ -51,6 +51,14 @@ function AnnotationActionBar({
} }
}; };
const onEdit = () => {
createDraft(annotation, {
tags: annotation.tags,
text: annotation.text,
isPrivate: isPrivate(annotation.permissions),
});
};
const onFlag = () => { const onFlag = () => {
if (!userProfile.userid) { if (!userProfile.userid) {
flash.error( flash.error(
...@@ -100,7 +108,6 @@ function AnnotationActionBar({ ...@@ -100,7 +108,6 @@ function AnnotationActionBar({
AnnotationActionBar.propTypes = { AnnotationActionBar.propTypes = {
annotation: propTypes.object.isRequired, annotation: propTypes.object.isRequired,
/** Callbacks for when action buttons are clicked/tapped */ /** Callbacks for when action buttons are clicked/tapped */
onEdit: propTypes.func.isRequired,
onReply: propTypes.func.isRequired, onReply: propTypes.func.isRequired,
// Injected services // Injected services
......
...@@ -59,7 +59,6 @@ function AnnotationOmega({ ...@@ -59,7 +59,6 @@ function AnnotationOmega({
}; };
// TODO // TODO
const fakeOnEdit = () => alert('Enter edit mode: TBD');
const fakeOnReply = () => alert('Reply: TBD'); const fakeOnReply = () => alert('Reply: TBD');
const fakeOnRevert = () => alert('Revert changes: TBD'); const fakeOnRevert = () => alert('Revert changes: TBD');
const fakeOnSave = () => alert('Save changes: TBD'); const fakeOnSave = () => alert('Save changes: TBD');
...@@ -98,7 +97,6 @@ function AnnotationOmega({ ...@@ -98,7 +97,6 @@ function AnnotationOmega({
<div className="annotation-actions"> <div className="annotation-actions">
<AnnotationActionBar <AnnotationActionBar
annotation={annotation} annotation={annotation}
onEdit={fakeOnEdit}
onReply={fakeOnReply} onReply={fakeOnReply}
/> />
</div> </div>
......
...@@ -4,13 +4,13 @@ import { act } from 'preact/test-utils'; ...@@ -4,13 +4,13 @@ import { act } from 'preact/test-utils';
import AnnotationActionBar from '../annotation-action-bar'; import AnnotationActionBar from '../annotation-action-bar';
import { $imports } from '../annotation-action-bar'; import { $imports } from '../annotation-action-bar';
import * as fixtures from '../../test/annotation-fixtures';
import mockImportedComponents from './mock-imported-components'; import mockImportedComponents from './mock-imported-components';
import { waitFor } from './util'; import { waitFor } from './util';
describe('AnnotationActionBar', () => { describe('AnnotationActionBar', () => {
let fakeAnnotation; let fakeAnnotation;
let fakeOnEdit;
let fakeOnReply; let fakeOnReply;
let fakeUserProfile; let fakeUserProfile;
...@@ -30,7 +30,6 @@ describe('AnnotationActionBar', () => { ...@@ -30,7 +30,6 @@ describe('AnnotationActionBar', () => {
annotation={fakeAnnotation} annotation={fakeAnnotation}
annotationMapper={fakeAnnotationMapper} annotationMapper={fakeAnnotationMapper}
flash={fakeFlash} flash={fakeFlash}
onEdit={fakeOnEdit}
onReply={fakeOnReply} onReply={fakeOnReply}
settings={fakeSettings} settings={fakeSettings}
{...props} {...props}
...@@ -56,12 +55,7 @@ describe('AnnotationActionBar', () => { ...@@ -56,12 +55,7 @@ describe('AnnotationActionBar', () => {
}; };
beforeEach(() => { beforeEach(() => {
fakeAnnotation = { fakeAnnotation = fixtures.defaultAnnotation();
group: 'fakegroup',
permissions: {},
user: 'acct:bar@foo.com',
};
fakeUserProfile = { fakeUserProfile = {
userid: 'account:foo@bar.com', userid: 'account:foo@bar.com',
}; };
...@@ -76,7 +70,6 @@ describe('AnnotationActionBar', () => { ...@@ -76,7 +70,6 @@ describe('AnnotationActionBar', () => {
error: sinon.stub(), error: sinon.stub(),
}; };
fakeOnEdit = sinon.stub();
fakeOnReply = sinon.stub(); fakeOnReply = sinon.stub();
fakePermits = sinon.stub().returns(true); fakePermits = sinon.stub().returns(true);
...@@ -85,8 +78,9 @@ describe('AnnotationActionBar', () => { ...@@ -85,8 +78,9 @@ describe('AnnotationActionBar', () => {
fakeIsShareable = sinon.stub().returns(true); fakeIsShareable = sinon.stub().returns(true);
fakeStore = { fakeStore = {
profile: sinon.stub().returns(fakeUserProfile), createDraft: sinon.stub(),
getGroup: sinon.stub().returns({}), getGroup: sinon.stub().returns({}),
profile: sinon.stub().returns(fakeUserProfile),
updateFlagStatus: sinon.stub(), updateFlagStatus: sinon.stub(),
}; };
...@@ -115,13 +109,20 @@ describe('AnnotationActionBar', () => { ...@@ -115,13 +109,20 @@ describe('AnnotationActionBar', () => {
assert.isTrue(getButton(wrapper, 'edit').exists()); assert.isTrue(getButton(wrapper, 'edit').exists());
}); });
it('invokes `onEdit` callback when edit button clicked', () => { it('creates a new draft when `Edit` button clicked', () => {
allowOnly('update'); allowOnly('update');
const button = getButton(createComponent(), 'edit'); const button = getButton(createComponent(), 'edit');
button.props().onClick(); button.props().onClick();
assert.calledOnce(fakeOnEdit); const call = fakeStore.createDraft.getCall(0);
assert.calledOnce(fakeStore.createDraft);
assert.equal(call.args[0], fakeAnnotation);
assert.include(call.args[1], {
isPrivate: false,
text: fakeAnnotation.text,
});
assert.isArray(call.args[1].tags);
}); });
it('does not show edit button if permissions do not allow', () => { it('does not show edit button if permissions do not allow', () => {
......
...@@ -66,7 +66,6 @@ ...@@ -66,7 +66,6 @@
<div class="annotation-actions" ng-if="!vm.isSaving && !vm.editing() && vm.id()"> <div class="annotation-actions" ng-if="!vm.isSaving && !vm.editing() && vm.id()">
<annotation-action-bar <annotation-action-bar
annotation="vm.annotation" annotation="vm.annotation"
on-edit="vm.edit()"
on-reply="vm.reply()"></annotation-action-bar> on-reply="vm.reply()"></annotation-action-bar>
</div> </div>
</footer> </footer>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment