fix purpose when missing

This commit is contained in:
Niqui O'Neill 2020-11-30 13:19:57 -08:00
parent 2caf83083f
commit 71aa9c128e
1 changed files with 13 additions and 9 deletions

View File

@ -9,7 +9,6 @@ const purposes = ['assessing', 'bookmarking', 'classifying', 'commenting', 'desc
* Comments are TextualBodies where the purpose field is either
* blank or 'commenting' or 'replying'
*/
const isComment = body =>
body.type === 'TextualBody' && (
!body.hasOwnProperty('purpose') || purposes.indexOf(body.purpose) > -1
@ -19,9 +18,11 @@ const isComment = body =>
* The draft reply is a comment body with a 'draft' flag
*/
const getDraftReply = (existingDraft, isReply) => {
return existingDraft ? existingDraft : {
type: 'TextualBody', value: '', purpose: isReply ? 'replying' : 'commenting', draft: true
let draft = existingDraft ? existingDraft : {
type: 'TextualBody', value: '', draft: true
};
draft.purpose = draft.purpose ? draft.purpose : isReply ? 'replying' : 'commenting';
return draft;
};
/**
@ -31,13 +32,11 @@ const CommentWidget = props => {
// All comments (draft + non-draft)
const all = props.annotation ?
props.annotation.bodies.filter(isComment) : [];
// Last draft comment without a creator field goes into the reply field
const draftReply = getDraftReply(all.slice().reverse().find(b => b.draft && !b.creator), all.length > 1);
// All except draft reply
const comments = all.filter(b => b != draftReply);
let comments = all.filter(b => b != draftReply);
comments = comments.map(elem => getDraftReply(elem));
const onEditReply = evt => {
const prev = draftReply.value;
const updated = evt.target.value;
@ -54,8 +53,13 @@ const CommentWidget = props => {
const onUpdatePurpose = evt => {
const prev = draftReply.purpose.trim();
const updated = evt.value.trim();
if (draftReply.value == '' && updated.length > 0) {
draftReply.purpose = updated;
this.setState({purpose: updated});
} else {
props.onUpdateBody(draftReply, { ...draftReply, purpose: updated });
}
}
// A comment should be read-only if:
// - the global read-only flag is set
@ -108,7 +112,7 @@ const CommentWidget = props => {
{ props.purpose == true &&
<TypeDropdown
editable={true}
content={draftReply.purpose ? draftReply.purpose : 'replying'}
content={draftReply.purpose}
onChange={onUpdatePurpose}
onSaveAndClose={props.onSaveAndClose}
/>