Skip to content
Snippets Groups Projects
Commit 3f96e0b0f87b authored by Noé Gaumont's avatar Noé Gaumont :octopus:
Browse files

feat: add CityShow and display it in MuseumShow

parent e3b1995cd03a
No related branches found
No related tags found
No related merge requests found
...@@ -7,4 +7,5 @@ ...@@ -7,4 +7,5 @@
TextField, TextField,
DateField, DateField,
NumberField, NumberField,
ReferenceField,
ShowGuesser, ShowGuesser,
...@@ -10,7 +11,8 @@ ...@@ -10,7 +11,8 @@
ShowGuesser, ShowGuesser,
ListGuesser,
Edit, Edit,
SimpleForm, SimpleForm,
TextInput, TextInput,
NumberInput, NumberInput,
DateInput, DateInput,
EditProps, EditProps,
...@@ -11,7 +13,8 @@ ...@@ -11,7 +13,8 @@
Edit, Edit,
SimpleForm, SimpleForm,
TextInput, TextInput,
NumberInput, NumberInput,
DateInput, DateInput,
EditProps, EditProps,
ShowProps,
ListProps, ListProps,
...@@ -17,7 +20,9 @@ ...@@ -17,7 +20,9 @@
ListProps, ListProps,
Show,
SimpleShowLayout,
DataProvider, DataProvider,
} from "react-admin"; } from "react-admin";
import { createDataProvider } from "ra-cubicweb/dist"; import { createDataProvider } from "ra-cubicweb/dist";
import { schema } from "./schema"; import { schema } from "./schema";
...@@ -18,9 +23,24 @@ ...@@ -18,9 +23,24 @@
DataProvider, DataProvider,
} from "react-admin"; } from "react-admin";
import { createDataProvider } from "ra-cubicweb/dist"; import { createDataProvider } from "ra-cubicweb/dist";
import { schema } from "./schema"; import { schema } from "./schema";
export const MuseumShow = (props: ShowProps) => (
<Show {...props}>
<SimpleShowLayout>
<TextField source="postal_address" />
<NumberField source="longitude" />
<NumberField source="latitude" />
<TextField source="name" />
<TextField source="id" />
<ReferenceField label="City" link="show" reference="City" source="is_in">
<TextField source="name" />
</ReferenceField>
</SimpleShowLayout>
</Show>
);
const MuseumEdit = (props: EditProps) => ( const MuseumEdit = (props: EditProps) => (
<Edit {...props}> <Edit {...props}>
<SimpleForm> <SimpleForm>
...@@ -45,6 +65,26 @@ ...@@ -45,6 +65,26 @@
</List> </List>
); );
export const CityList = (props: ListProps) => (
<List {...props}>
<Datagrid rowClick="show">
<NumberField source="zip_code" />
<TextField source="name" />
<TextField source="id" />
</Datagrid>
</List>
);
export const CityShow = (props: ShowProps) => (
<Show {...props}>
<SimpleShowLayout>
<NumberField source="zip_code" />
<TextField source="name" />
<TextField source="id" />
</SimpleShowLayout>
</Show>
);
const dataProvider = createDataProvider("http://localhost:8080", schema); const dataProvider = createDataProvider("http://localhost:8080", schema);
dataProvider.getList("toto", { dataProvider.getList("toto", {
pagination: { page: 1, perPage: 10 }, pagination: { page: 1, perPage: 10 },
...@@ -62,6 +102,6 @@ ...@@ -62,6 +102,6 @@
<Resource <Resource
name="Museum" name="Museum"
list={MuseumList} list={MuseumList}
show={ShowGuesser} show={MuseumShow}
edit={MuseumEdit} edit={MuseumEdit}
/> />
...@@ -66,5 +106,6 @@ ...@@ -66,5 +106,6 @@
edit={MuseumEdit} edit={MuseumEdit}
/> />
<Resource name="City" list={CityList} show={CityShow} />
</Admin> </Admin>
); );
......
import { client } from "@logilab/cwclientlibjs"; import { client } from "@logilab/cwclientlibjs";
import { AuthProvider, DataProvider } from "ra-core"; import { AuthProvider, DataProvider } from "ra-core";
import { Schema, ETypesNames } from "./Schema"; import { Schema, ETypesNames, Relationships } from "./Schema";
// import authProvider from './authProvider'; // import authProvider from './authProvider';
// see https://marmelab.com/react-admin/Authentication.html // see https://marmelab.com/react-admin/Authentication.html
...@@ -60,8 +60,10 @@ ...@@ -60,8 +60,10 @@
}; };
}); });
}, },
getOne: async (resource: ETypesNames<S>, _params) => { getOne: async (resource: ETypesNames<S>, params) => {
const attributesNames = ["eid", ...Object.keys(schema.etypes[resource])]; // FIXME Retrieve object relation
// Getting attributes
const attributesNames = [...Object.keys(schema.etypes[resource])];
const selection: string[] = []; const selection: string[] = [];
const restrictions: string[] = []; const restrictions: string[] = [];
attributesNames.forEach((key, idx) => { attributesNames.forEach((key, idx) => {
...@@ -69,4 +71,5 @@ ...@@ -69,4 +71,5 @@
selection.push(variable); selection.push(variable);
restrictions.push(`X ${key} ${variable}`); restrictions.push(`X ${key} ${variable}`);
}); });
const result = await rqlClient.queryRows( const result = await rqlClient.queryRows(
...@@ -72,4 +75,6 @@ ...@@ -72,4 +75,6 @@
const result = await rqlClient.queryRows( const result = await rqlClient.queryRows(
`Any ${selection.join(",")} Where ${restrictions.join(",")}` `Any ${selection.join(",")} Where ${restrictions.join(",")}, X eid ${
params.id
}`
); );
const row = result[0]; const row = result[0];
...@@ -74,12 +79,30 @@ ...@@ -74,12 +79,30 @@
); );
const row = result[0]; const row = result[0];
return { const entity = row.reduce(
data: row.reduce( (agg, attributeValue, idx) => ({
(agg, attributeValue, idx) => ({ [attributesNames[idx]]: attributeValue,
[attributesNames[idx]]: attributeValue, ...agg,
...agg, }),
}), { id: params.id }
{ id: row[0] } );
),
}; // Getting relations
const subjectRelations = Object.fromEntries(
Object.entries(schema.relationships).filter(
([_relationName, definition]: [string, S["relationships"][string]]) =>
definition.subject === resource
)
);
for (const relationName in subjectRelations) {
const definition: S["relationships"][string] =
subjectRelations[relationName];
const result = await rqlClient.queryRows(
`Any TARGET Where TARGET is ${definition.object},X ${relationName} TARGET, X eid ${params.id}`
);
if (result.length !== 0) {
entity[relationName] = result.map((row) => row[0]);
}
}
return { data: entity };
}, },
...@@ -85,5 +108,32 @@ ...@@ -85,5 +108,32 @@
}, },
getMany: (_resource, _params) => Promise.reject("Not implemented"), getMany: async (resource, params) => {
// FIXME handle several params id and refactor code to use GetOne
// Getting attributes
const attributesNames = [...Object.keys(schema.etypes[resource])];
const selection: string[] = [];
const restrictions: string[] = [];
attributesNames.forEach((key, idx) => {
const variable = `X${idx}`;
selection.push(variable);
restrictions.push(`X ${key} ${variable}`);
});
const result = await rqlClient.queryRows(
`Any ${selection.join(",")} Where ${restrictions.join(",")}, X eid ${
params.ids[0]
}`
);
const row = result[0];
const entity = row.reduce(
(agg, attributeValue, idx) => ({
[attributesNames[idx]]: attributeValue,
...agg,
}),
{ id: params.ids[0] }
);
return { data: [entity] };
},
getManyReference: (_resource, _params) => Promise.reject("Not implemented"), getManyReference: (_resource, _params) => Promise.reject("Not implemented"),
update: (_resource, _params) => Promise.reject("Not implemented"), update: (_resource, _params) => Promise.reject("Not implemented"),
updateMany: (_resource, _params) => Promise.reject("Not implemented"), updateMany: (_resource, _params) => Promise.reject("Not implemented"),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment