Skip to content

Commit

Permalink
[CE-368] Implement smart contract management.
Browse files Browse the repository at this point in the history
User can upload,list,delete smart contract code,
create new smart contract with name,description,version.
When user login at the first time, will copy system default
smart contract into user's smart contract directory.

Change-Id: Id208c36ecc4c588eda4ff262cdac7e4368d45e2f
Signed-off-by: Haitao Yue <hightall@me.com>
  • Loading branch information
hightall committed Jun 20, 2018
1 parent d6475e5 commit 9909115
Show file tree
Hide file tree
Showing 21 changed files with 1,030 additions and 8 deletions.
13 changes: 12 additions & 1 deletion user-dashboard/src/app/assets/src/common/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@ const menuData = [
icon: 'link',
path: 'chain',
},
{
{
name: 'Apply Chain',
path: 'apply-chain',
hideInMenu: true,
hideInBreadcrumb: false,
},
{
name: 'Smart Contract',
path: 'smart-contract',
icon: 'code-o',
},
{
name: 'Create New Smart Contract',
path: 'new-smart-contract',
hideInMenu: true,
hideInBreadcrumb: false,
},
];

function formatter(data, parentPath = '/', parentAuthority) {
Expand Down
6 changes: 6 additions & 0 deletions user-dashboard/src/app/assets/src/common/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ export const getRouterData = app => {
'/user/login': {
component: dynamicWrapper(app, ['login'], () => import('../routes/User/Login')),
},
'/smart-contract': {
component: dynamicWrapper(app, ['smartContract'], () => import('../routes/SmartContract')),
},
'/new-smart-contract': {
component: dynamicWrapper(app, ['smartContract'], () => import('../routes/SmartContract/New')),
},
};
// Get name from ./menu.js or just set it in the router data.
const menuData = getFlatMenuData(getMenuData());
Expand Down
52 changes: 52 additions & 0 deletions user-dashboard/src/app/assets/src/models/smartContract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
SPDX-License-Identifier: Apache-2.0
*/
import { querySmartContracts, deleteSmartContractCode, updateSmartContractCode, deleteSmartContract } from '../services/smart_contract';

export default {
namespace: 'smartContract',

state: {
smartContracts: [],
},

effects: {
*fetch(_, { call, put }) {
const response = yield call(querySmartContracts);
yield put({
type: 'setSmartContracts',
payload: response.data,
})
},
*deleteSmartContractCode({ payload }, { call }) {
yield call(deleteSmartContractCode, payload.id);
if (payload.callback) {
yield call(payload.callback)
}
},
*updateSmartContractCode({ payload }, { call }) {
const response = yield call(updateSmartContractCode, payload);
if (payload.callback) {
yield call(payload.callback, {
payload,
success: response.success,
});
}
},
*deleteSmartContract({ payload }, { call }) {
yield call(deleteSmartContract, payload.id);
if (payload.callback) {
yield call(payload.callback, payload);
}
},
},

reducers: {
setSmartContracts(state, action) {
return {
...state,
smartContracts: action.payload,
};
},
},
};
7 changes: 1 addition & 6 deletions user-dashboard/src/app/assets/src/routes/Chain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export default class CardList extends PureComponent {

render() {
const { chain: { chains }, loading } = this.props;
const imageSrc = `${window.webRoot}static/chain.png`;
const content = (
<div className={styles.pageHeaderContent}>
<p>
Expand All @@ -61,11 +60,7 @@ export default class CardList extends PureComponent {
const extraContent = (
<div className={styles.extraImg}>
<QueueAnim>
<img
key="image-logo"
alt="chain"
src={imageSrc}
/>
<Icon key="smart-contract" type="link" style={{fontSize: 80}} />
</QueueAnim>
</div>
);
Expand Down
223 changes: 223 additions & 0 deletions user-dashboard/src/app/assets/src/routes/SmartContract/New/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
/*
SPDX-License-Identifier: Apache-2.0
*/
import React, { PureComponent } from 'react';
import { Card, Form, Input, Button, Upload, Icon, message } from 'antd';
import { routerRedux } from 'dva/router';
import { connect } from 'dva';
import PropTypes from 'prop-types';
import { injectIntl } from 'react-intl';
import PageHeaderLayout from '../../../layouts/PageHeaderLayout';
import config from '../../../utils/config';

const FormItem = Form.Item;
const { TextArea } = Input;

@connect(({ smartContract }) => ({
smartContract,
}))
@Form.create()
class NewSmartContract extends PureComponent {
static contextTypes = {
routes: PropTypes.array,
params: PropTypes.object,
location: PropTypes.object,
};
state = {
submitting: false,
smartContractCodeId: '',
};
onRemoveFile = () => {
const { smartContractCodeId } = this.state;
this.props.dispatch({
type: 'smartContract/deleteSmartContractCode',
payload: {
id: smartContractCodeId,
callback: this.deleteCallback,
},
})
};
onUploadFile = info => {
if (info.file.status === 'done') {
const { response } = info.file;
if (response.success) {
this.setState({
smartContractCodeId: response.id,
});
} else {
message.error("Upload smart contract file failed");
}
} else if (info.file.status === 'error') {
message.error('Upload smart contract file failed.')
}
};
submitCallback = ({ payload, success }) => {
this.setState({
submitting: false,
});
if (success) {
message.success(`Create smart contract ${payload.name} successfully.`);
this.props.dispatch(
routerRedux.push({
pathname: '/smart-contract',
})
);
} else {
message.error(`Create smart contract ${payload.name} failed.`);
}
};
clickCancel = () => {
const { smartContractCodeId } = this.state;
if (smartContractCodeId !== '') {
this.props.dispatch({
type: 'smartContract/deleteSmartContractCode',
payload: {
id: smartContractCodeId,
},
});
}
this.props.dispatch(
routerRedux.push({
pathname: '/smart-contract',
})
);
};
handleSubmit = e => {
const { smartContractCodeId } = this.state;
e.preventDefault();
this.props.form.validateFieldsAndScroll({ force: true }, (err, values) => {
if (!err) {
this.props.dispatch({
type: 'smartContract/updateSmartContractCode',
payload: {
id: smartContractCodeId,
...values,
callback: this.submitCallback,
},
})
}
});
};
normFile = () => {
return this.state.smartContractCodeId;
};
validateUpload = (rule, value, callback) => {
const { smartContractCodeId } = this.state;
if (smartContractCodeId === '') {
callback('Must upload smart contract zip file');
}
callback();
};
deleteCallback = () => {
this.setState({
smartContractCodeId: '',
});
};
render() {
const { getFieldDecorator } = this.props.form;
const { submitting, smartContractCodeId } = this.state;
const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 7 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 12 },
md: { span: 10 },
},
};

const submitFormLayout = {
wrapperCol: {
xs: { span: 24, offset: 0 },
sm: { span: 10, offset: 7 },
},
};
const uploadProps = {
name: "smart_contract",
accept: '.zip',
action: `${config.url.smartContract.upload}?_csrf=${window.csrf}`,
onChange: this.onUploadFile,
onRemove: this.onRemoveFile,
beforeUpload() {
return smartContractCodeId === '';
},
};

return (
<PageHeaderLayout
title="Create New Smart Contract"
>
<Card bordered={false}>
<Form onSubmit={this.handleSubmit} hideRequiredMark style={{ marginTop: 8 }}>
<FormItem {...formItemLayout} label="Name">
{getFieldDecorator('name', {
initialValue: '',
rules: [
{
required: true,
message: 'Must input name of smart contract',
},
],
})(<Input placeholder="Name" />)}
</FormItem>
<FormItem {...formItemLayout} label="Version">
{getFieldDecorator('version', {
initialValue: '',
rules: [
{
required: true,
message: 'Must input version of smart contract',
},
],
})(<Input placeholder="Version" />)}
</FormItem>
<FormItem {...formItemLayout} label="Description">
{getFieldDecorator('description', {
initialValue: '',
rules: [
{
required: true,
message: 'Must input description of smart contract',
},
],
})(<TextArea placeholder="Description of smart contract" rows={4} />)}
</FormItem>
<FormItem
{...formItemLayout}
label="Upload"
extra="only accept .zip file."
>
{getFieldDecorator('smartContractCode', {
getValueFromEvent: this.normFile,
trigger: 'onBlur',
rules: [
{
validator: this.validateUpload,
},
],
})(
<Upload {...uploadProps}>
<Button disabled={smartContractCodeId !== ''}>
<Icon type="upload" /> Click to upload
</Button>
</Upload>
)}
</FormItem>
<FormItem {...submitFormLayout} style={{ marginTop: 32 }}>
<Button loading={submitting} type="primary" htmlType="submit">
Submit
</Button>
<Button onClick={this.clickCancel} style={{ marginLeft: 8 }}>
Cancel
</Button>
</FormItem>
</Form>
</Card>
</PageHeaderLayout>
);
}
}

export default injectIntl(NewSmartContract);
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@import '~antd/lib/style/themes/default.less';

.upperText {
text-transform: uppercase;
}

.optional {
color: @text-color-secondary;
font-style: normal;
margin-left: 4px;
}
Loading

0 comments on commit 9909115

Please sign in to comment.