import React, {Component} from 'react' import {message} from 'antd' import {Checkbox, WhiteSpace, Modal, Toast} from 'antd-mobile' import classNames from 'classnames' import {Mutation} from "react-apollo" import gql from "graphql-tag" import '../index.css' import {delete_userCart_by_id} from "../../../../utils/gql" const alert = Modal.alert message.config({ top: '45%', duration: 2, maxCount: 2, }) class CartEdit extends Component { constructor(props) { super(props) this.state = { isSelectAll:false, selectedCount:0 } } //获取数据 componentWillMount(){ this.setState({ cartList:this.props.cartList },()=>{ this.checkedAll('',false) }) } //获取输入框的值 getInputValue=(e,i)=>{ this.setState({ cartList:this.state.cartList.map((item,index)=>{ if(index===i){ item.count=e.target.value return item }else { return item } }) }) this.sumCount() } // 增加 augment=(e,i)=>{ this.setState({ cartList:this.state.cartList.map((item,index)=>{ if(index===i){ item.count=item.count*1+1 return item }else { return item } }) }) this.sumCount() } // 减少 reduce=(e,i)=> { this.setState({ cartList:this.state.cartList.map((item,index)=>{ if(index===i){ item.count=item.count*1-1 return item }else { return item } }) }) this.sumCount() } // 删除 delete=(delete_userCart_by_id)=>{ let {cartList, selectedCount} = this.state alert('', `确定要删除这${selectedCount}种商品吗?`, [ { text: '取消', onPress: () => console.log('cancel') }, { text: '确定', onPress: () => { let deleteList = cartList.filter((item)=> item.checked === true) let cartList1 = cartList.filter((item)=> item.checked === false) let deleteIdList = deleteList.map(item => item.id) // console.log('delete list',deleteIdList) delete_userCart_by_id({variables:{id:deleteIdList}}).then((data)=>{ // console.log('delete data',data) let num = data.data.delete_userCart.replace(/[^0-9]/ig,"") if(num){ Toast.info('删除成功', 1) let cartCount = JSON.parse(localStorage.getItem("cartCount")) - num localStorage.setItem("cartCount",JSON.stringify(cartCount)) this.setState({ cartList:cartList1, selectedCount:0 }) } }) } } ]) } //删除单个备用 del=(e,i)=> { this.setState({ cartList:this.state.cartList.filter((item,index)=>{ if(index!==i){ return true }else { return false } }) }) setTimeout(()=>{ this.sumCount() },1) } // 改变选择 changeCheckedStatus=(e,i)=>{ this.setState({ cartList:this.state.cartList.map((item,index)=>{ if(index===i){ item.checked=! item.checked } return item }) }) let flag = this.state.cartList.every((item,index)=>{ if( item.checked===false) { return false }else { return true } }) if(flag===true){ this.setState({isSelectAll:true}) }else { this.setState({isSelectAll:false}) } this.sumCount() } //全选或全不选,判断全选状态 checkedAll=(e,check)=>{ let checked = e.target ? e.target.checked : check if(checked===true){ this.setState({ cartList:this.state.cartList.map(( item,index)=>{ item.checked=true return item }), isSelectAll:true }) }else if(checked===false){ this.setState({ cartList:this.state.cartList.map((item,index)=>{ item.checked=false return item }), isSelectAll:false }) } this.sumCount() } //计算总合计 sumCount=()=>{ let selectedCount=0 this.state.cartList.forEach((item,index)=>{ if(item.checked===true){ selectedCount+=item.count } }) this.setState({ selectedCount }) } render() { let {cartList, isSelectAll, selectedCount} = this.state let listLength = cartList.length return ( {this.props.refetch()}} onError={error=>console.log('error',error)} > {(delete_userCart_by_id,{ loading, error }) => (
{ cartList.map((item,index)=>{ return(
{this.changeCheckedStatus(e,index)}} />
{item.product_id.name}
{item.specificationStock_id.color} {item.specificationStock_id.size}
¥ {item.product_id.price}
{this.getInputValue(e,index)}}/>
) }) }
{ listLength ?
{this.checkedAll(e,'')}} style={{marginLeft:15}} /> 全选
:'' }
)}
) } } export default CartEdit