Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • cubicweb/react-admin-cubicweb
1 result
Show changes
Commits on Source (3)
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
SimpleShowLayout, SimpleShowLayout,
DataProvider, DataProvider,
} from "react-admin"; } from "react-admin";
import { client } from "@logilab/cwclientlibjs";
import { createDataProvider } from "ra-cubicweb/dist"; import { createDataProvider } from "ra-cubicweb/dist";
import { schema } from "./schema"; import { schema } from "./schema";
...@@ -81,7 +82,10 @@ ...@@ -81,7 +82,10 @@
</Show> </Show>
); );
const dataProvider = createDataProvider("http://localhost:8080", schema); const httpClient = new client.CwSimpleHttpClient("http://localhost:8080", true);
const rqlClient = new client.CwRqlClient(httpClient);
const dataProvider = createDataProvider(rqlClient, schema);
dataProvider.getList("toto", { dataProvider.getList("toto", {
pagination: { page: 1, perPage: 10 }, pagination: { page: 1, perPage: 10 },
sort: { field: "id", order: "ASC" }, sort: { field: "id", order: "ASC" },
......
...@@ -16,6 +16,6 @@ ...@@ -16,6 +16,6 @@
}; };
export function createDataProvider<S extends Schema>( export function createDataProvider<S extends Schema>(
endpoint: string, rqlClient: client.CwRqlClient,
schema: S schema: S
): DataProvider { ): DataProvider {
...@@ -20,5 +20,3 @@ ...@@ -20,5 +20,3 @@
schema: S schema: S
): DataProvider { ): DataProvider {
const httpClient = new client.CwSimpleHttpClient(endpoint, true);
const rqlClient = new client.CwRqlClient(httpClient);
return { return {
...@@ -24,5 +22,6 @@ ...@@ -24,5 +22,6 @@
return { return {
getList: async (resource: ETypesNames<S>, { pagination }) => { getList: async (resource: ETypesNames<S>, { pagination, sort }) => {
const sortAttribute = sort.field === "id" ? "eid" : sort.field;
const attributesNames = ["eid", ...Object.keys(schema.etypes[resource])]; const attributesNames = ["eid", ...Object.keys(schema.etypes[resource])];
const selection: string[] = []; const selection: string[] = [];
const restrictions: string[] = []; const restrictions: string[] = [];
...@@ -26,7 +25,8 @@ ...@@ -26,7 +25,8 @@
const attributesNames = ["eid", ...Object.keys(schema.etypes[resource])]; const attributesNames = ["eid", ...Object.keys(schema.etypes[resource])];
const selection: string[] = []; const selection: string[] = [];
const restrictions: string[] = []; const restrictions: string[] = [];
let sortvariable = null;
attributesNames.forEach((key, idx) => { attributesNames.forEach((key, idx) => {
const variable = `X${idx}`; const variable = `X${idx}`;
selection.push(variable); selection.push(variable);
restrictions.push(`X ${key} ${variable}`); restrictions.push(`X ${key} ${variable}`);
...@@ -29,10 +29,13 @@ ...@@ -29,10 +29,13 @@
attributesNames.forEach((key, idx) => { attributesNames.forEach((key, idx) => {
const variable = `X${idx}`; const variable = `X${idx}`;
selection.push(variable); selection.push(variable);
restrictions.push(`X ${key} ${variable}`); restrictions.push(`X ${key} ${variable}`);
if (key === sortAttribute) {
sortvariable = variable;
}
}); });
const total = await rqlClient const total = await rqlClient
.queryRows(`Any Count(${selection[0]}) WHERE ${restrictions.join(",")}`) .queryRows(`Any Count(${selection[0]}) WHERE ${restrictions.join(",")}`)
.then((rows) => rows[0][0]); .then((rows) => rows[0][0]);
return rqlClient return rqlClient
.queryRows( .queryRows(
...@@ -33,10 +36,12 @@ ...@@ -33,10 +36,12 @@
}); });
const total = await rqlClient const total = await rqlClient
.queryRows(`Any Count(${selection[0]}) WHERE ${restrictions.join(",")}`) .queryRows(`Any Count(${selection[0]}) WHERE ${restrictions.join(",")}`)
.then((rows) => rows[0][0]); .then((rows) => rows[0][0]);
return rqlClient return rqlClient
.queryRows( .queryRows(
`Any ${selection.join(", ")} LIMIT ${pagination.perPage} OFFSET ${ `Any ${selection.join(", ")} ORDERBY ${sortvariable} ${
sort.order
} LIMIT ${pagination.perPage} OFFSET ${
pagination.page * pagination.perPage pagination.page * pagination.perPage
} WHERE ${restrictions.join(", ")}`, } WHERE ${restrictions.join(", ")}`,
{} {}
...@@ -79,7 +84,7 @@ ...@@ -79,7 +84,7 @@
[attributesNames[idx]]: attributeValue, [attributesNames[idx]]: attributeValue,
...agg, ...agg,
}), }),
{ id: params.id } { eid: params.id, id: params.id }
); );
// Getting relations // Getting relations
......