Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
scratch-www
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
scratch-www
Commits
ef931846
Commit
ef931846
authored
Apr 22, 2021
by
Paul Kaplan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reset comments when admin logs in
parent
40b531fe
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
89 additions
and
4 deletions
+89
-4
src/views/studio/studio-comments.jsx
src/views/studio/studio-comments.jsx
+23
-4
test/unit/components/studio-comments.test.jsx
test/unit/components/studio-comments.test.jsx
+66
-0
No files found.
src/views/studio/studio-comments.jsx
View file @
ef931846
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';
...
@@ -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';
...
@@ -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 = ({
...
@@ -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 = ({
...
@@ -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 = {
...
@@ -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(
...
@@ -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
);
test/unit/components/studio-comments.test.jsx
0 → 100644
View file @
ef931846
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
();
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment