Skip to content
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

Add new option to use query index api endpoint #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@civicactions/data-catalog-services",
"version": "1.5.0",
"version": "1.6.0-alpha.1",
"description": "Functions and React components to connect to the DKAN api.",
"main": "lib/index.js",
"scripts": {
Expand Down
68 changes: 36 additions & 32 deletions src/hooks/useDatastore/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,40 @@ export async function fetchDataFromQuery(
if (typeof setLoading === "function") {
setLoading(true);
}
return await axios({
method: "GET",
url: `${rootUrl}/datastore/query/${id}`,
params: {
keys: keys,
limit: limit,
offset: offset,
conditions: conditions,
sorts: sort,
properties: properties,
groupings: groupings,
...additionalParams,
},
paramsSerializer: (params) => {
return qs.stringify(params);
},
}).then((res) => {
const { data } = res;
const propertyKeys =
data.schema[id] && data.schema[id].fields
? Object.keys(data.schema[id].fields)
: [];
setValues(data.results), setCount(data.count);
if (propertyKeys.length) {
setColumns(prepareColumns ? prepareColumns(propertyKeys) : propertyKeys);
}
setSchema(data.schema);
if (typeof setLoading === "function") {
setLoading(false);
}
return data;
});

let url = `${rootUrl}/datastore/query/${id}`;
if (Array.isArray(id)) {
url = `${rootUrl}/datastore/query/${id[0]}/${id[1]}`;
}
const params = {
keys: keys,
limit: limit,
offset: offset,
conditions: conditions,
sorts: sort,
properties: properties,
groupings: groupings,
...additionalParams,
};

const res = await axios.get(`${url}?${qs.stringify(params)}`);
const data = res.data;

let schema_fields = [];
if (Object.keys(data.schema).length > 0) {
const schemaId = Object.keys(data.schema)[0];
schema_fields = Object.keys(data.schema[schemaId].fields);
}

setValues(data.results);
setCount(data.count);
if (schema_fields.length) {
setColumns(prepareColumns ? prepareColumns(schema_fields) : schema_fields);
}
setSchema(data.schema);
if (typeof setLoading === "function") {
setLoading(false);
}

return data;
}
42 changes: 41 additions & 1 deletion src/hooks/useDatastore/fetch.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import axios from "axios";
import { act } from "@testing-library/react-hooks";
import { fetchDataFromQuery } from "./fetch";

jest.mock("axios");
jest.useFakeTimers();

const rootUrl = "http://dkan.com/api/1";
const data = {
data: {
Expand All @@ -18,6 +21,7 @@ const data = {
},
},
};

const distribution = {
identifier: "1234-1234",
data: {
Expand All @@ -29,7 +33,7 @@ const distribution = {

describe("fetchDataFromQuery", () => {
test("returns data from datastore query endpoint", async () => {
axios.mockImplementation(() => Promise.resolve(data));
axios.get.mockImplementation(() => Promise.resolve(data));
const results = await fetchDataFromQuery(distribution.identifier, rootUrl, {
keys: true,
limit: 20,
Expand All @@ -41,6 +45,42 @@ describe("fetchDataFromQuery", () => {
setColumns: () => {},
setSchema: () => {},
});
await act(async () => {
jest.runAllTimers();
});

expect(axios.get).toHaveBeenCalledWith(
`${rootUrl}/datastore/query/1234-1234?keys=true&limit=20&offset=0`
);

expect(results.count).toEqual(data.data.count);
expect(results.results).toEqual(data.data.results);
});
test("returns data from datastore query index endpoint", async () => {
axios.get.mockImplementation(() => Promise.resolve(data));
const results = await fetchDataFromQuery(
[distribution.identifier, 0],
rootUrl,
{
keys: true,
limit: 20,
offset: 0,
conditions: [],
sorts: [],
setValues: () => {},
setCount: () => {},
setColumns: () => {},
setSchema: () => {},
}
);
await act(async () => {
jest.runAllTimers();
});

expect(axios.get).toHaveBeenCalledWith(
`${rootUrl}/datastore/query/1234-1234/0?keys=true&limit=20&offset=0`
);

expect(results.count).toEqual(data.data.count);
expect(results.results).toEqual(data.data.results);
});
Expand Down
19 changes: 18 additions & 1 deletion src/hooks/useDatastore/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ const useDatastore = (
const prevLimitRef = useRef();
const prevOffsetRef = useRef();

let queryId = id;
let queryIndex = 0;

if (Array.isArray(id)) {
queryId = id[0];
queryIndex = id[1];
}

useEffect(() => {
prevLimitRef.current = limit;
prevOffsetRef.current = offset;
Expand Down Expand Up @@ -83,7 +91,16 @@ const useDatastore = (
}
}
}
}, [id, rootUrl, offset, conditions, sort, limit, requireConditions]);
}, [
queryId,
queryIndex,
rootUrl,
offset,
conditions,
sort,
limit,
requireConditions,
]);

return {
loading,
Expand Down
28 changes: 26 additions & 2 deletions src/hooks/useDatastore/useDatastore.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const distribution = {
};

describe("useDatastore Custom Hook", () => {
test("returns data from datastore query endpoint", async () => {
axios.mockImplementation(() => Promise.resolve(data));
test("returns data from datastore query endpoint with distribution id", async () => {
axios.get.mockImplementation(() => Promise.resolve(data));
const { result } = renderHook(() =>
useDatastore(distribution.identifier, rootUrl, {})
);
Expand All @@ -56,4 +56,28 @@ describe("useDatastore Custom Hook", () => {
});
expect(result.current.offset).toEqual(25);
});
test("returns data from datastore query endpoint with dataset id", async () => {
axios.get.mockImplementation(() => Promise.resolve(data));
const { result } = renderHook(() =>
useDatastore([distribution.identifier, 0], rootUrl, {})
);
await act(async () => {});
expect(result.current.values).toEqual(data.data.results);
expect(result.current.limit).toEqual(20);
expect(result.current.offset).toEqual(0);
expect(result.current.count).toEqual("1");
expect(result.current.columns).toEqual([
"record_id",
"column_1",
"column_2",
]);
await act(async () => {
result.current.setLimit(100);
});
expect(result.current.limit).toEqual(100);
await act(async () => {
result.current.setOffset(25);
});
expect(result.current.offset).toEqual(25);
});
});