Skip to content

Commit

Permalink
Increse limit for getMany, data provider refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
wittfeldt committed Nov 7, 2021
1 parent 05eda5c commit d3dde9f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 50 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lolocompany/react-admin-lolo",
"version": "2.1.1",
"version": "2.1.2",
"description": "A library for schema-driven web apps using [Lolo](https://lolo.company), [React Admin](https://github.com/marmelab/react-admin) and [RJSF](https://github.com/rjsf-team/react-jsonschema-form)",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
91 changes: 42 additions & 49 deletions src/providers/dataProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,36 @@ export default apiUrl => {
return { data: res.json };
};

const getList = async (resource, params, queryOpts = {}) => {
console.log('getList', resource, params, q);

const {
page = 1,
perPage = 10
} = params.pagination || {};

const {
field = 'id',
order = 'ASC'
} = params.sort || {};

query = {
limit: perPage,
sort: `${field} ${order.toLowerCase()}`,
offset: (page - 1) * perPage,
...buildQs(params.filter),
...queryOpts
};

const url = `/${resource}?${queryString.stringify(query)}`;
const res = await fetchJson(url);

return {
data: res.json[kebabToCamel(resource)],
total: res.json.total
};
};

return {
/**
* API URL
Expand All @@ -74,26 +104,7 @@ export default apiUrl => {
* getList
*/

getList: async (resource, params) => {
const { page = 1, perPage = 10 } = params.pagination || {};
const { field = 'id', order = 'ASC' } = params.sort || {};

const query = {
limit: perPage,
sort: `${field} ${order.toLowerCase()}`,
offset: (page - 1) * perPage,
...buildQs(params.filter),
};

const url = `/${resource}?${stringify(query)}`;
const res = await fetchJson(url);
const cKey = pluralize(camelize(resource.replace(/-/g, ''), true));

return {
data: res.json[kebabToCamel(resource)],
total: res.json.total,
};
},
getList,

/**
* getOne
Expand All @@ -107,43 +118,25 @@ export default apiUrl => {
/**
* getMany
*/

getMany: (resource, params) => {
const query = params.ids.reduce((memo, id) => {
return (memo += `&q[id]=${id}`);
}, `qor=1`);

const url = `/${resource}?${query}`;
params.filter = { id: params.ids };

return fetchJson(url).then(({ headers, json }) => ({
data: json[kebabToCamel(resource)],
total: json.total,
}));
return getList(resource, params, {
qor: 1,
qre: 0,
limit: 100
});
},

/**
* getManyReference
*/
getManyReference: (resource, params) => {
params.filter[params.target] = params.id;

getManyReference: async (resource, params) => {
const { page = 1, perPage = 10 } = params.pagination || {};
const { field = 'id', order = 'ASC' } = params.sort || {};

const query = {
limit: perPage,
sort: `${field} ${order.toLowerCase()}`,
offset: (page - 1) * perPage,
qre: 0,
...buildQs({ ...params.filter, [params.target]: params.id }),
};

const url = `/${resource}?${stringify(query)}`;
const res = await fetchJson(url);

return {
data: res.json[kebabToCamel(resource)],
total: res.json.total,
};
return getList(resource, params, {
qre: 0
});
},

/**
Expand Down

0 comments on commit d3dde9f

Please sign in to comment.