Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions client/src/features/cohorts/api/api.ts
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;
};
8 changes: 2 additions & 6 deletions client/src/features/cohorts/data/useCohortsData.tsx
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
});
};
7 changes: 7 additions & 0 deletions client/src/features/dashboard/api/api.ts
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;
};
10 changes: 3 additions & 7 deletions client/src/features/dashboard/data/useDashboardData.tsx
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
});
};
6 changes: 6 additions & 0 deletions client/src/features/search/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ export interface SearchResult {
profilePath: string;
cohort: number | null;
}

export interface SearchResultResponse {
hits: {
data: SearchResult[];
};
}
7 changes: 7 additions & 0 deletions client/src/features/search/api/api.ts
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`);
return response.data.hits.data;
};
19 changes: 2 additions & 17 deletions client/src/features/search/data/queries.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { SearchResult } from '../Search';
import axios from 'axios';
import { useQuery } from '@tanstack/react-query';
import { getSearchResults } from '../api/api';

export const searchKeys = {
all: ['search'] as const, // for broad invalidation
Expand All @@ -11,23 +10,9 @@ export const searchKeys = {
export const useSearch = (query: string) => {
return useQuery({
queryKey: searchKeys.byQuery(query),
queryFn: async () => {
const response = await axios.get(`/api/search?q=${query}&limit=20`);
const trainees: SearchResult[] = response.data.hits.data.map((trainee: SearchResult) => trainee);
return trainees;
},
queryFn: () => getSearchResults(query),
enabled: query.length > 1, // Query runs only if search string has more than 1 character
refetchOnMount: false,
refetchOnWindowFocus: false,
});
};

interface SearchResultResponse {
hits: {
data: SearchResult[];
};
}
export const getSearchResults = async (query: string): Promise<SearchResult[]> => {
const response = await axios.get<SearchResultResponse>(`/api/search?q=${query}&limit=20`);
return response.data.hits.data;
};
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;
}
};

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;
}
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useMutation, useQuery } from '@tanstack/react-query';

import { Strike } from '../../../../../data/types/Trainee';
import axios from 'axios';
import { getStrikes, addStrike, deleteStrike, editStrike } from '../api/api';

/**
* Hook to add a strike to a trainee.
Expand All @@ -10,11 +9,7 @@ import axios from 'axios';
*/
export const useAddStrike = (traineeId: string) => {
return useMutation({
mutationFn: async (strike: Strike) => {
return axios.post(`/api/trainees/${traineeId}/strikes`, strike).catch((error) => {
throw new Error(error.response?.data?.error || 'Failed to add strike');
});
},
mutationFn: (strike: Strike) => addStrike(traineeId, strike),
});
};

Expand All @@ -27,8 +22,8 @@ export const useGetStrikes = (traineeId: string) => {
return useQuery({
queryKey: ['strikes', traineeId],
queryFn: async () => {
const { data } = await axios.get<Strike[]>(`/api/trainees/${traineeId}/strikes`);
return orderStrikesByDateDesc(data as Strike[]);
const data = await getStrikes(traineeId);
return orderStrikesByDateDesc(data);
},
enabled: !!traineeId,
refetchOnWindowFocus: false,
Expand All @@ -43,9 +38,7 @@ export const useGetStrikes = (traineeId: string) => {

export const useDeleteStrike = (traineeId: string) => {
return useMutation({
mutationFn: async (strikeId: string) => {
return axios.delete(`/api/trainees/${traineeId}/strikes/${strikeId}`);
},
mutationFn: (strikeId: string) => deleteStrike(traineeId, strikeId),
});
};

Expand All @@ -55,11 +48,7 @@ export const useDeleteStrike = (traineeId: string) => {
*/
export const useEditStrike = (traineeId: string) => {
return useMutation({
mutationFn: async (strike: Strike) => {
return axios.put(`/api/trainees/${traineeId}/strikes/${strike.id}`, strike).catch((error) => {
throw new Error(error.response?.data?.error || 'Failed to edit strike');
});
},
mutationFn: (strike: Strike) => editStrike(traineeId, strike),
});
};

Expand Down
33 changes: 33 additions & 0 deletions client/src/features/trainee-profile/education/tests/api/api.ts
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;
}
};

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;
}
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useMutation, useQuery } from '@tanstack/react-query';

import { Test } from '../../../../../data/types/Trainee';
import axios from 'axios';
import { getTests, addTest, deleteTest, editTest } from '../api/api';

/**
* Hook to add a test to a trainee.
Expand All @@ -10,11 +9,7 @@ import axios from 'axios';
*/
export const useAddTest = (traineeId: string) => {
return useMutation({
mutationFn: async (test: Test) => {
return axios.post(`/api/trainees/${traineeId}/tests`, test).catch((error) => {
throw new Error(error.response?.data?.error || 'Failed to add test');
});
},
mutationFn: (test: Test) => addTest(traineeId, test),
});
};

Expand All @@ -27,7 +22,7 @@ export const useGetTests = (traineeId: string) => {
return useQuery({
queryKey: ['tests', traineeId],
queryFn: async () => {
const { data } = await axios.get<Test[]>(`/api/trainees/${traineeId}/tests`);
const data = await getTests(traineeId);
return orderTestsByDateDesc(data);
},

Expand All @@ -44,9 +39,7 @@ export const useGetTests = (traineeId: string) => {

export const useDeleteTest = (traineeId: string) => {
return useMutation({
mutationFn: (testId: string) => {
return axios.delete(`/api/trainees/${traineeId}/tests/${testId}`);
},
mutationFn: (testId: string) => deleteTest(traineeId, testId),
});
};

Expand All @@ -56,11 +49,7 @@ export const useDeleteTest = (traineeId: string) => {
*/
export const useEditTest = (traineeId: string) => {
return useMutation({
mutationFn: async (test: Test) => {
return axios.put(`/api/trainees/${traineeId}/tests/${test.id}`, test).catch((error) => {
throw new Error(error.response?.data?.error || 'Failed to edit test');
});
},
mutationFn: (test: Test) => editTest(traineeId, test),
});
};

Expand Down
47 changes: 47 additions & 0 deletions client/src/features/trainee-profile/interactions/api/api.ts
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[];
} catch (error) {
if (axios.isAxiosError(error)) {
throw new Error(error.response?.data?.error || 'Failed to get interactions');
}
throw new Error('Failed to get interactions');
}
};

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;
}
};

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;
}
};

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;
}
};
Loading
Loading