Skip to content

Commit b97e713

Browse files
GoZOodunglas
authored andcommitted
Update getting-started documentation (#366)
1 parent 6733dfe commit b97e713

File tree

1 file changed

+51
-20
lines changed

1 file changed

+51
-20
lines changed

admin/getting-started.md

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Then, create a new React application for your admin:
1111

1212
$ create-react-app my-admin
1313

14+
Now, go to the newly created `my-admin` directory:
15+
16+
$ cd my-admin
17+
1418
React and React DOM will be directly provided as dependencies of Admin On REST. As having different versions of React
1519
causes issues, remove `react` and `react-dom` from the `dependencies` section of the generated `package.json` file:
1620

@@ -34,6 +38,12 @@ import { HydraAdmin } from '@api-platform/admin';
3438
export default () => <HydraAdmin entrypoint="https://demo.api-platform.com"/>; // Replace with your own API entrypoint
3539
```
3640

41+
Be sure to make your API send proper [CORS HTTP headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) to allow the admin's domain to access it. To do so, update the value of the `cors_allow_origin` parameter in `app/config/parameters.yml` (it will be `http://localhost:3000` by default).
42+
43+
Clear the cache to apply this change:
44+
45+
$ docker-compose exec app bin/console cache:clear --env=prod
46+
3747
Your new administration interface is ready! Type `yarn start` to try it!
3848

3949
Note: if you don't want to hardcode the API URL, you can [use an environment variable](https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#adding-custom-environment-variables).
@@ -51,27 +61,31 @@ In the following example, we change components used for the `description` proper
5161
import React from 'react';
5262
import { RichTextField } from 'admin-on-rest';
5363
import RichTextInput from 'aor-rich-text-input';
54-
import HydraAdmin from 'api-platform-admin/lib/hydra/HydraAdmin';
55-
import parseHydraDocumentation from 'api-doc-parser/lib/hydra/parseHydraDocumentation';
64+
import { HydraAdmin } from '@api-platform/admin';
65+
import parseHydraDocumentation from '@api-platform/api-doc-parser/lib/hydra/parseHydraDocumentation';
5666

5767
const entrypoint = 'https://demo.api-platform.com';
5868

59-
const apiDocumentationParser = entrypoint => parseHydraDocumentation(entrypoint)
60-
.then(api => {
61-
api.resources.map(resource => {
62-
const books = api.resources.find(r => 'books' === r.name);
63-
books.fields.find(f => 'description' === f.name).fieldComponent = <RichTextField source="description" key="description"/>;
64-
books.fields.find(f => 'description' === f.name).inputComponent = <RichTextInput source="description" key="description"/>;
69+
const myApiDocumentationParser = entrypoint => parseHydraDocumentation(entrypoint)
70+
.then( ({ api }) => {
71+
const books = api.resources.find(({ name }) => 'books' === name);
72+
const description = books.fields.find(f => 'description' === f.name);
6573

66-
return resource;
67-
});
74+
description.input = props => (
75+
<RichTextInput {...props} source="description" />
76+
);
6877

69-
return api;
78+
description.input.defaultProps = {
79+
addField: true,
80+
addLabel: true
81+
};
82+
83+
return { api };
7084
})
7185
;
7286

7387
export default (props) => (
74-
<HydraAdmin apiDocumentationParser={apiDocumentationParser} entrypoint={entrypoint}/>
88+
<HydraAdmin apiDocumentationParser={myApiDocumentationParser} entrypoint={entrypoint}/>
7589
);
7690
```
7791

@@ -92,13 +106,28 @@ In the following example, we will:
92106
```javascript
93107
import { FunctionField, ImageField, ImageInput } from 'admin-on-rest/lib/mui';
94108
import React from 'react';
95-
import HydraAdmin from 'api-platform-admin/lib/hydra/HydraAdmin';
96-
import parseHydraDocumentation from 'api-doc-parser/lib/hydra/parseHydraDocumentation';
109+
import { RichTextField } from 'admin-on-rest';
110+
import RichTextInput from 'aor-rich-text-input';
111+
import { HydraAdmin } from '@api-platform/admin';
112+
import parseHydraDocumentation from '@api-platform/api-doc-parser/lib/hydra/parseHydraDocumentation';
97113

98114
const entrypoint = 'https://demo.api-platform.com';
99115

100-
const apiDocumentationParser = entrypoint => parseHydraDocumentation(entrypoint)
101-
.then(api => {
116+
const myApiDocumentationParser = entrypoint => parseHydraDocumentation(entrypoint)
117+
.then( ({ api }) => {
118+
119+
const books = api.resources.find(({ name }) => 'books' === name);
120+
const description = books.fields.find(f => 'description' === f.name);
121+
122+
description.input = props => (
123+
<RichTextInput {...props} source="description" />
124+
);
125+
126+
description.input.defaultProps = {
127+
addField: true,
128+
addLabel: true,
129+
};
130+
102131
api.resources.map(resource => {
103132
if ('http://schema.org/ImageObject' === resource.id) {
104133
resource.fields.map(field => {
@@ -145,12 +174,12 @@ const apiDocumentationParser = entrypoint => parseHydraDocumentation(entrypoint)
145174
return resource;
146175
});
147176

148-
return api;
177+
return { api };
149178
})
150179
;
151180

152181
export default (props) => (
153-
<HydraAdmin apiDocumentationParser={apiDocumentationParser} entrypoint={entrypoint}/>
182+
<HydraAdmin apiDocumentationParser={myApiDocumentationParser} entrypoint={entrypoint} />
154183
);
155184
```
156185

@@ -172,15 +201,17 @@ export default class extends Component {
172201
state = {api: null};
173202

174203
componentDidMount() {
175-
parseHydraDocumentation(entrypoint).then(api => {
204+
parseHydraDocumentation(entrypoint).then( ({ api }) => => {
176205
const books = api.resources.find(r => 'books' === r.name);
177206

178207
books.writableFields.find(f => 'description' === f.name).inputProps = {
179208
validate: value => value.length >= 30 ? undefined : 'Minimum length: 30';
180209
};
181210

182211
this.setState({api: api});
183-
})
212+
213+
return { api };
214+
});
184215
}
185216

186217
render() {

0 commit comments

Comments
 (0)