Skip to content

Commit e91d44e

Browse files
committed
refactor(hooks): update query keys and improve ID filtering logic
1 parent 1e0db43 commit e91d44e

File tree

5 files changed

+19
-17
lines changed

5 files changed

+19
-17
lines changed

src/hooks/useFetchSpeakers.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe("fetch speaker hook and speaker adapter", () => {
6666
expect(result.current.data).toEqual(speakerAdapter(payload.data));
6767
});
6868

69-
it("should adapt from server response a query with id", async () => {
69+
it("should filter by ID when an ID is passed", async () => {
7070
//Given
7171
const { wrapper } = getQueryClientWrapper();
7272
mockedAxios.get.mockResolvedValueOnce(payload);

src/hooks/useFetchSpeakers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const useFetchSpeakers = (
2828
}
2929

3030
return useQuery(
31-
"api-speakers",
31+
["api-speakers", speakerId],
3232
async () => {
3333
try {
3434
const serverResponse = await axios.get(url, {
@@ -50,8 +50,8 @@ export const useFetchSpeakers = (
5050
}
5151
},
5252
{
53-
cacheTime: 1800000, // 30 minutes
54-
staleTime: 1800000, // 30 minutes
53+
cacheTime: 1800000,
54+
staleTime: 1800000,
5555
},
5656
);
5757
};

src/hooks/useFetchTalks.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe("useFetchTalksById", () => {
103103
vi.clearAllMocks();
104104
});
105105

106-
it("should use default URL when no parameter is provided", async () => {
106+
it("should fetch a single talk by id", async () => {
107107
const mockSession = createMockSession();
108108
const mockData: IGroup[] = [
109109
createMockGroup({

src/hooks/useFetchTalks.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const getUrl = (urlOrYear?: string): string => {
4040
* @returns The query result
4141
*/
4242
const useFetchTalksBase = <T>(
43-
queryKey: string,
43+
queryKey: string | (string | undefined)[],
4444
urlOrYear?: string,
4545
dataTransformer: (data: IGroup[]) => T = (data: IGroup[]) =>
4646
data as unknown as T,
@@ -68,13 +68,19 @@ export const useFetchTalksById = (
6868
id: string,
6969
urlOrYear?: string,
7070
): UseQueryResult<Session> => {
71-
return useFetchTalksBase<Session>("talks", urlOrYear, (data: IGroup[]) => {
72-
const sessions = data
73-
.map((track: IGroup) => track.sessions)
74-
.flat(1)
75-
.filter((session: { id: number | string }) => String(session.id) === id);
76-
return sessions[0];
77-
});
71+
return useFetchTalksBase<Session>(
72+
["talks", id],
73+
urlOrYear,
74+
(data: IGroup[]) => {
75+
const sessions = data
76+
.map((track: IGroup) => track.sessions)
77+
.flat(1)
78+
.filter(
79+
(session: { id: number | string }) => String(session.id) === id,
80+
);
81+
return sessions[0];
82+
},
83+
);
7884
};
7985

8086
export const useFetchLiveView = (

src/index.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,4 @@ root.render(
4545
</BrowserRouter>
4646
</React.Suspense>,
4747
);
48-
49-
// If you want to start measuring performance in your app, pass a function
50-
// to log results (for example: reportWebVitals())
51-
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
5248
reportWebVitals();

0 commit comments

Comments
 (0)