Skip to content

Commit

Permalink
fix: merge
Browse files Browse the repository at this point in the history
  • Loading branch information
mariojsnunes committed Jun 14, 2024
1 parent 76b7ef8 commit 57533ce
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 143 deletions.
13 changes: 3 additions & 10 deletions src/pages/Research/Content/ResearchArticle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@ import { useCommonStores } from 'src/common/hooks/useCommonStores'
import { Breadcrumbs } from 'src/pages/common/Breadcrumbs/Breadcrumbs'
import { NotFoundPage } from 'src/pages/NotFound/NotFound'
import { useResearchStore } from 'src/stores/Research/research.store'
import {
isAllowedToDeleteContent,
isAllowedToEditContent,
} from 'src/utils/helpers'
import { isAllowedToDeleteContent, isAllowedToEditContent } from 'src/utils/helpers'
import { seoTagsUpdate } from 'src/utils/seo'
import { Box, Flex } from 'theme-ui'

import {
getPublicUpdates,
getResearchTotalCommentCount,
researchUpdateStatusFilter,
} from '../researchHelpers'
import { getPublicUpdates, researchUpdateStatusFilter } from '../researchHelpers'
import { researchCommentUrlPattern } from './helper'
import ResearchDescription from './ResearchDescription'
import ResearchUpdate from './ResearchUpdate'
Expand Down Expand Up @@ -218,7 +211,7 @@ const ResearchArticle = observer(() => {
onFollowClick={() => onFollowClick(item.slug)}
contributors={contributors}
subscribersCount={researchStore.subscribersCount}
commentsCount={getResearchTotalCommentCount(item)}
commentsCount={item.totalCommentCount}
updatesCount={
item.updates?.filter((u) =>
researchUpdateStatusFilter(item, u, researchStore.activeUser?._id),
Expand Down
91 changes: 2 additions & 89 deletions src/pages/Research/researchHelpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,98 +1,11 @@
import { ResearchUpdateStatus } from 'oa-shared'
import { FactoryResearchItemUpdate } from 'src/test/factories/ResearchItem'
import { describe, expect, it } from 'vitest'

import {
getResearchTotalCommentCount,
researchUpdateStatusFilter,
} from './researchHelpers'
import { researchUpdateStatusFilter } from './researchHelpers'

import type { IResearch } from 'src/models'

describe('Research Helpers', () => {
describe('getResearchTotalCommentCount Function', () => {
it('should return 0 when item has no updates', () => {
const item = { item: {} } as any
expect(getResearchTotalCommentCount(item)).toBe(0)
})

it('should return 0 when updates have no comments', () => {
const item = {
updates: Array.from({ length: 3 }).fill(
FactoryResearchItemUpdate({
status: ResearchUpdateStatus.PUBLISHED,
_deleted: false,
}),
),
} as IResearch.ItemDB
expect(getResearchTotalCommentCount(item)).toBe(0)
})

it('should use totalCommentCount if present', () => {
const item = {
totalCommentCount: 5,
updates: Array.from({ length: 3 }).fill(
FactoryResearchItemUpdate({
status: ResearchUpdateStatus.PUBLISHED,
_deleted: false,
}),
),
} as IResearch.ItemDB
expect(getResearchTotalCommentCount(item)).toBe(5)
})

it('should use totalCommentCount when 0', () => {
const item = {
totalCommentCount: 0,
updates: Array.from({ length: 3 }).fill(
FactoryResearchItemUpdate({
status: ResearchUpdateStatus.PUBLISHED,
_deleted: false,
}),
),
} as IResearch.ItemDB
expect(getResearchTotalCommentCount(item)).toBe(0)
})

it('should return the correct amount of comments', () => {
const item = {
updates: Array.from({ length: 3 }).fill(
FactoryResearchItemUpdate({
status: ResearchUpdateStatus.PUBLISHED,
_deleted: false,
comments: Array.from({ length: 3 }),
}),
),
} as IResearch.ItemDB
expect(getResearchTotalCommentCount(item)).toBe(9)
})

it('should ignore deleted and draft updates', () => {
const item = {
updates: Array.from({ length: 2 })
.fill(
FactoryResearchItemUpdate({
status: ResearchUpdateStatus.PUBLISHED,
_deleted: false,
comments: Array.from({ length: 2 }),
}),
)
.concat([
FactoryResearchItemUpdate({
status: ResearchUpdateStatus.PUBLISHED,
_deleted: true,
comments: Array.from({ length: 3 }),
}),
FactoryResearchItemUpdate({
status: ResearchUpdateStatus.DRAFT,
_deleted: false,
comments: Array.from({ length: 6 }),
}),
]),
} as IResearch.ItemDB
expect(getResearchTotalCommentCount(item)).toBe(4)
})
})

describe('Research Update Status Filter', () => {
it('should not show item when deleted', async () => {
// prepare
Expand Down
24 changes: 0 additions & 24 deletions src/pages/Research/researchHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,6 @@ import { ResearchStatus, ResearchUpdateStatus } from 'oa-shared'

import type { IResearch } from 'src/models'

export const getResearchTotalCommentCount = (
item: IResearch.ItemDB,
): number => {
if (Object.hasOwnProperty.call(item, 'totalCommentCount')) {
return item.totalCommentCount || 0
}

if (item.updates) {
const commentOnUpdates = item.updates.reduce((totalComments, update) => {
const updateCommentsLength =
!update._deleted &&
update.status !== ResearchUpdateStatus.DRAFT &&
update.comments
? update.comments.length
: 0
return totalComments + updateCommentsLength
}, 0)

return commentOnUpdates ? commentOnUpdates : 0
} else {
return 0
}
}

export const researchUpdateStatusFilter = (
item: IResearch.Item,
update: IResearch.Update,
Expand Down
20 changes: 0 additions & 20 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,6 @@ export const isContactable = (preference: boolean | undefined) => {
: DEFAULT_PUBLIC_CONTACT_PREFERENCE
}

export const getPublicUpdates = (item: IResearch.Item) => {
if (item.updates) {
return item.updates.filter(
(update) =>
update.status !== ResearchUpdateStatus.DRAFT && !update._deleted,
)
} else {
return []
}
}

export const getProjectEmail = (subject: string) => {
const site = getConfigurationOption(
'REACT_APP_PLATFORM_THEME',
Expand Down Expand Up @@ -245,12 +234,3 @@ export const buildStatisticsLabel = ({
return `${typeof stat === 'number' ? stat : 0} ${statUnit}s`
}

export const researchStatusColour = (
researchStatus?: ResearchStatus,
): string => {
return researchStatus === ResearchStatus.ARCHIVED
? 'lightgrey'
: researchStatus === ResearchStatus.COMPLETED
? 'betaGreen'
: 'accent.base'
}

0 comments on commit 57533ce

Please sign in to comment.