Skip to content

Commit

Permalink
Merge pull request #622 from 1ifeworld/salief/adjust-profile-item-count
Browse files Browse the repository at this point in the history
salief/adjust profile item count
  • Loading branch information
salieflewis authored May 30, 2024
2 parents 2c810fb + d48c520 commit 6ec1a2f
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 5 deletions.
9 changes: 6 additions & 3 deletions apps/site/app/[username]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Button, Flex, Grid, Stack, Typography } from '@/design-system'
import { Flex, Grid, Stack, Typography } from '@/design-system'
import { getChannelsItemsWithUser } from '@/gql'
import { getDataForUsername } from '@/lib'
import { getDataForUsername, getItemsWithUserId } from '@/lib'
import { ChannelCard } from '@/server'
import { pluralize, sortChannels } from '@/utils'
import { ChannelDialog, NewChannelTrigger, UserSettings } from '@/client'
Expand All @@ -11,6 +11,7 @@ export default async function Profile({
params: { username: string }
}) {
const userData = await getDataForUsername({ username: params.username })

let channels
let items

Expand All @@ -23,6 +24,8 @@ export default async function Profile({
items = itemsResp
}

const { itemsWithUserId } = await getItemsWithUserId(userData.id)

// @ts-ignore
const sortedChannels = channels?.items ? sortChannels(channels.items) : []

Expand All @@ -35,7 +38,7 @@ export default async function Profile({
</Flex>
<Typography className="text-secondary-foreground">
{pluralize(sortedChannels.length as number, 'channel', 'channels')},{' '}
{pluralize(items?.items?.length as number, 'item', 'items')}
{pluralize(itemsWithUserId as number, 'item', 'items')}
</Typography>
</Stack>
<Stack className="gap-y-[15px]">
Expand Down
9 changes: 9 additions & 0 deletions apps/site/config/pgPool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'server-only'

import pg from 'pg'

const { Pool } = pg

export const pgPool = new Pool({
connectionString: process.env.DATABASE_URL,
})
16 changes: 16 additions & 0 deletions apps/site/lib/actions/getItemsWithUserId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use server'

import { pgPool } from '@/config/pgPool'

export async function getItemsWithUserId(createdById: number) {
try {
await pgPool.connect()
const query =
'SELECT COUNT(*) FROM "public"."Item" WHERE "createdById" = $1;'
const res = await pgPool.query(query, [createdById])
return { itemsWithUserId: res.rows[0].count }
} catch (err) {
console.error(err)
return { itemsWithUserId: 0 } // Ensure a return on error
}
}
1 change: 1 addition & 0 deletions apps/site/lib/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './getItemsWithUserId'
export * from './revalidationHelper'
2 changes: 2 additions & 0 deletions apps/site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"nextjs-toploader": "^1.6.11",
"p-queue": "^8.0.1",
"p-retry": "^6.2.0",
"pg": "^8.11.5",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-dropzone": "^14.2.3",
Expand All @@ -71,6 +72,7 @@
"@graphql-codegen/typescript-graphql-request": "^5.0.0",
"@graphql-codegen/typescript-operations": "^4.0.1",
"@types/node": "^20.5.6",
"@types/pg": "^8.11.6",
"@types/react": "18.2.21",
"@types/react-dom": "18.2.7",
"autoprefixer": "10.4.15",
Expand Down
97 changes: 95 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6ec1a2f

Please sign in to comment.