Skip to content

Commit aa4c740

Browse files
chore: Migrate useOwner to ts (#3601)
1 parent 9ed135c commit aa4c740

File tree

4 files changed

+93
-51
lines changed

4 files changed

+93
-51
lines changed

src/pages/RepoPage/CoverageOnboarding/GitHubActions/GitHubActions.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const mockGetUploadTokenRequired = {
5757

5858
const mockDetailOwner = {
5959
owner: {
60-
ownerid: '1234',
60+
ownerid: 1234,
6161
username: 'codecov',
6262
avatarUrl: 'https://avatars.githubusercontent.com/u/1234?v=4',
6363
isCurrentUserPartOfOrg: true,

src/services/user/useOwner.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/services/user/useOwner.test.jsx renamed to src/services/user/useOwner.test.tsx

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ import { useOwner } from './useOwner'
88

99
const queryClient = new QueryClient({
1010
defaultOptions: { queries: { retry: false } },
11-
logger: {
12-
error: () => {},
13-
},
1411
})
1512

1613
const wrapper =
17-
(initialEntries = '/gh') =>
14+
(initialEntries = '/gh'): React.FC<React.PropsWithChildren> =>
1815
({ children }) => (
1916
<QueryClientProvider client={queryClient}>
2017
<MemoryRouter initialEntries={[initialEntries]}>
@@ -37,30 +34,30 @@ afterAll(() => {
3734
server.close()
3835
})
3936

37+
const mockCodecovOrg = {
38+
username: 'codecov',
39+
avatarUrl: 'http://127.0.0.1/avatar-url',
40+
isCurrentUserPartOfOrg: true,
41+
isAdmin: false,
42+
}
43+
4044
describe('useOwner', () => {
41-
function setup(dataReturned = undefined) {
45+
function setup() {
4246
server.use(
4347
graphql.query('DetailOwner', () => {
44-
return HttpResponse.json({ data: { owner: dataReturned } })
48+
return HttpResponse.json({ data: { owner: mockCodecovOrg } })
4549
})
4650
)
4751
}
4852

4953
describe('when called and user is authenticated', () => {
50-
const codecovOrg = {
51-
username: 'codecov',
52-
avatarUrl: 'http://127.0.0.1/avatar-url',
53-
isCurrentUserPartOfOrg: true,
54-
isAdmin: false,
55-
}
56-
5754
it('returns the org', async () => {
58-
setup(codecovOrg)
55+
setup()
5956
const { result } = renderHook(() => useOwner({ username: 'codecov' }), {
6057
wrapper: wrapper(),
6158
})
6259

63-
await waitFor(() => expect(result.current.data).toEqual(codecovOrg))
60+
await waitFor(() => expect(result.current.data).toEqual(mockCodecovOrg))
6461
})
6562
})
6663
})

src/services/user/useOwner.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { useQuery } from '@tanstack/react-query'
2+
import { useParams } from 'react-router-dom'
3+
import { z } from 'zod'
4+
5+
import Api from 'shared/api'
6+
import { Provider, rejectNetworkError } from 'shared/api/helpers'
7+
8+
const OwnerSchema = z.object({
9+
ownerid: z.number().nullish(),
10+
username: z.string().nullish(),
11+
avatarUrl: z.string().nullish(),
12+
isCurrentUserPartOfOrg: z.boolean().nullish(),
13+
isAdmin: z.boolean().nullish(),
14+
})
15+
16+
export type Owner = z.infer<typeof OwnerSchema>
17+
18+
const RequestSchema = z.object({
19+
owner: OwnerSchema.nullish(),
20+
})
21+
22+
interface URLParams {
23+
provider: Provider
24+
}
25+
26+
interface UseOwnerArgs {
27+
username?: string
28+
opts?: {
29+
suspense?: boolean
30+
enabled?: boolean
31+
}
32+
}
33+
34+
const query = `
35+
query DetailOwner($username: String!) {
36+
owner(username: $username) {
37+
ownerid
38+
username
39+
avatarUrl
40+
isCurrentUserPartOfOrg
41+
isAdmin
42+
}
43+
}
44+
`
45+
46+
export function useOwner({
47+
username,
48+
opts = { enabled: username !== undefined },
49+
}: UseOwnerArgs) {
50+
const { provider } = useParams<URLParams>()
51+
52+
const variables = {
53+
username,
54+
}
55+
56+
return useQuery({
57+
queryKey: ['owner', variables, provider, query],
58+
queryFn: ({ signal }) =>
59+
Api.graphql({
60+
provider,
61+
query,
62+
variables,
63+
signal,
64+
}).then((res) => {
65+
const parsedRes = RequestSchema.safeParse(res?.data)
66+
67+
if (!parsedRes.success) {
68+
return rejectNetworkError({
69+
status: 404,
70+
data: {},
71+
dev: 'useOwner - 404 Failed to parse data',
72+
error: parsedRes.error,
73+
})
74+
}
75+
76+
return res?.data?.owner
77+
}),
78+
...opts,
79+
})
80+
}

0 commit comments

Comments
 (0)