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

Check if id is an array and update datastore api correctly #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
63 changes: 43 additions & 20 deletions src/hooks/useDatastore/fetch.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
import axios from 'axios';
import qs from 'qs'
import axios from "axios";
import qs from "qs";

export async function fetchDataFromQuery(id, rootUrl, options, additionalParams) {
const { keys, limit, offset, conditions, sort, prepareColumns, properties, setValues, setCount, setColumns, setLoading, setSchema } = options;
if(!id) {
export async function fetchDataFromQuery(
id,
rootUrl,
options,
additionalParams
) {
const {
keys,
limit,
offset,
conditions,
sort,
prepareColumns,
properties,
setValues,
setCount,
setColumns,
setLoading,
setSchema,
} = options;
if (!id) {
// TODO: Throw error
return false;
}
if(typeof setLoading === 'function') {
if (typeof setLoading === "function") {
setLoading(true);
}

let url = Array.isArray(id)
? `${rootUrl}/datastore/query/${id[0]}/${id[1]}`
: `${rootUrl}/datastore/query/${id}`;
return await axios({
method: 'GET',
url: `${rootUrl}/datastore/query/${id}`,
method: "GET",
url: url,
params: {
keys: keys,
limit: limit,
Expand All @@ -23,21 +45,22 @@ export async function fetchDataFromQuery(id, rootUrl, options, additionalParams)
...additionalParams,
},
paramsSerializer: (params) => {
return qs.stringify(params)
}
})
.then((res) => {
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)
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') {
setSchema(data.schema);
if (typeof setLoading === "function") {
setLoading(false);
}
return data;
})
});
}