From ffb9a1b4da36d1a927623cdd5bf4070e840b394d Mon Sep 17 00:00:00 2001 From: dgading Date: Thu, 23 Dec 2021 10:56:11 -0500 Subject: [PATCH] Check if id is an array and update datastore api correctly --- src/hooks/useDatastore/fetch.js | 63 ++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/src/hooks/useDatastore/fetch.js b/src/hooks/useDatastore/fetch.js index 1dfefcb..b44b069 100644 --- a/src/hooks/useDatastore/fetch.js +++ b/src/hooks/useDatastore/fetch.js @@ -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, @@ -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; - }) + }); }