Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dash operation naming strategy #374

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@api-platform/api-doc-parser": "^0.12.0",
"final-form-submit-errors": "^0.1.0",
"history": "^4.7.2",
"inflection": "^1.12.0",
"jsonld": "^3.2.0",
"lodash.isplainobject": "^4.0.6",
"prop-types": "^15.6.2",
Expand Down
33 changes: 24 additions & 9 deletions src/ResourceGuesser.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
import React from 'react';
import inflection from 'inflection';
import { Resource } from 'react-admin';
import PropTypes from 'prop-types';
import ListGuesser from './ListGuesser';
import CreateGuesser from './CreateGuesser';
import EditGuesser from './EditGuesser';
import ShowGuesser from './ShowGuesser';

const ResourceGuesser = ({ list, edit, create, show, ...props }) => (
<Resource
{...props}
create={undefined === create ? CreateGuesser : create}
edit={undefined === edit ? EditGuesser : edit}
list={undefined === list ? ListGuesser : list}
show={undefined === show ? ShowGuesser : show}
/>
);
const ResourceGuesser = ({ list, edit, create, show, ...props }) => {
if (typeof props.options === 'undefined') {
props.options = {};
}

props.options.label =
typeof props.options.label !== 'undefined'
? props.options.label
: inflection.transform(props.name.replace(/-/g, '_'), [
'underscore',
'humanize',
]);

return (
<Resource
{...props}
create={undefined === create ? CreateGuesser : create}
edit={undefined === edit ? EditGuesser : edit}
list={undefined === list ? ListGuesser : list}
show={undefined === show ? ShowGuesser : show}
/>
);
};

ResourceGuesser.propTypes = {
name: PropTypes.string.isRequired,
Expand Down
40 changes: 40 additions & 0 deletions src/__snapshots__/ResourceGuesser.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ exports[`<ResourceGuesser /> renders with create 1`] = `
edit={[Function]}
list={[Function]}
name="dummy"
options={
Object {
"label": "Dummy",
}
}
show={[Function]}
/>
`;
Expand All @@ -16,6 +21,11 @@ exports[`<ResourceGuesser /> renders with edit 1`] = `
edit={[Function]}
list={[Function]}
name="dummy"
options={
Object {
"label": "Dummy",
}
}
show={[Function]}
/>
`;
Expand All @@ -26,6 +36,11 @@ exports[`<ResourceGuesser /> renders with list 1`] = `
edit={[Function]}
list={[Function]}
name="dummy"
options={
Object {
"label": "Dummy",
}
}
show={[Function]}
/>
`;
Expand All @@ -36,6 +51,11 @@ exports[`<ResourceGuesser /> renders with show 1`] = `
edit={[Function]}
list={[Function]}
name="dummy"
options={
Object {
"label": "Dummy",
}
}
show={[Function]}
/>
`;
Expand All @@ -46,6 +66,11 @@ exports[`<ResourceGuesser /> renders without create 1`] = `
edit={[Function]}
list={[Function]}
name="dummy"
options={
Object {
"label": "Dummy",
}
}
show={[Function]}
/>
`;
Expand All @@ -56,6 +81,11 @@ exports[`<ResourceGuesser /> renders without edit 1`] = `
edit={[Function]}
list={[Function]}
name="dummy"
options={
Object {
"label": "Dummy",
}
}
show={[Function]}
/>
`;
Expand All @@ -66,6 +96,11 @@ exports[`<ResourceGuesser /> renders without list 1`] = `
edit={[Function]}
list={[Function]}
name="dummy"
options={
Object {
"label": "Dummy",
}
}
show={[Function]}
/>
`;
Expand All @@ -76,6 +111,11 @@ exports[`<ResourceGuesser /> renders without show 1`] = `
edit={[Function]}
list={[Function]}
name="dummy"
options={
Object {
"label": "Dummy",
}
}
show={[Function]}
/>
`;
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3669,7 +3669,7 @@ indefinite-observable@^2.0.1:
dependencies:
symbol-observable "1.2.0"

inflection@~1.12.0:
inflection@^1.12.0, inflection@~1.12.0:
version "1.12.0"
resolved "https://registry.yarnpkg.com/inflection/-/inflection-1.12.0.tgz#a200935656d6f5f6bc4dc7502e1aecb703228416"
integrity sha1-ogCTVlbW9fa8TcdQLhrstwMihBY=
Expand Down