Fix count of viewed comments of a post (#2297)
* fix: parse existingRootComments as Number to correctly add new comments to the count * return commentsViewedAt and commentsViewedNum parsed as Number * hotfix: commentsViewedNum accepts itemId directly * consistently receive itemId on new-comments functions that only uses IDs
This commit is contained in:
		
							parent
							
								
									96f149fa79
								
							
						
					
					
						commit
						d4efacadc0
					
				@ -110,7 +110,7 @@ export default function ItemInfo ({
 | 
			
		||||
        </>}
 | 
			
		||||
      <Link
 | 
			
		||||
        href={`/items/${item.id}`} onClick={(e) => {
 | 
			
		||||
          const viewedAt = commentsViewedAt(item)
 | 
			
		||||
          const viewedAt = commentsViewedAt(item.id)
 | 
			
		||||
          if (viewedAt) {
 | 
			
		||||
            e.preventDefault()
 | 
			
		||||
            router.push(
 | 
			
		||||
 | 
			
		||||
@ -29,7 +29,7 @@ import { useShowModal } from './modal'
 | 
			
		||||
import { BoostHelp } from './adv-post-form'
 | 
			
		||||
 | 
			
		||||
function onItemClick (e, router, item) {
 | 
			
		||||
  const viewedAt = commentsViewedAt(item)
 | 
			
		||||
  const viewedAt = commentsViewedAt(item.id)
 | 
			
		||||
  if (viewedAt) {
 | 
			
		||||
    e.preventDefault()
 | 
			
		||||
    if (e.ctrlKey || e.metaKey) {
 | 
			
		||||
 | 
			
		||||
@ -8,20 +8,23 @@ export function commentsViewed (item) {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function commentsViewedAfterComment (rootId, createdAt) {
 | 
			
		||||
  window.localStorage.setItem(`${COMMENTS_VIEW_PREFIX}:${rootId}`, new Date(createdAt).getTime())
 | 
			
		||||
  const existingRootComments = window.localStorage.getItem(`${COMMENTS_NUM_PREFIX}:${rootId}`) || 0
 | 
			
		||||
  window.localStorage.setItem(`${COMMENTS_NUM_PREFIX}:${rootId}`, existingRootComments + 1)
 | 
			
		||||
export function commentsViewedAt (itemId) {
 | 
			
		||||
  return Number(window.localStorage.getItem(`${COMMENTS_VIEW_PREFIX}:${itemId}`))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function commentsViewedAt (item) {
 | 
			
		||||
  return window.localStorage.getItem(`${COMMENTS_VIEW_PREFIX}:${item.id}`)
 | 
			
		||||
export function commentsViewedNum (itemId) {
 | 
			
		||||
  return Number(window.localStorage.getItem(`${COMMENTS_NUM_PREFIX}:${itemId}`))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function commentsViewedAfterComment (rootId, createdAt) {
 | 
			
		||||
  window.localStorage.setItem(`${COMMENTS_VIEW_PREFIX}:${rootId}`, new Date(createdAt).getTime())
 | 
			
		||||
  window.localStorage.setItem(`${COMMENTS_NUM_PREFIX}:${rootId}`, commentsViewedNum(rootId) + 1)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function newComments (item) {
 | 
			
		||||
  if (!item.parentId) {
 | 
			
		||||
    const viewedAt = commentsViewedAt(item)
 | 
			
		||||
    const viewNum = window.localStorage.getItem(`${COMMENTS_NUM_PREFIX}:${item.id}`)
 | 
			
		||||
    const viewedAt = commentsViewedAt(item.id)
 | 
			
		||||
    const viewNum = commentsViewedNum(item.id)
 | 
			
		||||
 | 
			
		||||
    if (viewedAt && viewNum) {
 | 
			
		||||
      return viewedAt < new Date(item.lastCommentAt).getTime() || viewNum < item.ncomments
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user