-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathuseGetStudysetById.tsx
More file actions
50 lines (45 loc) · 1.64 KB
/
useGetStudysetById.tsx
File metadata and controls
50 lines (45 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { AxiosError, AxiosResponse } from 'axios';
import { StudyReturn, StudysetReturn } from 'neurostore-typescript-sdk';
import { useSnackbar } from 'notistack';
import { useQuery } from 'react-query';
import API from 'utils/api';
export const STUDYSET_QUERY_STRING = 'studysets';
const useGetStudysetById = (studysetId?: string, nested?: boolean) => {
const { enqueueSnackbar } = useSnackbar();
const { data, isLoading, isError, error, refetch, isRefetching } = useQuery<
AxiosResponse<StudysetReturn>,
AxiosError,
StudysetReturn,
[string, string | undefined, boolean | undefined]
>(
[STUDYSET_QUERY_STRING, studysetId, nested],
() => API.NeurostoreServices.StudySetsService.studysetsIdGet(studysetId || '', nested),
{
enabled: !!studysetId,
onError: () => {
enqueueSnackbar('there was an error retrieving the studyset', { variant: 'error' });
},
select: (res) => {
res.data.studies?.sort((a, b) => {
if (typeof a === 'string' && typeof b === 'string') {
return a.localeCompare(b);
} else {
const studyA = a as StudyReturn;
const studyB = b as StudyReturn;
return (studyA.name || '').localeCompare(studyB.name || '');
}
});
return res.data;
},
}
);
return {
data,
isLoading,
isError,
error,
refetch,
isRefetching,
};
};
export default useGetStudysetById;