|
|
import { translate } from 'i18n-calypso'; |
|
|
import { |
|
|
COMMENTS_DELETE, |
|
|
COMMENTS_RECEIVE, |
|
|
COMMENTS_COUNT_INCREMENT, |
|
|
COMMENTS_WRITE_ERROR, |
|
|
} from 'calypso/state/action-types'; |
|
|
import { requestCommentsList } from 'calypso/state/comments/actions'; |
|
|
import { bypassDataLayer } from 'calypso/state/data-layer/utils'; |
|
|
import { http } from 'calypso/state/data-layer/wpcom-http/actions'; |
|
|
import { errorNotice } from 'calypso/state/notices/actions'; |
|
|
import { getSitePost } from 'calypso/state/posts/selectors'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const createPlaceholderComment = ( commentText, postId, parentCommentId ) => ( { |
|
|
ID: 'placeholder-' + new Date().getTime(), |
|
|
parent: parentCommentId ? { ID: parentCommentId } : false, |
|
|
date: new Date().toISOString(), |
|
|
content: commentText, |
|
|
status: 'pending', |
|
|
type: 'comment', |
|
|
post: { ID: postId }, |
|
|
isPlaceholder: true, |
|
|
placeholderState: 'PENDING', |
|
|
} ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const dispatchNewCommentRequest = ( action, path ) => { |
|
|
const { siteId, postId, parentCommentId, commentText } = action; |
|
|
const placeholder = createPlaceholderComment( commentText, postId, parentCommentId ); |
|
|
|
|
|
|
|
|
return [ |
|
|
{ |
|
|
type: COMMENTS_RECEIVE, |
|
|
siteId, |
|
|
postId, |
|
|
comments: [ placeholder ], |
|
|
skipSort: !! parentCommentId, |
|
|
}, |
|
|
|
|
|
http( { |
|
|
method: 'POST', |
|
|
apiVersion: '1.1', |
|
|
path, |
|
|
body: { |
|
|
content: commentText, |
|
|
}, |
|
|
onSuccess: { |
|
|
...action, |
|
|
placeholderId: placeholder.ID, |
|
|
}, |
|
|
onFailure: { ...action, placeholderId: placeholder.ID }, |
|
|
} ), |
|
|
]; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const updatePlaceholderComment = ( |
|
|
{ siteId, postId, parentCommentId, placeholderId, refreshCommentListQuery }, |
|
|
comment |
|
|
) => { |
|
|
const actions = [ |
|
|
|
|
|
bypassDataLayer( { type: COMMENTS_DELETE, siteId, postId, commentId: placeholderId } ), |
|
|
|
|
|
{ |
|
|
type: COMMENTS_RECEIVE, |
|
|
siteId, |
|
|
postId, |
|
|
comments: [ comment ], |
|
|
skipSort: !! parentCommentId, |
|
|
meta: { |
|
|
comment: { |
|
|
context: 'add', |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
|
|
|
{ type: COMMENTS_COUNT_INCREMENT, siteId, postId }, |
|
|
]; |
|
|
|
|
|
if ( refreshCommentListQuery ) { |
|
|
actions.push( requestCommentsList( refreshCommentListQuery ) ); |
|
|
} |
|
|
|
|
|
return actions; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const handleWriteCommentFailure = |
|
|
( { siteId, postId, parentCommentId, placeholderId }, rawError ) => |
|
|
( dispatch, getState ) => { |
|
|
|
|
|
const post = getSitePost( getState(), siteId, postId ); |
|
|
const postTitle = post && post.title && post.title.trim().slice( 0, 20 ).trim().concat( '…' ); |
|
|
const error = postTitle |
|
|
? translate( 'Could not add a reply to “%(postTitle)s”', { args: { postTitle } } ) |
|
|
: translate( 'Could not add a reply to this post' ); |
|
|
|
|
|
|
|
|
dispatch( { |
|
|
type: COMMENTS_WRITE_ERROR, |
|
|
siteId, |
|
|
postId, |
|
|
commentId: placeholderId, |
|
|
parentCommentId, |
|
|
error, |
|
|
errorType: rawError && rawError.error, |
|
|
} ); |
|
|
|
|
|
dispatch( errorNotice( error, { duration: 5000 } ) ); |
|
|
}; |
|
|
|