Skip to content

Commit

Permalink
Merge pull request #1648 from ral-facilities/#1528-isis-data-pubs
Browse files Browse the repository at this point in the history
Implement new ISIS data publication hierarchy #1528
  • Loading branch information
louise-davies authored Feb 19, 2024
2 parents 794f26e + 9341b7f commit 03624bf
Show file tree
Hide file tree
Showing 54 changed files with 4,293 additions and 2,084 deletions.
122 changes: 108 additions & 14 deletions packages/datagateway-common/src/api/dataPublications.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
useDataPublicationsInfinite,
useDataPublicationsPaginated,
useDataPublication,
useDataPublications,
} from './dataPublications';

jest.mock('../handleICATError');
Expand Down Expand Up @@ -276,7 +277,7 @@ describe('data publications api functions', () => {
describe('useDataPublication', () => {
it('sends axios request to fetch a single data publication and returns successful response', async () => {
(axios.get as jest.Mock).mockResolvedValue({
data: mockData[0],
data: mockData,
});

const { result, waitFor } = renderHook(() => useDataPublication(1), {
Expand All @@ -292,18 +293,26 @@ describe('data publications api functions', () => {
id: { eq: 1 },
})
);
params.append('include', JSON.stringify('users'));
params.append(
'include',
JSON.stringify({
content: {
dataCollectionInvestigations: {
investigation: {
investigationInstruments: 'instrument',
JSON.stringify([
{
content: {
dataCollectionInvestigations: {
investigation: [
'datasets',
{
datasets: 'type',
investigationInstruments: 'instrument',
},
],
},
},
},
})
'users',
'facility',
'dates',
])
);

expect(axios.get).toHaveBeenCalledWith(
Expand Down Expand Up @@ -335,20 +344,105 @@ describe('data publications api functions', () => {
id: { eq: 1 },
})
);
params.append('include', JSON.stringify('users'));
params.append(
'include',
JSON.stringify({
content: {
dataCollectionInvestigations: {
investigation: {
investigationInstruments: 'instrument',
JSON.stringify([
{
content: {
dataCollectionInvestigations: {
investigation: [
'datasets',
{
datasets: 'type',
investigationInstruments: 'instrument',
},
],
},
},
},
'users',
'facility',
'dates',
])
);

expect(axios.get).toHaveBeenCalledWith(
'https://example.com/api/datapublications',
expect.objectContaining({
params,
})
);
expect((axios.get as jest.Mock).mock.calls[0][1].params.toString()).toBe(
params.toString()
);
expect(handleICATError).toHaveBeenCalledWith({ message: 'Test error' });
});
});

describe('useDataPublications', () => {
it('sends axios request to fetch a data publications with specified filters and returns successful response', async () => {
(axios.get as jest.Mock).mockResolvedValue({
data: mockData,
});

params.append('order', JSON.stringify('id asc'));
params.append(
'where',
JSON.stringify({
name: { eq: 'test' },
})
);

const { result, waitFor } = renderHook(
() =>
useDataPublications([
{
filterType: 'where',
filterValue: JSON.stringify({ name: { eq: 'test' } }),
},
]),
{
wrapper: createReactQueryWrapper(history),
}
);

await waitFor(() => result.current.isSuccess);

expect(axios.get).toHaveBeenCalledWith(
'https://example.com/api/datapublications',
expect.objectContaining({
params,
})
);
expect((axios.get as jest.Mock).mock.calls[0][1].params.toString()).toBe(
params.toString()
);
expect(result.current.data).toEqual(mockData);
});

it('sends axios request to fetch a single data publication and calls handleICATError on failure', async () => {
(axios.get as jest.Mock).mockRejectedValue({
message: 'Test error',
});

params.append('order', JSON.stringify('id asc'));
params.append('include', '"type"');

const { result, waitFor } = renderHook(
() =>
useDataPublications([
{
filterType: 'include',
filterValue: '"type"',
},
]),
{
wrapper: createReactQueryWrapper(history),
}
);

await waitFor(() => result.current.isError);

expect(axios.get).toHaveBeenCalledWith(
'https://example.com/api/datapublications',
expect.objectContaining({
Expand Down
71 changes: 54 additions & 17 deletions packages/datagateway-common/src/api/dataPublications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
useQuery,
UseInfiniteQueryResult,
useInfiniteQuery,
UseQueryOptions,
} from 'react-query';
import { useSelector } from 'react-redux';
import { useLocation } from 'react-router-dom';
Expand Down Expand Up @@ -144,16 +145,17 @@ export const useDataPublicationsInfinite = (
};

export const useDataPublication = (
dataPublicationId: number
): UseQueryResult<DataPublication[], AxiosError> => {
const apiUrl = useSelector((state: StateType) => state.dgcommon.urls.apiUrl);

return useQuery<
dataPublicationId: number,
queryOptions?: UseQueryOptions<
DataPublication[],
AxiosError,
DataPublication[],
DataPublication,
[string, number]
>(
>
): UseQueryResult<DataPublication, AxiosError> => {
const apiUrl = useSelector((state: StateType) => state.dgcommon.urls.apiUrl);

return useQuery(
['dataPublication', dataPublicationId],
() => {
return fetchDataPublications(apiUrl, { sort: {}, filters: {} }, [
Expand All @@ -165,22 +167,57 @@ export const useDataPublication = (
},
{
filterType: 'include',
filterValue: JSON.stringify('users'),
},
{
filterType: 'include',
filterValue: JSON.stringify({
content: {
dataCollectionInvestigations: {
investigation: {
investigationInstruments: 'instrument',
filterValue: JSON.stringify([
{
content: {
dataCollectionInvestigations: {
investigation: [
'datasets',
{
datasets: 'type',
investigationInstruments: 'instrument',
},
],
},
},
},
}),
'users',
'facility',
'dates',
]),
},
]);
},
{
onError: (error) => {
handleICATError(error);
},
retry: retryICATErrors,
select: (data) => data[0],
...queryOptions,
}
);
};

export const useDataPublications = (
additionalFilters: AdditionalFilters
): UseQueryResult<DataPublication[], AxiosError> => {
const apiUrl = useSelector((state: StateType) => state.dgcommon.urls.apiUrl);

return useQuery<
DataPublication[],
AxiosError,
DataPublication[],
[string, AdditionalFilters?]
>(
['dataPublication', additionalFilters],
(params) => {
return fetchDataPublications(
apiUrl,
{ sort: {}, filters: {} },
additionalFilters
);
},
{
onError: (error) => {
handleICATError(error);
Expand Down
13 changes: 9 additions & 4 deletions packages/datagateway-common/src/app.types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ export interface DataCollectionDataset {

export interface DataCollectionInvestigation {
id: number;
dataCollection: DataCollection;
investigation: Investigation;
dataCollection?: DataCollection;
investigation?: Investigation;
}

export interface DataCollection {
Expand All @@ -171,16 +171,21 @@ export interface DataPublicationUser {
fullName: string;
}

export interface DataPublicationType {
id: number;
name: string;
}

export interface DataPublication {
id: number;
pid: string;
title: string;
modTime: string;
createTime: string;
facility?: Facility;
description?: string;
publicationDate?: string;
users?: DataPublicationUser[];
content?: DataCollection;
type?: DataPublicationType;
}

interface InstrumentScientist {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ exports[`Investigation details panel component should check if multiple publicat
<a
class="MuiTypography-root MuiTypography-inherit MuiLink-root MuiLink-underlineAlways css-1ps4owl-MuiTypography-root-MuiLink-root"
data-testid="investigation-details-panel-pid-link"
href="https://doi.org/Data Publication Pid"
href="https://doi.org/Data Publication Study Pid"
>
Data Publication Pid
Data Publication Study Pid
</a>
</p>
</div>
Expand Down Expand Up @@ -388,9 +388,9 @@ exports[`Investigation details panel component should check if multiple samples
<a
class="MuiTypography-root MuiTypography-inherit MuiLink-root MuiLink-underlineAlways css-1ps4owl-MuiTypography-root-MuiLink-root"
data-testid="investigation-details-panel-pid-link"
href="https://doi.org/Data Publication Pid"
href="https://doi.org/Data Publication Study Pid"
>
Data Publication Pid
Data Publication Study Pid
</a>
</p>
</div>
Expand Down Expand Up @@ -822,9 +822,9 @@ exports[`Investigation details panel component should render correctly 1`] = `
<a
class="MuiTypography-root MuiTypography-inherit MuiLink-root MuiLink-underlineAlways css-1ps4owl-MuiTypography-root-MuiLink-root"
data-testid="investigation-details-panel-pid-link"
href="https://doi.org/Data Publication Pid"
href="https://doi.org/Data Publication Study Pid"
>
Data Publication Pid
Data Publication Study Pid
</a>
</p>
</div>
Expand Down Expand Up @@ -1057,9 +1057,9 @@ exports[`Investigation details panel component should render user, sample and pu
<a
class="MuiTypography-root MuiTypography-inherit MuiLink-root MuiLink-underlineAlways css-1ps4owl-MuiTypography-root-MuiLink-root"
data-testid="investigation-details-panel-pid-link"
href="https://doi.org/Data Publication Pid"
href="https://doi.org/Data Publication Study Pid"
>
Data Publication Pid
Data Publication Study Pid
</a>
</p>
</div>
Expand Down
Loading

0 comments on commit 03624bf

Please sign in to comment.