-
Notifications
You must be signed in to change notification settings - Fork 2
refactor: centralize feature API calls #297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
stasel
merged 6 commits into
HackYourFuture:main
from
Hadley99:task/issue-287-centralize-api-calls
Feb 9, 2026
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0021e81
refactor: centralize feature API calls
Hadley99 00ece08
Improved error handling
Hadley99 566abdf
Removed try/catch to preserve 401 redirect functionality
Hadley99 12d3093
improved parameter handling
Hadley99 c230afd
refactor: streamline API response handling in trainee info functions
Hadley99 cd65534
fixed params for axios
Hadley99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import axios from 'axios'; | ||
| import { Cohort } from '../Cohorts'; | ||
|
|
||
| export const getCohorts = async () => { | ||
| const { data } = await axios.get<Cohort[]>('/api/cohorts'); | ||
| return data; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,13 @@ | ||
| import { Cohort } from '../Cohorts'; | ||
| import axios from 'axios'; | ||
| import { useQuery } from '@tanstack/react-query'; | ||
| import { getCohorts } from '../api/api'; | ||
|
|
||
| /** | ||
| * A React Query hook that fetches cohort data form api for specific dates. | ||
| */ | ||
| export const useCohortsData = () => { | ||
| return useQuery({ | ||
| queryKey: ['CohortsInfo'], | ||
| queryFn: async () => { | ||
| const { data } = await axios.get<Cohort[]>(`/api/cohorts`); | ||
| return data; | ||
| }, | ||
| queryFn: getCohorts, | ||
| refetchOnWindowFocus: false, // Prevent refetching on window focus | ||
| }); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import axios from 'axios'; | ||
| import { DashboardData } from '../Dashboard'; | ||
|
|
||
| export const getDashboardData = async (startDate?: string, endDate?: string): Promise<DashboardData> => { | ||
| const response = await axios.get<DashboardData>(`/api/dashboard?startDate=${startDate}&endDate=${endDate}`); | ||
| return response.data; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,16 @@ | ||
| import { DashboardData } from '../Dashboard'; | ||
| import axios from 'axios'; | ||
| import { useQuery } from '@tanstack/react-query'; | ||
| import { getDashboardData } from '../api/api'; | ||
|
|
||
| /** | ||
| * A React Query hook that fetches dashboard data form api for specific dates. | ||
| * A React Query hook that fetches dashboard data from api for specific dates. | ||
| * | ||
| * @param {string | undefined} startDate | ||
| * @param {string | undefined} endDate | ||
| */ | ||
| export const useDashboardData = (startDate: string | undefined, endDate: string | undefined) => { | ||
| return useQuery({ | ||
| queryKey: ['DashboardInfo'], | ||
| queryFn: async () => { | ||
| const response = await axios.get<DashboardData>(`/api/dashboard?startDate=${startDate}&endDate=${endDate}`); | ||
| return response.data; | ||
| }, | ||
| queryFn: () => getDashboardData(startDate, endDate), | ||
| refetchOnWindowFocus: false, // Prevent refetching on window focus | ||
| }); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import axios from 'axios'; | ||
| import { SearchResult, SearchResultResponse } from '../Search'; | ||
|
|
||
| export const getSearchResults = async (query: string): Promise<SearchResult[]> => { | ||
| const response = await axios.get<SearchResultResponse>(`/api/search?q=${query}&limit=20`); | ||
Hadley99 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return response.data.hits.data; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
client/src/features/trainee-profile/education/strikes/api/api.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import axios from 'axios'; | ||
| import { Strike } from '../../../../../data/types/Trainee'; | ||
|
|
||
| export const getStrikes = async (traineeId: string) => { | ||
| const { data } = await axios.get<Strike[]>(`/api/trainees/${traineeId}/strikes`); | ||
| return data; | ||
| }; | ||
|
|
||
| export const addStrike = async (traineeId: string, strike: Strike) => { | ||
| try { | ||
| await axios.post(`/api/trainees/${traineeId}/strikes`, strike); | ||
| } catch (error) { | ||
| if (axios.isAxiosError(error)) { | ||
| throw new Error(error.response?.data?.error || 'Failed to add strike'); | ||
| } | ||
| throw error; | ||
| } | ||
Hadley99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| export const deleteStrike = async (traineeId: string, strikeId: string) => { | ||
| await axios.delete(`/api/trainees/${traineeId}/strikes/${strikeId}`); | ||
| }; | ||
|
|
||
| export const editStrike = async (traineeId: string, strike: Strike) => { | ||
| try { | ||
| await axios.put(`/api/trainees/${traineeId}/strikes/${strike.id}`, strike); | ||
| } catch (error) { | ||
| if (axios.isAxiosError(error)) { | ||
| throw new Error(error.response?.data?.error || 'Failed to edit strike'); | ||
| } | ||
| throw error; | ||
| } | ||
Hadley99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
client/src/features/trainee-profile/education/tests/api/api.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import axios from 'axios'; | ||
| import { Test } from '../../../../../data/types/Trainee'; | ||
|
|
||
| export const getTests = async (traineeId: string) => { | ||
| const { data } = await axios.get<Test[]>(`/api/trainees/${traineeId}/tests`); | ||
| return data; | ||
| }; | ||
|
|
||
| export const addTest = async (traineeId: string, test: Test) => { | ||
| try { | ||
| await axios.post(`/api/trainees/${traineeId}/tests`, test); | ||
| } catch (error) { | ||
| if (axios.isAxiosError(error)) { | ||
| throw new Error(error.response?.data?.error || 'Failed to add test'); | ||
| } | ||
| throw error; | ||
| } | ||
Hadley99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| export const deleteTest = async (traineeId: string, testId: string) => { | ||
| await axios.delete(`/api/trainees/${traineeId}/tests/${testId}`); | ||
| }; | ||
|
|
||
| export const editTest = async (traineeId: string, test: Test) => { | ||
| try { | ||
| await axios.put(`/api/trainees/${traineeId}/tests/${test.id}`, test); | ||
| } catch (error) { | ||
| if (axios.isAxiosError(error)) { | ||
| throw new Error(error.response?.data?.error || 'Failed to edit test'); | ||
| } | ||
| throw error; | ||
| } | ||
Hadley99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
client/src/features/trainee-profile/interactions/api/api.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import axios from 'axios'; | ||
| import { Interaction } from '../Interactions'; | ||
|
|
||
| export const getInteractions = async (traineeId: string) => { | ||
| try { | ||
| const { data } = await axios.get(`/api/trainees/${traineeId}/interactions`); | ||
| return data as Interaction[]; | ||
Hadley99 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } catch (error) { | ||
| if (axios.isAxiosError(error)) { | ||
| throw new Error(error.response?.data?.error || 'Failed to get interactions'); | ||
| } | ||
| throw new Error('Failed to get interactions'); | ||
Hadley99 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| }; | ||
|
|
||
| export const addInteraction = async (traineeId: string, interaction: Partial<Interaction>) => { | ||
| try { | ||
| await axios.post(`/api/trainees/${traineeId}/interactions`, interaction); | ||
| } catch (error) { | ||
| if (axios.isAxiosError(error)) { | ||
| throw new Error(error.response?.data?.error || 'Failed to add interaction'); | ||
| } | ||
| throw error; | ||
| } | ||
zalexa19 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| export const deleteInteraction = async (traineeId: string, interactionId: string) => { | ||
| try { | ||
| await axios.delete(`/api/trainees/${traineeId}/interactions/${interactionId}`); | ||
| } catch (error) { | ||
| if (axios.isAxiosError(error)) { | ||
| throw new Error(error.response?.data?.error || 'Failed to delete interaction'); | ||
| } | ||
| throw error; | ||
| } | ||
Hadley99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| export const editInteraction = async (traineeId: string, interaction: Interaction) => { | ||
| try { | ||
| await axios.put(`/api/trainees/${traineeId}/interactions/${interaction.id}`, interaction); | ||
| } catch (error) { | ||
| if (axios.isAxiosError(error)) { | ||
| throw new Error(error.response?.data?.error || 'Failed to edit interaction'); | ||
| } | ||
| throw error; | ||
| } | ||
Hadley99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.