Skip to content

Commit

Permalink
Refactor Profile Screen params
Browse files Browse the repository at this point in the history
  • Loading branch information
youngkidwarrior committed Oct 1, 2024
1 parent 84f8765 commit d0dceca
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
5 changes: 5 additions & 0 deletions packages/app/features/profile/screen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ProfileScreen } from './screen'
import { TamaguiProvider, config } from '@my/ui'
import { render, screen, act, waitFor } from '@testing-library/react-native'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { useProfileScreenParams } from 'app/routers/params'

Check warning on line 6 in packages/app/features/profile/screen.test.tsx

View workflow job for this annotation

GitHub Actions / Unit Tests

'useProfileScreenParams' is defined but never used

const queryClient = new QueryClient()
const TAG_NAME = 'pip_test44677'
Expand Down Expand Up @@ -99,6 +100,10 @@ jest.mock('solito/router', () => ({
}),
}))

jest.mock('app/routers/params', () => ({
useProfileScreenParams: jest.fn().mockReturnValue([{ sendid: 0 }, jest.fn()]),
}))

test('ProfileScreen', async () => {
jest.useFakeTimers()

Expand Down
17 changes: 11 additions & 6 deletions packages/app/features/profile/screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@ import { useInterUserActivityFeed } from './utils/useInterUserActivityFeed'
import type { Activity } from 'app/utils/zod/activity'
import { amountFromActivity } from 'app/utils/activity'
import { Fragment } from 'react'
const { useParam } = createParam<{ sendid: string }>()
import { useProfileScreenParams } from 'app/routers/params'

interface ProfileScreenProps {
sendid?: string
sendid?: number | null
}

export function ProfileScreen({ sendid: propSendid }: ProfileScreenProps) {
const [paramSendid] = useParam('sendid')
const otherUserId = propSendid || paramSendid
const { data: otherUserProfile, isLoading, error } = useProfileLookup('sendid', otherUserId || '')
const [{ sendid: paramSendid }] = useProfileScreenParams()
const otherUserId = propSendid || Number(paramSendid)
const {
data: otherUserProfile,
isLoading,
error,
} = useProfileLookup('sendid', otherUserId?.toString() || '')
const { user, profile: currentUserProfile } = useUser()
const router = useRouter()
const media = useMedia()
Expand All @@ -48,7 +53,7 @@ export function ProfileScreen({ sendid: propSendid }: ProfileScreenProps) {
} = useInterUserActivityFeed({
pageSize: 10,
otherUserId,
currentUserId: currentUserProfile?.send_id.toString(),
currentUserId: currentUserProfile?.send_id,
})
const { pages } = data ?? {}
const formatTags = (tags: string[]) => tags?.map((tag) => `/${tag}`).join(' ')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import type { ZodError } from 'zod'
export function useInterUserActivityFeed(params: {
pageSize?: number
refetchInterval?: number
currentUserId?: string
otherUserId?: string
currentUserId?: number
otherUserId?: number
}): UseInfiniteQueryResult<InfiniteData<Activity[]>, PostgrestError | ZodError> {
const { pageSize = 3, refetchInterval = 30_000, otherUserId, currentUserId } = params

Expand Down
18 changes: 17 additions & 1 deletion packages/app/routers/params.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,23 @@ export const useSendScreenParams = () => {
] as const
}

export type ProfileScreenParams = undefined
export type ProfileScreenParams = {
sendid?: string
}

const { useParam: useProfileParam, useParams: useProfileParams } =
createParam<ProfileScreenParams>()

export const useSendId = () => {
const [sendid, setSendid] = useProfileParam('sendid')
return [sendid, setSendid] as const
}

export const useProfileScreenParams = () => {
const { setParams } = useProfileParams()
const [sendid] = useSendId()
return [{ sendid }, setParams] as const
}

export type AuthScreenParams = {
redirectUri?: string
Expand Down

0 comments on commit d0dceca

Please sign in to comment.