File size: 3,717 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
import page from '@automattic/calypso-router';
import { startsWith } from 'lodash';
import { addQueryArgs, getSiteFragment } from 'calypso/lib/route';
import CommentView from 'calypso/my-sites/comment/main';
import { removeNotice } from 'calypso/state/notices/actions';
import { getNotices } from 'calypso/state/notices/selectors';
import CommentsManagement from './main';
const mapPendingStatusToUnapproved = ( status ) => ( 'pending' === status ? 'unapproved' : status );
const sanitizeInt = ( number ) => {
const integer = parseInt( number, 10 );
return ! Number.isNaN( integer ) && integer > 0 ? integer : false;
};
const sanitizeQueryAction = ( action ) => {
if ( ! action ) {
return null;
}
const validActions = {
approve: 'approved',
edit: 'edit',
unapprove: 'unapproved',
trash: 'trash',
spam: 'spam',
delete: 'delete',
};
return validActions.hasOwnProperty( action.toLowerCase() )
? validActions[ action.toLowerCase() ]
: null;
};
const changePage = ( path ) => ( pageNumber ) => {
if ( window ) {
window.scrollTo( 0, 0 );
}
return page( addQueryArgs( { page: pageNumber }, path ) );
};
export const siteComments = ( context, next ) => {
const { params, path, query } = context;
const siteFragment = getSiteFragment( path );
if ( ! siteFragment ) {
return page.redirect( '/comments/all' );
}
const status = mapPendingStatusToUnapproved( params.status );
const analyticsPath = `/comments/${ status }/:site`;
const pageNumber = sanitizeInt( query.page ) || 1;
context.primary = (
<CommentsManagement
analyticsPath={ analyticsPath }
changePage={ changePage( path ) }
page={ pageNumber }
siteFragment={ siteFragment }
status={ status }
/>
);
next();
};
export const postComments = ( context, next ) => {
const { params, path, query } = context;
const siteFragment = getSiteFragment( path );
if ( ! siteFragment ) {
return page.redirect( '/comments/all' );
}
const status = mapPendingStatusToUnapproved( params.status );
const postId = sanitizeInt( params.post );
const analyticsPath = `/comments/${ status }/:site/:post`;
if ( ! postId ) {
return page.redirect( `/comments/${ params.status }/${ siteFragment }` );
}
const pageNumber = sanitizeInt( query.page ) || 1;
context.primary = (
<CommentsManagement
analyticsPath={ analyticsPath }
changePage={ changePage( path ) }
page={ pageNumber }
postId={ postId }
siteFragment={ siteFragment }
status={ status }
/>
);
next();
};
export const comment = ( context, next ) => {
const { params, path, query } = context;
const siteFragment = getSiteFragment( path );
const commentId = sanitizeInt( params.comment );
if ( ! commentId ) {
return siteFragment
? page.redirect( `/comments/all/${ siteFragment }` )
: page.redirect( '/comments/all' );
}
const action = sanitizeQueryAction( query.action );
const redirectToPostView = ( postId ) => () =>
page.redirect( `/comments/all/${ siteFragment }/${ postId }` );
context.primary = <CommentView { ...{ action, commentId, siteFragment, redirectToPostView } } />;
next();
};
export const redirect = ( { path } ) => {
const siteFragment = getSiteFragment( path );
if ( siteFragment ) {
return page.redirect( `/comments/all/${ siteFragment }` );
}
return page.redirect( '/comments/all' );
};
export const clearCommentNotices = ( { store }, next ) => {
const nextPath = page.current;
if ( ! startsWith( nextPath, '/comments' ) ) {
const { getState, dispatch } = store;
const notices = getNotices( getState() );
notices.forEach( ( { noticeId } ) => {
if ( startsWith( noticeId, 'comment-notice' ) ) {
dispatch( removeNotice( noticeId ) );
}
} );
}
next();
};
|