From 89c08bbf4f078adf92d4ad2cb3b9132e9df7a0ef Mon Sep 17 00:00:00 2001 From: shen100 Date: Mon, 15 May 2017 23:33:21 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E8=BE=91=E4=BA=A7=E5=93=81=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E4=B8=8E=E5=90=8E=E5=8F=B0=E6=8E=A5=E5=8F=A3=E5=AF=B9?= =?UTF-8?q?=E6=8E=A5=E5=AE=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../actions/product/requestSaveProduct.js | 44 +++++++++++ .../javascripts/admin/constants/index.js | 4 + .../admin/containers/product/EditProduct.js | 74 +++++++++++++------ .../javascripts/admin/reducers/product.js | 9 ++- 4 files changed, 109 insertions(+), 22 deletions(-) create mode 100644 nodejs/static/javascripts/admin/actions/product/requestSaveProduct.js diff --git a/nodejs/static/javascripts/admin/actions/product/requestSaveProduct.js b/nodejs/static/javascripts/admin/actions/product/requestSaveProduct.js new file mode 100644 index 00000000..316f8553 --- /dev/null +++ b/nodejs/static/javascripts/admin/actions/product/requestSaveProduct.js @@ -0,0 +1,44 @@ +import { + REQUEST_SAVE_PRODUCT, + REQUEST_SAVE_PRODUCT_SUCCESS, +} from '../../constants'; + +function receive(data) { + return { + type : REQUEST_SAVE_PRODUCT_SUCCESS, + product : data.product + }; +} + +export default function(product) { + return dispatch => { + var url = pageConfig.apiPath + '/admin/product/update'; + var categories = []; + for (var i = 0; i < product.categories.length; i++) { + categories.push({ + id: parseInt(product.categories[i].split('-')[1]) + }); + } + var reqData ={ + id : product.id, + name : product.name, + categories : categories, + status : parseInt(product.status), + originalPrice : product.originalPrice, + price : product.price, + remark : product.remark, + detail : product.detail + }; + return fetch(url, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + body: JSON.stringify(reqData) + }) + .then(response => response.json()) + .then(json => dispatch(receive(json.data))); + }; +}; + diff --git a/nodejs/static/javascripts/admin/constants/index.js b/nodejs/static/javascripts/admin/constants/index.js index 94d5517b..b906e110 100644 --- a/nodejs/static/javascripts/admin/constants/index.js +++ b/nodejs/static/javascripts/admin/constants/index.js @@ -20,6 +20,10 @@ export const REQUEST_HOT_PRODUCT_LIST = 'requestHotProductList'; export const REQUEST_PRODUCT_LIST = 'requestProductList'; +export const REQUEST_SAVE_PRODUCT = 'requestSaveProduct'; + +export const REQUEST_SAVE_PRODUCT_SUCCESS = 'requestSaveProductSuccess'; + export const CHANGE_PRODUCT_STATUS = 'changeProductStatus'; export const REQUEST_CATEGORY_LIST = 'requestCategoryList'; diff --git a/nodejs/static/javascripts/admin/containers/product/EditProduct.js b/nodejs/static/javascripts/admin/containers/product/EditProduct.js index 795d7590..061d9b08 100644 --- a/nodejs/static/javascripts/admin/containers/product/EditProduct.js +++ b/nodejs/static/javascripts/admin/containers/product/EditProduct.js @@ -13,6 +13,7 @@ import { } from 'antd'; import requestProduct from '../../actions/product/requestProduct'; +import requestSaveProduct from '../../actions/product/requestSaveProduct'; import requestCategoryList from '../../actions/category/requestCategoryList'; import Software from '../Software'; import utils from '../../utils'; @@ -25,9 +26,17 @@ import '../../../../styles/admin/product/editProduct.css'; class EditProduct extends Component { constructor(props) { super(props); + this.onNameBlur = this.onNameBlur.bind(this); + this.onCategoriesChange = this.onCategoriesChange.bind(this); + this.onOriginalPriceBlur = this.onOriginalPriceBlur.bind(this); + this.onPriceBlur = this.onPriceBlur.bind(this); + this.onRemarkBlur = this.onRemarkBlur.bind(this); + this.onStatusChange = this.onStatusChange.bind(this); + this.onSubmit = this.onSubmit.bind(this); + this.state = { productId : this.props.routeParams.id, - parents : [], //父分类 + categories : [], //产品所属的分类 name : '', detail : '', originalPrice : 0, @@ -85,15 +94,15 @@ class EditProduct extends Component { componentWillReceiveProps(nextProps) { var product = nextProps.data.product; if (product) { - var parents = []; + var categories = []; for (var i = 0; i < product.categories.length; i++) { var parentId = product.categories[i].parentId; var id = product.categories[i].id; - parents.push(parentId + '-' + id); + categories.push(parentId + '-' + id); } this.setState({ productId : product.id, - parents : parents, //父分类 + categories : categories, name : product.name, detail : product.detail, originalPrice : product.originalPrice, @@ -104,20 +113,43 @@ class EditProduct extends Component { this.loadUEditor(); } } - onNameChange() { - this.refs.nameInput; + onNameBlur(event) { + this.setState({ name: event.target.value }); } - onParentsChange(value) { - this.setState({ parents: value }); + onCategoriesChange(value) { + this.setState({ categories: value }); } - onOriginalPriceChange(price) { + onOriginalPriceBlur(event) { + var price = event.target.value; + price = Number(price); this.setState({ originalPrice: price }); } - onPriceChange(price) { + onPriceBlur(event) { + var price = event.target.value; + price = Number(price); this.setState({ price: price }); } - onStatusChange(stauts) { - this.setState({ stauts: stauts }); + onRemarkBlur(event) { + this.setState({ remark: event.target.value }); + } + onStatusChange(status) { + this.setState({ status: status }); + } + onSubmit() { + if (!this.state.ueditor) { + return; + } + const { dispatch } = this.props; + dispatch(requestSaveProduct({ + id : this.state.productId, + name : this.state.name, + categories : this.state.categories, + status : this.state.status, + originalPrice : this.state.originalPrice, + price : this.state.price, + remark : this.state.remark, + detail : this.state.ueditor.getContent() + })); } render() { let { data } = this.props; @@ -158,8 +190,8 @@ class EditProduct extends Component { const treeProps = { treeData, - value : this.state.parents, - onChange : this.onParentsChange.bind(this), + value : this.state.categories, + onChange : this.onCategoriesChange, multiple : true, treeCheckable: true, showCheckedStrategy: TreeSelect.SHOW_PARENT, @@ -179,12 +211,12 @@ class EditProduct extends Component { isLoading ? null :
- + - + - + - + - + - +
@@ -210,7 +242,7 @@ class EditProduct extends Component {
- + diff --git a/nodejs/static/javascripts/admin/reducers/product.js b/nodejs/static/javascripts/admin/reducers/product.js index 5035fb98..cc08a12c 100644 --- a/nodejs/static/javascripts/admin/reducers/product.js +++ b/nodejs/static/javascripts/admin/reducers/product.js @@ -5,7 +5,8 @@ import { REQUEST_PRODUCT, REQUEST_PRODUCT_SUCCESS, REQUEST_CATEGORY_LIST, - REQUEST_CATEGORY_LIST_SUCCESS + REQUEST_CATEGORY_LIST_SUCCESS, + REQUEST_SAVE_PRODUCT_SUCCESS } from '../constants'; let initState = { @@ -54,6 +55,12 @@ export default (state = initState, action) => { product: action.product }; } + case REQUEST_SAVE_PRODUCT_SUCCESS: { + return { + ...state, + product: action.product + }; + } case REQUEST_CATEGORY_LIST: { return { ...state,