-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 29a2b8e
Showing
9 changed files
with
432 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"presets": ["es2015"], | ||
"plugins": ["transform-object-rest-spread"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
lib | ||
node_modules | ||
.npm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Carlos Almeida | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# Loopback Client for react-admin | ||
For using [Loopback 3](https://loopback.io/) with [react-admin](https://github.com/marmelab/react-admin). | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install --save react-admin-loopback | ||
``` | ||
|
||
## Usage | ||
|
||
```js | ||
// in src/App.js | ||
import React from 'react'; | ||
import { Admin, Resource } from 'react-admin'; | ||
import loopbackClient, { authProvider } from 'react-admin-loopback'; | ||
import { List, Datagrid, TextField, NumberField } from 'react-admin'; | ||
|
||
import { ShowButton, EditButton, Edit, SimpleForm, DisabledInput, TextInput, NumberInput } from 'react-admin'; | ||
import { Create} from 'react-admin'; | ||
import { Show, SimpleShowLayout } from 'react-admin'; | ||
|
||
const BookList = (props) => ( | ||
<List {...props}> | ||
<Datagrid> | ||
<ShowButton /> | ||
<EditButton /> | ||
<TextField source="author" /> | ||
<NumberField source="count" /> | ||
</Datagrid> | ||
</List> | ||
); | ||
export const BookShow = (props) => ( | ||
<Show {...props}> | ||
<SimpleShowLayout> | ||
<TextField source="author" /> | ||
<NumberField source="count" /> | ||
</SimpleShowLayout> | ||
</Show> | ||
); | ||
export const BookEdit = (props) => ( | ||
<Edit {...props}> | ||
<SimpleForm> | ||
<DisabledInput source="id" /> | ||
<TextInput source="author" /> | ||
<NumberInput source="count" /> | ||
</SimpleForm> | ||
</Edit> | ||
); | ||
export const BookCreate = (props) => ( | ||
<Create {...props}> | ||
<SimpleForm> | ||
<TextInput source="author" /> | ||
<NumberInput source="count" /> | ||
</SimpleForm> | ||
</Create> | ||
); | ||
const App = () => ( | ||
<Admin dataProvider={loopbackClient('http://localhost:3000')} authProvider={authProvider('http://localhost:3000/users/login')}> | ||
<Resource name="books" show={BookShow} create={BookCreate} edit={BookEdit} list={BookList} /> | ||
</Admin> | ||
); | ||
|
||
export default App; | ||
``` | ||
|
||
The dataProvider supports include: | ||
|
||
```js | ||
// dataProvider.js | ||
import loopbackProvider from 'react-admin-loopback'; | ||
|
||
const dataProvider = loopbackProvider('http://localhost:3000'); | ||
export default (type, resource, params) => | ||
new Promise(resolve => | ||
setTimeout(() => resolve(dataProvider(type, resource, params)), 500) | ||
); | ||
``` | ||
|
||
```js | ||
|
||
import dataProvider from './dataProvider'; | ||
|
||
... | ||
|
||
dataProvider(GET_LIST, 'books', { | ||
filter: { hide: false }, | ||
sort: { field: 'id', order: 'DESC' }, | ||
pagination: { page: 1, perPage: 0 }, | ||
include: 'users' | ||
}).then(response => { | ||
... | ||
}); | ||
|
||
... | ||
|
||
``` | ||
|
||
## License | ||
|
||
This library is licensed under the [MIT Licence](LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "react-admin-loopback", | ||
"version": "1.0.0", | ||
"description": "Packages related to using Loopback with react-admin", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "babel ./src -d lib --ignore '*.spec.js' --presets babel-preset-es2015" | ||
}, | ||
"files": [ | ||
"*.md", | ||
"lib", | ||
"src" | ||
], | ||
"devDependencies": { | ||
"babel-cli": "^6.23.0", | ||
"babel-core": "^6.23.1", | ||
"babel-plugin-transform-object-rest-spread": "^6.23.0", | ||
"babel-preset-es2015": "^6.22.0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/darthwesker/react-admin-loopback.git" | ||
}, | ||
"keywords": [ | ||
"react", | ||
"react-admin", | ||
"loopback", | ||
"rest-client" | ||
], | ||
"author": "Carlos Almeida", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/darthwesker/react-admin-loopback/issues" | ||
}, | ||
"homepage": "https://github.com/darthwesker/react-admin-loopback#readme" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { AUTH_LOGIN, AUTH_LOGOUT, AUTH_CHECK, AUTH_ERROR } from 'react-admin'; | ||
import storage from './storage'; | ||
|
||
export const authProvider = (loginApiUrl, noAccessPage = '/login') => { | ||
return (type, params) => { | ||
if (type === AUTH_LOGIN) { | ||
const request = new Request(loginApiUrl, { | ||
method: 'POST', | ||
body: JSON.stringify(params), | ||
headers: new Headers({ 'Content-Type': 'application/json' }), | ||
}); | ||
return fetch(request) | ||
.then(response => { | ||
if (response.status < 200 || response.status >= 300) { | ||
throw new Error(response.statusText); | ||
} | ||
return response.json(); | ||
}) | ||
.then(({ ttl, ...data }) => { | ||
storage.save('lbtoken', data, ttl); | ||
}); | ||
} | ||
if (type === AUTH_LOGOUT) { | ||
storage.remove('lbtoken'); | ||
return Promise.resolve(); | ||
} | ||
if (type === AUTH_ERROR) { | ||
const { status } = params; | ||
if (status === 401 || status === 403) { | ||
storage.remove('lbtoken'); | ||
return Promise.reject(); | ||
} | ||
return Promise.resolve(); | ||
} | ||
if (type === AUTH_CHECK) { | ||
const token = storage.load('lbtoken'); | ||
if (token && token.id) { | ||
return Promise.resolve(); | ||
} else { | ||
storage.remove('lbtoken'); | ||
return Promise.reject({ redirectTo: noAccessPage }); | ||
} | ||
} | ||
return Promise.reject('Unkown method'); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { fetchUtils } from 'react-admin'; | ||
import storage from './storage'; | ||
|
||
export default (url, options = {}) => { | ||
options.user = { | ||
authenticated: true, | ||
token: storage.load('lbtoken').id | ||
} | ||
return fetchUtils.fetchJson(url, options); | ||
} |
Oops, something went wrong.