mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2024-11-27 09:35:56 -05:00
Reset comments when admin logs in
This commit is contained in:
parent
40b531fe7b
commit
ef9318463e
2 changed files with 89 additions and 4 deletions
|
@ -1,4 +1,4 @@
|
||||||
import React, {useEffect} from 'react';
|
import React, {useEffect, useRef} from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import {connect} from 'react-redux';
|
import {connect} from 'react-redux';
|
||||||
import {FormattedMessage} from 'react-intl';
|
import {FormattedMessage} from 'react-intl';
|
||||||
|
@ -9,6 +9,7 @@ import TopLevelComment from '../preview/comment/top-level-comment.jsx';
|
||||||
import studioCommentActions from '../../redux/studio-comment-actions.js';
|
import studioCommentActions from '../../redux/studio-comment-actions.js';
|
||||||
import StudioCommentsAllowed from './studio-comments-allowed.jsx';
|
import StudioCommentsAllowed from './studio-comments-allowed.jsx';
|
||||||
|
|
||||||
|
import {selectIsAdmin} from '../../redux/session';
|
||||||
import {
|
import {
|
||||||
selectShowCommentComposer,
|
selectShowCommentComposer,
|
||||||
selectCanDeleteComment,
|
selectCanDeleteComment,
|
||||||
|
@ -22,6 +23,7 @@ import {selectStudioCommentsAllowed} from '../../redux/studio.js';
|
||||||
const StudioComments = ({
|
const StudioComments = ({
|
||||||
comments,
|
comments,
|
||||||
commentsAllowed,
|
commentsAllowed,
|
||||||
|
isAdmin,
|
||||||
handleLoadMoreComments,
|
handleLoadMoreComments,
|
||||||
handleNewComment,
|
handleNewComment,
|
||||||
moreCommentsToLoad,
|
moreCommentsToLoad,
|
||||||
|
@ -35,12 +37,22 @@ const StudioComments = ({
|
||||||
canRestoreComment,
|
canRestoreComment,
|
||||||
handleDeleteComment,
|
handleDeleteComment,
|
||||||
handleRestoreComment,
|
handleRestoreComment,
|
||||||
|
handleResetComments,
|
||||||
handleReportComment,
|
handleReportComment,
|
||||||
handleLoadMoreReplies
|
handleLoadMoreReplies
|
||||||
}) => {
|
}) => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (comments.length === 0) handleLoadMoreComments();
|
if (comments.length === 0) handleLoadMoreComments();
|
||||||
}, []); // Only runs once after the first render
|
}, [comments.length === 0]);
|
||||||
|
|
||||||
|
// The comments you see depend on your admin status
|
||||||
|
// so reset them if isAdmin changes.
|
||||||
|
const adminRef = useRef(isAdmin);
|
||||||
|
useEffect(() => {
|
||||||
|
const wasAdmin = adminRef.current;
|
||||||
|
adminRef.current = isAdmin;
|
||||||
|
if (isAdmin !== wasAdmin) handleResetComments();
|
||||||
|
}, [isAdmin]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
|
@ -93,6 +105,7 @@ const StudioComments = ({
|
||||||
StudioComments.propTypes = {
|
StudioComments.propTypes = {
|
||||||
comments: PropTypes.arrayOf(PropTypes.shape({})),
|
comments: PropTypes.arrayOf(PropTypes.shape({})),
|
||||||
commentsAllowed: PropTypes.bool,
|
commentsAllowed: PropTypes.bool,
|
||||||
|
isAdmin: PropTypes.bool,
|
||||||
handleLoadMoreComments: PropTypes.func,
|
handleLoadMoreComments: PropTypes.func,
|
||||||
handleNewComment: PropTypes.func,
|
handleNewComment: PropTypes.func,
|
||||||
moreCommentsToLoad: PropTypes.bool,
|
moreCommentsToLoad: PropTypes.bool,
|
||||||
|
@ -106,13 +119,19 @@ StudioComments.propTypes = {
|
||||||
handleDeleteComment: PropTypes.func,
|
handleDeleteComment: PropTypes.func,
|
||||||
handleRestoreComment: PropTypes.func,
|
handleRestoreComment: PropTypes.func,
|
||||||
handleReportComment: PropTypes.func,
|
handleReportComment: PropTypes.func,
|
||||||
|
handleResetComments: PropTypes.func,
|
||||||
handleLoadMoreReplies: PropTypes.func,
|
handleLoadMoreReplies: PropTypes.func,
|
||||||
postURI: PropTypes.string
|
postURI: PropTypes.string
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export {
|
||||||
|
StudioComments
|
||||||
|
};
|
||||||
|
|
||||||
export default connect(
|
export default connect(
|
||||||
state => ({
|
state => ({
|
||||||
comments: state.comments.comments,
|
comments: state.comments.comments,
|
||||||
|
isAdmin: selectIsAdmin(state),
|
||||||
moreCommentsToLoad: state.comments.moreCommentsToLoad,
|
moreCommentsToLoad: state.comments.moreCommentsToLoad,
|
||||||
replies: state.comments.replies,
|
replies: state.comments.replies,
|
||||||
commentsAllowed: selectStudioCommentsAllowed(state),
|
commentsAllowed: selectStudioCommentsAllowed(state),
|
||||||
|
@ -130,7 +149,7 @@ export default connect(
|
||||||
handleDeleteComment: studioCommentActions.deleteComment,
|
handleDeleteComment: studioCommentActions.deleteComment,
|
||||||
handleRestoreComment: studioCommentActions.restoreComment,
|
handleRestoreComment: studioCommentActions.restoreComment,
|
||||||
handleReportComment: studioCommentActions.reportComment,
|
handleReportComment: studioCommentActions.reportComment,
|
||||||
handleLoadMoreReplies: studioCommentActions.getReplies
|
handleLoadMoreReplies: studioCommentActions.getReplies,
|
||||||
|
handleResetComments: studioCommentActions.resetComments
|
||||||
}
|
}
|
||||||
)(StudioComments);
|
)(StudioComments);
|
||||||
|
|
66
test/unit/components/studio-comments.test.jsx
Normal file
66
test/unit/components/studio-comments.test.jsx
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
import React from 'react';
|
||||||
|
import {mountWithIntl} from '../../helpers/intl-helpers.jsx';
|
||||||
|
import {StudioComments} from '../../../src/views/studio/studio-comments.jsx';
|
||||||
|
|
||||||
|
describe('Studio comments', () => {
|
||||||
|
test('if there are no comments, they get loaded', () => {
|
||||||
|
const loadComments = jest.fn();
|
||||||
|
const component = mountWithIntl(
|
||||||
|
<StudioComments
|
||||||
|
comments={[]}
|
||||||
|
handleLoadMoreComments={loadComments}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
expect(loadComments).toHaveBeenCalled();
|
||||||
|
|
||||||
|
// When updated to have comments, load is not called again
|
||||||
|
loadComments.mockClear();
|
||||||
|
component.setProps({comments: [{id: 123, author: {}}]});
|
||||||
|
component.update();
|
||||||
|
expect(loadComments).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
// When reset to have no comments again, load is called again
|
||||||
|
loadComments.mockClear();
|
||||||
|
component.setProps({comments: []});
|
||||||
|
component.update();
|
||||||
|
expect(loadComments).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('becoming an admin resets the comments', () => {
|
||||||
|
const resetComments = jest.fn();
|
||||||
|
const component = mountWithIntl(
|
||||||
|
<StudioComments
|
||||||
|
isAdmin={false}
|
||||||
|
comments={[{id: 123, author: {}}]}
|
||||||
|
handleResetComments={resetComments}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
expect(resetComments).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
// When updated to isAdmin=true, reset is called
|
||||||
|
resetComments.mockClear();
|
||||||
|
component.setProps({isAdmin: true});
|
||||||
|
component.update();
|
||||||
|
expect(resetComments).toHaveBeenCalled();
|
||||||
|
|
||||||
|
// If updated back to isAdmin=false, reset is also called
|
||||||
|
// not currently possible in the UI, but if it was, we'd want to clear comments
|
||||||
|
resetComments.mockClear();
|
||||||
|
component.setProps({isAdmin: false});
|
||||||
|
component.update();
|
||||||
|
expect(resetComments).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('being an admin on initial render doesnt reset comments', () => {
|
||||||
|
// This ensures that comments don't get reloaded when changing tabs
|
||||||
|
const resetComments = jest.fn();
|
||||||
|
mountWithIntl(
|
||||||
|
<StudioComments
|
||||||
|
isAdmin
|
||||||
|
comments={[{id: 123, author: {}}]}
|
||||||
|
handleResetComments={resetComments}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
expect(resetComments).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue