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 :