Skip to content

Commit dfe822b

Browse files
committed
refactor: replace useQueries with useQuery in getConferences and getGithubRepos; update getArticles in getProductHuntProducts and add getSourceArticles
1 parent 6a23f7b commit dfe822b

4 files changed

Lines changed: 82 additions & 35 deletions

File tree

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { useQueries, UseQueryOptions } from '@tanstack/react-query'
2-
import { QueryConfig } from 'src/lib/react-query'
3-
import { Conference } from 'src/types'
1+
import { useQuery } from '@tanstack/react-query'
42
import { axios } from 'src/lib/axios'
3+
import { ExtractFnReturnType, QueryConfig } from 'src/lib/react-query'
4+
import { Conference } from 'src/types'
55

6-
const getConferences = async (tag: string): Promise<Conference[]> => {
7-
return axios.get(`/data/v2/conferences/${tag}.json`)
6+
const getConferences = async (tags: string[]): Promise<Conference[]> => {
7+
return axios.get(`/engine/conferences`, {
8+
params: {
9+
tags: tags?.join(','),
10+
},
11+
})
812
}
913

1014
type QueryFnType = typeof getConferences
@@ -15,13 +19,9 @@ type UseGetConferencesOptions = {
1519
}
1620

1721
export const useGetConferences = ({ config, tags }: UseGetConferencesOptions) => {
18-
return useQueries({
19-
queries: tags.map<UseQueryOptions<Conference[]>>((tag) => {
20-
return {
21-
...config,
22-
queryKey: ['conferences', tag],
23-
queryFn: () => getConferences(tag),
24-
}
25-
})
22+
return useQuery<ExtractFnReturnType<QueryFnType>>({
23+
...config,
24+
queryKey: ['conferences', ...tags],
25+
queryFn: () => getConferences(tags),
2626
})
2727
}
Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
1-
import { useQueries, UseQueryOptions } from '@tanstack/react-query'
2-
import { QueryConfig } from 'src/lib/react-query'
3-
import { Repository } from 'src/types'
1+
import { useQuery } from '@tanstack/react-query'
42
import { axios } from 'src/lib/axios'
3+
import { ExtractFnReturnType, QueryConfig } from 'src/lib/react-query'
4+
import { Repository } from 'src/types'
55

6-
const getRepos = async (tag: string, dateRange: string): Promise<Repository[]> => {
7-
return axios.get(`/data/v2/github/${tag}/${dateRange}.json`)
6+
const getRepos = async ({
7+
tags,
8+
dateRange,
9+
}: {
10+
tags: string[]
11+
dateRange: string
12+
}): Promise<Repository[]> => {
13+
return axios.get(`/engine/repos`, {
14+
params: {
15+
range: dateRange,
16+
tags: tags.join(','),
17+
},
18+
})
819
}
920

1021
type QueryFnType = typeof getRepos
1122

1223
type UseGetReposOptions = {
1324
config?: QueryConfig<QueryFnType>
1425
tags: string[]
15-
dateRange: "daily" | "monthly" | "weekly"
26+
dateRange: 'daily' | 'monthly' | 'weekly'
1627
}
1728

1829
export const useGetGithubRepos = ({ config, tags, dateRange }: UseGetReposOptions) => {
19-
return useQueries({
20-
queries: tags.map<UseQueryOptions<Repository[]>>((tag) => {
21-
return {
22-
...config,
23-
queryKey: ['github', tag, dateRange],
24-
queryFn: () => getRepos(tag, dateRange),
25-
}
26-
})
30+
return useQuery<ExtractFnReturnType<QueryFnType>>({
31+
...config,
32+
queryKey: ['github', ...tags, dateRange],
33+
queryFn: () => getRepos({ tags, dateRange }),
2734
})
28-
}
35+
}
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
import { useQuery } from '@tanstack/react-query'
2-
import { ExtractFnReturnType, QueryConfig } from 'src/lib/react-query'
3-
import { Article } from 'src/types'
42
import { axios } from 'src/lib/axios'
3+
import { ExtractFnReturnType, QueryConfig } from 'src/lib/react-query'
4+
import { Product } from 'src/types'
55

6-
const getArticles = async (): Promise<Article[]> => {
7-
return axios.get('/data/v2/producthunt.json')
6+
const getArticles = async ({ date }: { date: string }): Promise<Product[]> => {
7+
return axios.get(`/engine/products`, {
8+
params: {
9+
date,
10+
},
11+
})
812
}
913

1014
type QueryFnType = typeof getArticles
1115

1216
type UseGetArticlesOptions = {
1317
config?: QueryConfig<QueryFnType>
18+
date: string
1419
}
1520

16-
export const useGeProductHuntProducts = ({ config }: UseGetArticlesOptions = {}) => {
21+
export const useGeProductHuntProducts = ({ date, config }: UseGetArticlesOptions) => {
1722
return useQuery<ExtractFnReturnType<QueryFnType>>({
1823
...config,
19-
queryKey: ['producthunt'],
20-
queryFn: () => getArticles(),
24+
queryKey: ['producthunt', date],
25+
queryFn: () => getArticles({ date }),
2126
})
2227
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { useQuery } from '@tanstack/react-query'
2+
import { axios } from 'src/lib/axios'
3+
import { ExtractFnReturnType, QueryConfig } from 'src/lib/react-query'
4+
import { Article } from 'src/types'
5+
6+
const getArticles = async ({
7+
source,
8+
tags,
9+
}: {
10+
source: string
11+
tags?: string[]
12+
}): Promise<Article[]> => {
13+
return axios.get(`/engine/feeds`, {
14+
params: {
15+
source,
16+
...(tags?.length ? { tags: tags.join(',') } : {}),
17+
},
18+
})
19+
}
20+
21+
type QueryFnType = typeof getArticles
22+
23+
type UseGetArticlesOptions = {
24+
config?: QueryConfig<QueryFnType>
25+
source: string
26+
tags?: string[]
27+
}
28+
29+
export const useGetSourceArticles = ({ config, source, tags }: UseGetArticlesOptions) => {
30+
return useQuery<ExtractFnReturnType<QueryFnType>>({
31+
...config,
32+
queryKey: [source, ...(tags || [])],
33+
queryFn: () => getArticles({ source, tags }),
34+
})
35+
}

0 commit comments

Comments
 (0)