Wait for session to be fetched before loading comments

This commit is contained in:
Paul Kaplan 2021-05-17 14:53:02 -04:00
parent 8984f2cedc
commit cc66341252
2 changed files with 7 additions and 3 deletions

View file

@ -128,6 +128,7 @@ module.exports.selectToken = state => get(state, ['session', 'session', 'user',
module.exports.selectIsAdmin = state => get(state, ['session', 'session', 'permissions', 'admin'], false); module.exports.selectIsAdmin = state => get(state, ['session', 'session', 'permissions', 'admin'], false);
module.exports.selectIsSocial = state => get(state, ['session', 'session', 'permissions', 'social'], false); module.exports.selectIsSocial = state => get(state, ['session', 'session', 'permissions', 'social'], false);
module.exports.selectIsEducator = state => get(state, ['session', 'session', 'permissions', 'educator'], false); module.exports.selectIsEducator = state => get(state, ['session', 'session', 'permissions', 'educator'], false);
module.exports.selectHasFetchedSession = state => state.session.status === module.exports.Status.FETCHED;
// NB logged out user id as NaN so that it can never be used in equality testing since NaN !== NaN // NB logged out user id as NaN so that it can never be used in equality testing since NaN !== NaN
module.exports.selectUserId = state => get(state, ['session', 'session', 'user', 'id'], NaN); module.exports.selectUserId = state => get(state, ['session', 'session', 'user', 'id'], NaN);

View file

@ -9,7 +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 {selectIsAdmin, selectHasFetchedSession} from '../../redux/session';
import { import {
selectShowCommentComposer, selectShowCommentComposer,
selectCanDeleteComment, selectCanDeleteComment,
@ -24,6 +24,7 @@ const StudioComments = ({
comments, comments,
commentsAllowed, commentsAllowed,
isAdmin, isAdmin,
hasFetchedSession,
handleLoadMoreComments, handleLoadMoreComments,
handleNewComment, handleNewComment,
moreCommentsToLoad, moreCommentsToLoad,
@ -42,8 +43,8 @@ const StudioComments = ({
handleLoadMoreReplies handleLoadMoreReplies
}) => { }) => {
useEffect(() => { useEffect(() => {
if (comments.length === 0) handleLoadMoreComments(); if (comments.length === 0 && hasFetchedSession) handleLoadMoreComments();
}, [comments.length === 0]); }, [comments.length === 0, hasFetchedSession]);
// The comments you see depend on your admin status // The comments you see depend on your admin status
// so reset them if isAdmin changes. // so reset them if isAdmin changes.
@ -108,6 +109,7 @@ StudioComments.propTypes = {
comments: PropTypes.arrayOf(PropTypes.shape({})), comments: PropTypes.arrayOf(PropTypes.shape({})),
commentsAllowed: PropTypes.bool, commentsAllowed: PropTypes.bool,
isAdmin: PropTypes.bool, isAdmin: PropTypes.bool,
hasFetchedSession: PropTypes.bool,
handleLoadMoreComments: PropTypes.func, handleLoadMoreComments: PropTypes.func,
handleNewComment: PropTypes.func, handleNewComment: PropTypes.func,
moreCommentsToLoad: PropTypes.bool, moreCommentsToLoad: PropTypes.bool,
@ -133,6 +135,7 @@ export {
export default connect( export default connect(
state => ({ state => ({
comments: state.comments.comments, comments: state.comments.comments,
hasFetchedSession: selectHasFetchedSession(state),
isAdmin: selectIsAdmin(state), isAdmin: selectIsAdmin(state),
moreCommentsToLoad: state.comments.moreCommentsToLoad, moreCommentsToLoad: state.comments.moreCommentsToLoad,
replies: state.comments.replies, replies: state.comments.replies,