Skip to content
Permalink
Browse files

WIP: refactoring

  • Loading branch information...
JF
JF committed Jul 8, 2019
1 parent 35eb437 commit 371dc6d3eadd9e55916559a166fa346ef838857e
@@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import {Admin, Loading, TranslationProvider, Query} from 'react-admin';
import {createMuiTheme} from '@material-ui/core/styles';
import ResourceGuesser from './ResourceGuesser';
import {existsAsChild} from './docsUtils';
import {notExistsAsChild} from './helpers';

const theme = createMuiTheme({
palette: {
@@ -17,50 +17,47 @@ const theme = createMuiTheme({
},
});

const AdminGuesser = ({children, blacklist, ...props}) => {
const notBlackListed = (blacklist, resourceName) => {
const blacklisted = new Set(blacklist);

return (
<Query type="INTROSPECT">
{({data, loading, error}) => {
if (loading) {
return (
<TranslationProvider>
<Loading />
</TranslationProvider>
);
}
if (error) {
console.error(error);
return <div>Error while reading the API schema</div>;
}
return !blacklisted.has(resourceName);
};

const AdminGuesser = ({children, blacklist, ...props}) => (
<Query type="INTROSPECT">
{({data, loading, error}) => {
if (loading) {
return (
<Admin theme={theme} {...props}>
{children || <></>}
{data.resources
.filter(
resource =>
!resource.deprecated &&
!blacklisted.has(resource.name) &&
existsAsChild(children)(resource.name),
)
.map(resource => (
<ResourceGuesser name={resource.name} key={resource.name} />
))}
</Admin>
<TranslationProvider>
<Loading />
</TranslationProvider>
);
}}
</Query>
);
};
}
if (error) {
console.error(error);
return <div>Error while reading the API schema</div>;
}

return (
<Admin theme={theme} {...props}>
{children || <></>}
{data.resources
.filter(
resource =>
!resource.deprecated &&
notBlackListed(blacklist, resource.name) &&
notExistsAsChild(children, 'name', resource.name),
)
.map(resource => (
<ResourceGuesser name={resource.name} key={resource.name} />
))}
</Admin>
);
}}
</Query>
);

AdminGuesser.propTypes = {
fieldFactory: PropTypes.func,
inputFactory: PropTypes.func,
parameterFactory: PropTypes.func,
resourceFactory: PropTypes.func,
dataProvider: PropTypes.func.isRequired,
resources: PropTypes.array,
blacklist: PropTypes.array,
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
};
@@ -1,54 +1,65 @@
import React, {Children} from 'react';
import React from 'react';
import PropTypes from 'prop-types';
import {Create, SimpleForm, Query, Loading} from 'react-admin';
import {existsAsChild, getResource} from './docsUtils';
import {getResourceProperties} from './helpers';
import InputGuesser from './InputGuesser';

const CreateGuesser = ({...props}) => {
const children = Children.toArray(props.children);
const {resource: resourceName} = props;

return (
<Query type="INTROSPECT">
{({data: api, loading, error}) => {
if (loading) {
return <Loading />;
}

if (error) {
console.error(error);
return <div>Error while reading the API schema</div>;
}

const resource = getResource(api.resources, resourceName);

if (!resource || !resource.writableFields) {
console.error(
`Resource ${resourceName} not present inside api description`,
);
return (
<div>
Resource ${resourceName} not present inside api description
</div>
);
}

const fields = resource.fields.filter(existsAsChild(children));
const CreateGuesserView = ({children, inputs, ...props}) => (
<Create {...props}>
<SimpleForm>
{children}
{inputs.map(input => (
<InputGuesser key={input.name} source={input.name} />
))}
</SimpleForm>
</Create>
);

CreateGuesserView.propTypes = {
children: PropTypes.object,
inputs: PropTypes.array.isRequired,
};

const CreateGuesser = props => (
<Query type="INTROSPECT">
{({data, loading, error}) => {
if (loading) {
return <Loading />;
}

if (error) {
console.error(error);
return <div>Error while reading the API schema</div>;
}

const {
resource: resourceName,
inputs: allowedInputNames,
children,
} = props;

const resource = data.resources.find(({name}) => resourceName === name);

if (!resource || !resource.writableFields) {
console.error(
`Resource ${resourceName} not present inside api description`,
);
return (
<Create {...props}>
<SimpleForm>
{children}
{fields.map(field => (
<InputGuesser key={field.name} source={field.name} />
))}
</SimpleForm>
</Create>
<div>Resource ${resourceName} not present inside api description</div>
);
}}
</Query>
);
};
}

const inputs = getResourceProperties(
resource,
'writable',
children,
allowedInputNames,
);

return <CreateGuesserView {...props} inputs={inputs} />;
}}
</Query>
);

export default CreateGuesser;

@@ -1,12 +1,28 @@
import React from 'react';
import {Query, Edit, Loading, SimpleForm} from 'react-admin';
import PropTypes from 'prop-types';
import {Edit, SimpleForm, Query, Loading} from 'react-admin';
import {getResourceProperties} from './helpers';
import InputGuesser from './InputGuesser';
import {existsAsChild} from './docsUtils';

const EditGuesserView = ({children, inputs, ...props}) => (
<Edit {...props}>
<SimpleForm>
{children}
{inputs.map(input => (
<InputGuesser key={input.name} source={input.name} />
))}
</SimpleForm>
</Edit>
);

EditGuesserView.propTypes = {
children: PropTypes.object,
inputs: PropTypes.array.isRequired,
};

const EditGuesser = props => (
<Query type="INTROSPECT" resource={props.ressource}>
<Query type="INTROSPECT">
{({data, loading, error}) => {
const {resource, children} = props;
if (loading) {
return <Loading />;
}
@@ -15,33 +31,40 @@ const EditGuesser = props => (
console.error(error);
return <div>Error while reading the API schema</div>;
}
const resourceSchema = data.resources.find(r => r.name === resource);

if (!resourceSchema || !resourceSchema.fields) {
const {
resource: resourceName,
inputs: allowedInputNames,
children,
} = props;

const resource = data.resources.find(({name}) => resourceName === name);

if (!resource || !resource.writableFields) {
console.error(
`Resource ${props.resource} not present inside api description`,
`Resource ${resourceName} not present inside api description`,
);
return (
<div>
Resource ${props.resource} not present inside api description
</div>
<div>Resource ${resourceName} not present inside api description</div>
);
}

const fields = resourceSchema.fields.filter(existsAsChild(children));

return (
<Edit {...props}>
<SimpleForm>
{children}
{fields.map(field => (
<InputGuesser key={field.name} source={field.name} />
))}
</SimpleForm>
</Edit>
const inputs = getResourceProperties(
resource,
'writable',
children,
allowedInputNames,
);

return <EditGuesserView {...props} inputs={inputs} />;
}}
</Query>
);

export default EditGuesser;

EditGuesser.propTypes = {
children: PropTypes.object,
resource: PropTypes.string.isRequired,
inputs: PropTypes.array,
};
@@ -14,11 +14,7 @@ import {
Query,
Loading,
} from 'react-admin';
import {
getReferenceNameField,
getResource,
getResourceField,
} from './docsUtils';
import {getReferenceNameField} from './helpers';

const isFieldSortable = (field, resource) => {
return (
@@ -83,7 +79,7 @@ const FieldGuesser = props => {

return (
<Query type="INTROSPECT">
{({data: api, loading, error}) => {
{({data, loading, error}) => {
if (loading) {
return <Loading />;
}
@@ -93,7 +89,7 @@ const FieldGuesser = props => {
return <div>Error while reading the API schema</div>;
}

const resource = getResource(api.resources, resourceName);
const resource = data.resources.find(({name}) => resourceName === name);

if (!resource || !resource.fields) {
console.error(
@@ -106,7 +102,9 @@ const FieldGuesser = props => {
);
}

const field = getResourceField(resource, fieldName);
const field = resource.readableFields.find(
({name}) => fieldName === name,
);

if (!field) {
console.error(

0 comments on commit 371dc6d

Please sign in to comment.
You can’t perform that action at this time.