Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Commit

Permalink
[WIP] Use admin-on-rest
Browse files Browse the repository at this point in the history
  • Loading branch information
djhi committed Sep 7, 2016
1 parent 52d8eff commit c775f1e
Show file tree
Hide file tree
Showing 19 changed files with 262 additions and 193 deletions.
2 changes: 1 addition & 1 deletion config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
appName: 'New App',
apps: {
admin: {
api_url: `${apiUrl}/admin/`,
api_url: `${apiUrl}/admin`,
},
api: {
allowedOrigins,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"node": ">= 6.4.0"
},
"dependencies": {
"admin-on-rest": "0.2.0",
"babel-plugin-transform-runtime": "6.12.0",
"babel-polyfill": "6.13.0",
"babel-register": "6.11.6",
Expand Down
1 change: 0 additions & 1 deletion src/admin/css/main.scss

This file was deleted.

4 changes: 2 additions & 2 deletions src/admin/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body ng-app="myApp" ng-strict-di>
<div ui-view></div>
<body>
<div id="root"></div>
</body>
</html>
11 changes: 0 additions & 11 deletions src/admin/js/header.html

This file was deleted.

2 changes: 1 addition & 1 deletion src/admin/js/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ document.getElementById('loginForm').addEventListener('submit', (event) => {

const req = new XMLHttpRequest();
req.withCredentials = true;
req.open('POST', `${ADMIN_API_URL}authenticate`, true);
req.open('POST', `${ADMIN_API_URL}/authenticate`, true);
req.setRequestHeader('Content-Type', 'application/json');

req.onload = () => {
Expand Down
67 changes: 18 additions & 49 deletions src/admin/js/main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/* global angular APP_NAME ADMIN_API_URL */
import 'ng-admin';
import productsConfig from './products/config';
import ordersConfig from './orders/config';
import headerTemplate from './header.html';
require('ng-admin/build/ng-admin.min.css');
/* eslint max-len: off */
import React from 'react';
import { render } from 'react-dom';
import { Admin, Resource } from 'admin-on-rest';

import restClient from './restClient';
import { ProductList, ProductEdit, ProductCreate, ProductIcon } from './products';
import { OrderList, OrderEdit, OrderCreate, OrderIcon } from './orders';

function redirectToLogin() {
window.location = '/admin/login.html';
Expand All @@ -17,51 +20,17 @@ function logout() {
redirectToLogin();
}

window.logout = logout;

if (!window.localStorage.getItem('token')) redirectToLogin();

const myApp = angular.module('myApp', ['ng-admin']);

myApp.config(['NgAdminConfigurationProvider', (nga) => {
const admin = nga
.application(APP_NAME)
.baseApiUrl(ADMIN_API_URL);

admin.addEntity(nga.entity('products'));
admin.addEntity(nga.entity('orders'));

productsConfig(nga, admin);
ordersConfig(nga, admin);

admin.menu(nga.menu()
.addChild(nga
.menu(admin.getEntity('products'))
.icon('<span class="fa fa-picture-o fa-fw"></span>')
)
.addChild(nga
.menu(admin.getEntity('orders'))
.icon('<span class="fa fa-credit-card fa-fw"></span>')
)
);

admin.dashboard(nga.dashboard());
admin.header(headerTemplate);

nga.configure(admin);
}]);

myApp.config(['RestangularProvider', (RestangularProvider) => {
RestangularProvider.setDefaultHttpFields({ withCredentials: true });
RestangularProvider.addFullRequestInterceptor((element, operation, what, url, headers) => {
const currentTime = (new Date()).getTime();
const tokenExpires = window.localStorage.getItem('expires');
const finalHeaders = headers || {};

if (tokenExpires && tokenExpires < currentTime) logout();
const currentTime = (new Date()).getTime();
const tokenExpires = window.localStorage.getItem('expires');

finalHeaders.Authorization = window.localStorage.getItem('token');
if (tokenExpires && tokenExpires < currentTime) logout();

return { headers: finalHeaders };
});
}]);
render(
<Admin restClient={restClient(ADMIN_API_URL, () => window.localStorage.getItem('token'), logout)}>
<Resource name="products" list={ProductList} edit={ProductEdit} create={ProductCreate} icon={ProductIcon} />
<Resource name="orders" list={OrderList} edit={OrderEdit} create={OrderCreate} icon={OrderIcon} />
</Admin>,
document.getElementById('root')
);
10 changes: 10 additions & 0 deletions src/admin/js/orders/Create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import { Create, DateInput, TextInput } from 'admin-on-rest/lib/mui';

export default (props) => (
<Create title="Create an Order" {...props}>
<DateInput label="date" source="date" />
<TextInput label="total" source="total" options={{ type: 'number' }} />
<TextInput label="status" source="status" />
</Create>
);
16 changes: 16 additions & 0 deletions src/admin/js/orders/Edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React, { PropTypes } from 'react';
import { Edit, DateInput, TextInput } from 'admin-on-rest/lib/mui';

const Title = ({ record }) => <span>Order {record ? `"${record.reference}"` : ''}</span>;

Title.propTypes = {
record: PropTypes.object.isRequired,
};

export default (props) => (
<Edit title={Title} {...props}>
<DateInput label="date" source="date" />
<TextInput label="total" source="total" options={{ type: 'number' }} />
<TextInput label="status" source="status" />
</Edit>
);
18 changes: 18 additions & 0 deletions src/admin/js/orders/List.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';
import { Filter, List, DateField, TextField, EditButton, TextInput } from 'admin-on-rest/lib/mui';

export const OrderFilter = (props) => (
<Filter {...props}>
<TextInput label="Search" source="reference" alwaysOn />
</Filter>
);

export default (props) => (
<List {...props} filter={OrderFilter}>
<DateField label="date" source="date" />
<TextField label="reference" source="reference" />
<TextField label="total" source="total" />
<TextField label="status" source="status" />
<EditButton basePath="/orders" />
</List>
);
61 changes: 0 additions & 61 deletions src/admin/js/orders/config.js

This file was deleted.

11 changes: 11 additions & 0 deletions src/admin/js/orders/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import OrderIcon from 'material-ui/svg-icons/action/description';
import OrderList from './List';
import OrderEdit from './Edit';
import OrderCreate from './Create';

export default {
OrderList,
OrderEdit,
OrderCreate,
OrderIcon,
};
12 changes: 12 additions & 0 deletions src/admin/js/products/Create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import { Create, TextInput } from 'admin-on-rest/lib/mui';

export default (props) => (
<Create title="Create a Post" {...props}>
<TextInput label="Reference" source="reference" />
<TextInput label="price" source="price" options={{ type: 'number' }} />
<TextInput label="width" source="width" options={{ type: 'number' }} />
<TextInput label="height" source="height" options={{ type: 'number' }} />
<TextInput label="stock" source="stock" options={{ type: 'number' }} />
</Create>
);
18 changes: 18 additions & 0 deletions src/admin/js/products/Edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React, { PropTypes } from 'react';
import { Edit, TextInput } from 'admin-on-rest/lib/mui';

const Title = ({ record }) => <span>{record ? record.reference : ''}</span>;

Title.propTypes = {
record: PropTypes.object.isRequired,
};

export default (props) => (
<Edit title={Title} {...props}>
<TextInput label="Reference" source="reference" />
<TextInput label="price" source="price" options={{ type: 'number' }} />
<TextInput label="width" source="width" options={{ type: 'number' }} />
<TextInput label="height" source="height" options={{ type: 'number' }} />
<TextInput label="stock" source="stock" options={{ type: 'number' }} />
</Edit>
);
19 changes: 19 additions & 0 deletions src/admin/js/products/List.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import { Filter, List, TextField, EditButton, TextInput } from 'admin-on-rest/lib/mui';

export const ProductFilter = (props) => (
<Filter {...props}>
<TextInput label="Search" source="reference" alwaysOn />
</Filter>
);

export default (props) => (
<List {...props} filter={ProductFilter}>
<TextField label="reference" source="reference" />
<TextField label="price" source="price" />
<TextField label="width" source="width" />
<TextField label="height" source="height" />
<TextField label="stock" source="stock" />
<EditButton basePath="/products" />
</List>
);
66 changes: 0 additions & 66 deletions src/admin/js/products/config.js

This file was deleted.

11 changes: 11 additions & 0 deletions src/admin/js/products/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import ProductIcon from 'material-ui/svg-icons/action/redeem';
import ProductList from './List';
import ProductEdit from './Edit';
import ProductCreate from './Create';

export default {
ProductList,
ProductEdit,
ProductCreate,
ProductIcon,
};
Loading

0 comments on commit c775f1e

Please sign in to comment.