Skip to content

Commit

Permalink
fix reaction counts
Browse files Browse the repository at this point in the history
  • Loading branch information
lyoshenka committed Mar 6, 2024
1 parent ea273c2 commit 49ab05c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
6 changes: 0 additions & 6 deletions components/landing/HeroSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ const ExtraPadding = styled.div`
}
`

const Description = styled.div`
display: flex;
flex-direction: column;
gap: 0.4rem;
`

const Buttons = styled.div`

Check warning on line 60 in components/landing/HeroSection.tsx

View workflow job for this annotation

GitHub Actions / Lint

'Buttons' is assigned a value but never used
display: flex;
flex-direction: column;
Expand Down
37 changes: 31 additions & 6 deletions pages/api/v2/activity/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import {
import { CitizenshipStatus, RoleLevel, RoleType } from '@/utils/types/profile'
import { OfferType } from '@/utils/types/offer'
import { LocationType } from '@/utils/types/location'
import { withAuth } from '@/utils/api/withAuth'
import { AuthData, requireProfile, withAuth } from '@/utils/api/withAuth'

async function handler(
req: NextApiRequest,
res: NextApiResponse<ActivityListResponse>
res: NextApiResponse<ActivityListResponse>,
opts: { auth: AuthData }
) {
if (req.method != 'GET') {
res.status(405).send({ error: 'Method not allowed' })
Expand Down Expand Up @@ -56,15 +57,39 @@ async function handler(
prisma.activity.count({ where: activityQuery.where }),
])

let activtiesWithMyReactions: { [key: number]: boolean } = {}
if (opts.auth.authToken) {
const profile = await requireProfile(req, res, opts)
const myReactions = await prisma.activityReaction.findMany({
select: { activityId: true },
where: {
profile: { id: profile.id },
activity: {
id: {
in: activities.map((a) => a.id),
},
},
},
})
activtiesWithMyReactions = myReactions.reduce((obj, r, i) => {
obj[i] = true
return obj
}, activtiesWithMyReactions)
}

res.status(200).send({
activities: toFragments(activities as ActivityWithRelations[]),
activities: toFragments(
activities as ActivityWithRelations[],
activtiesWithMyReactions
),
count: activities.length,
totalCount,
})
}

const toFragments = (
activities: ActivityWithRelations[]
activities: ActivityWithRelations[],
activtiesWithMyReactions: { [key: number]: boolean }
): ActivityListFragment[] => {
return activities.map((activity) => {
return {
Expand Down Expand Up @@ -156,8 +181,8 @@ const toFragments = (
})),
avatarUrl: activity.profile.avatar ? activity.profile.avatar.url : '',
},
reactionCount: 0, // TODO: implement
hasReactionByMe: false, // TODO: implement
reactionCount: activity._count.reactions,
hasReactionByMe: activtiesWithMyReactions[activity.id] || false,
}
})
}
Expand Down
6 changes: 6 additions & 0 deletions utils/types/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ export type ActivitySummaryResponse =
// must match ActivityQueryInclude below
export type ActivityWithRelations = Prisma.ActivityGetPayload<{
include: {
_count: {
select: { reactions: true }
}
profile: {
select: {
externId: true
Expand Down Expand Up @@ -211,6 +214,9 @@ export type ActivityWithRelations = Prisma.ActivityGetPayload<{

// must match ActivityWithRelations above
export const ActivityQueryInclude = {
_count: {
select: { reactions: true },
},
profile: {
select: {
externId: true,
Expand Down

0 comments on commit 49ab05c

Please sign in to comment.