index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. import React, {Component} from 'react'
  2. import {withRouter} from 'react-router-dom'
  3. import {message} from 'antd'
  4. import {Checkbox, WhiteSpace, Modal} from 'antd-mobile'
  5. import classNames from 'classnames'
  6. import {Mutation} from "react-apollo"
  7. import gql from "graphql-tag"
  8. import moment from 'moment'
  9. import debounce from 'lodash.debounce'
  10. import '../index.css'
  11. import {delete_userCart_by_id, update_userCart} from "../../../../utils/gql"
  12. const alert = Modal.alert
  13. message.config({
  14. top: '45%',
  15. duration: 2,
  16. maxCount: 2,
  17. })
  18. class CartEdit extends Component {
  19. constructor(props) {
  20. super(props)
  21. this.state = {
  22. isSelectAll:false,
  23. selectedCount:0
  24. }
  25. this.updateCartProductCount = debounce(this.updateCartProductCount, 5000)
  26. }
  27. //获取数据
  28. componentWillMount(){
  29. this.setState({
  30. cartList:this.props.cartList
  31. },()=>{
  32. this.checkedAll('',false)
  33. })
  34. }
  35. // 判断数量update类型
  36. handleChangedCount = (e,type,i,updateCount) => {
  37. e.persist()
  38. let result
  39. switch (type) {
  40. case 'augment':
  41. result = this.augment(e,i)
  42. break
  43. case 'input':
  44. result = this.getInputValue(e,i)
  45. break
  46. case 'reduce':
  47. result = this.reduce(e,i)
  48. break
  49. default:
  50. console.log('handleChangedCount type error')
  51. break
  52. }
  53. if(result.id) {
  54. let {id, count} = result
  55. this.updateCartProductCount(id, count, updateCount)
  56. }
  57. }
  58. // 使用函数防抖5s后请求更新数量
  59. updateCartProductCount = (id,count,updateCount) => {
  60. // console.log('updateCartProductCount id count',id,count)
  61. let updatedAt = moment().format('YYYY-MM-DD HH:mm:ss')
  62. const update = {
  63. id,
  64. count,
  65. updatedAt
  66. }
  67. updateCount({variables:update})
  68. }
  69. //获取输入框的值
  70. getInputValue = (e,i) =>{
  71. let id = '', count = 0
  72. this.setState({
  73. cartList:this.state.cartList.map((item,index)=>{
  74. if(index===i){
  75. item.count = e.target.value
  76. id = item.id
  77. count = item.count
  78. return item
  79. }else {
  80. return item
  81. }
  82. })
  83. })
  84. this.sumCount()
  85. return {
  86. id, count
  87. }
  88. }
  89. // 增加
  90. augment = (e,i) =>{
  91. let id = '', count = 0
  92. this.setState({
  93. cartList:this.state.cartList.map((item,index)=>{
  94. if(index===i){
  95. item.count = item.count*1 + 1
  96. id = item.id
  97. count = item.count
  98. return item
  99. }else {
  100. return item
  101. }
  102. })
  103. })
  104. this.sumCount()
  105. return {
  106. id, count
  107. }
  108. }
  109. // 减少
  110. reduce = (e,i) => {
  111. let id = '', count = 0
  112. this.setState({
  113. cartList:this.state.cartList.map((item,index)=>{
  114. if(index===i){
  115. item.count = item.count*1 - 1
  116. id = item.id
  117. count = item.count
  118. return item
  119. }else {
  120. return item
  121. }
  122. })
  123. })
  124. this.sumCount()
  125. return {
  126. id, count
  127. }
  128. }
  129. // 删除
  130. delete=(delete_userCart_by_id)=>{
  131. let {cartList, selectedCount} = this.state
  132. alert('', `确定要删除这${selectedCount}种商品吗?`, [
  133. { text: '取消', onPress: () => console.log('cancel') },
  134. {
  135. text: '确定',
  136. onPress: () => {
  137. let deleteList = cartList.filter((item)=> item.checked === true)
  138. let cartList1 = cartList.filter((item)=> item.checked === false)
  139. let deleteIdList = deleteList.map(item => item.id)
  140. // console.log('delete list',deleteIdList)
  141. delete_userCart_by_id({variables:{id:deleteIdList}}).then((data)=>{
  142. // console.log('delete data',data)
  143. let num = data.data.delete_userCart.replace(/[^0-9]/ig,"")
  144. if(num){
  145. message.success('删除成功')
  146. let cartCount = JSON.parse(localStorage.getItem("cartCount")) - num
  147. localStorage.setItem("cartCount",JSON.stringify(cartCount))
  148. this.setState({
  149. cartList:cartList1,
  150. selectedCount:0
  151. })
  152. }
  153. })
  154. }
  155. }
  156. ])
  157. }
  158. //删除单个备用
  159. del=(e,i)=> {
  160. this.setState({
  161. cartList:this.state.cartList.filter((item,index)=>{
  162. if(index!==i){
  163. return true
  164. }else {
  165. return false
  166. }
  167. })
  168. })
  169. setTimeout(()=>{
  170. this.sumCount()
  171. },1)
  172. }
  173. // 改变选择
  174. changeCheckedStatus=(e,i)=>{
  175. this.setState({
  176. cartList:this.state.cartList.map((item,index)=>{
  177. if(index===i){
  178. item.checked=! item.checked
  179. }
  180. return item
  181. })
  182. })
  183. let flag = this.state.cartList.every((item,index)=>{
  184. if( item.checked===false) {
  185. return false
  186. }else {
  187. return true
  188. }
  189. })
  190. if(flag===true){
  191. this.setState({isSelectAll:true})
  192. }else {
  193. this.setState({isSelectAll:false})
  194. }
  195. this.sumCount()
  196. }
  197. //全选或全不选,判断全选状态
  198. checkedAll=(e,check)=>{
  199. let checked = e.target ? e.target.checked : check
  200. if(checked===true){
  201. this.setState({
  202. cartList:this.state.cartList.map(( item,index)=>{
  203. item.checked=true
  204. return item
  205. }),
  206. isSelectAll:true
  207. })
  208. }else if(checked===false){
  209. this.setState({
  210. cartList:this.state.cartList.map((item,index)=>{
  211. item.checked=false
  212. return item
  213. }),
  214. isSelectAll:false
  215. })
  216. }
  217. this.sumCount()
  218. }
  219. //计算总合计
  220. sumCount=()=>{
  221. let {cartList} = this.state
  222. let selectedCount=0, cartCount=0
  223. let cartListLength = cartList.length
  224. cartList.forEach((item,index)=>{
  225. cartCount+=item.count
  226. if(item.checked===true){
  227. selectedCount+=item.count
  228. }
  229. if(index === cartListLength - 1){
  230. localStorage.setItem("cartCount",JSON.stringify(cartCount))
  231. this.setState({
  232. selectedCount
  233. })
  234. }
  235. })
  236. }
  237. skipToProductDetail = (e,productId) => {
  238. e.preventDefault()
  239. this.props.history.push({
  240. pathname: '/home/detail',
  241. state: {
  242. id:productId
  243. }
  244. })
  245. }
  246. render() {
  247. let {cartList, isSelectAll, selectedCount} = this.state
  248. let listLength = cartList.length
  249. return (
  250. <Mutation mutation={gql(delete_userCart_by_id)}
  251. onCompleted={()=>{this.props.refetch()}}
  252. onError={error=>console.log('error',error)}
  253. >
  254. {(delete_userCart_by_id,{ loading, error }) => (
  255. <div className="cart-content-wrap">
  256. <div className='cart-content'>
  257. {
  258. cartList.map((item,index)=>{
  259. return(
  260. <div key={item.id+'edit'}>
  261. <div className="cart-list">
  262. <div className="cart-list-checkbox">
  263. <Checkbox
  264. style={{marginLeft:15}}
  265. checked={item.checked}
  266. onChange={(e)=>{this.changeCheckedStatus(e,index)}}
  267. />
  268. </div>
  269. <div className="cart-list-image" onClick={(e)=>this.skipToProductDetail(e,item.product_id.id)}>
  270. <img src={item.product_id.img || "https://gw.alipayobjects.com/zos/rmsportal/nywPmnTAvTmLusPxHPSu.png"} alt=""/>
  271. </div>
  272. <div className="cart-list-intro" onClick={(e)=>this.skipToProductDetail(e,item.product_id.id)}>
  273. <div>{item.product_id.name}</div>
  274. <div>{item.specificationStock_id.color} {item.specificationStock_id.size}</div>
  275. <div className='product-item-price'>
  276. <span>¥{(item.product_id.price*item.product_id.discountRate/100).toFixed(2)}</span>&nbsp;
  277. <span>¥{(item.product_id.price).toFixed(2)}</span>
  278. </div>
  279. </div>
  280. <div className="cart-list-count">
  281. <Mutation mutation={gql(update_userCart)}
  282. onError={error=>console.log('error',error)}
  283. >
  284. {(update_userCart,{ loading, error }) => (
  285. <div className="selected">
  286. <button
  287. className={classNames({
  288. 'selected_button-white': true,
  289. 'selected_button-disabled': item.count <= 1
  290. })}
  291. // disabled={item.count <= 1}
  292. onClick={(e)=>{
  293. if(item.count > 1){
  294. this.handleChangedCount(e,'reduce',index,update_userCart)
  295. }else {
  296. message.warning('数量不能小于1个')
  297. }
  298. }}
  299. >-</button>
  300. <input className="selected_input" type="text"
  301. min={1} step={1} max={item.specificationStock_id.stock}
  302. value={item.count}
  303. onChange={(e)=>{this.handleChangedCount(e,'input',index,update_userCart)}}
  304. />
  305. <button className="selected_button-white" onClick={(e)=>{this.handleChangedCount(e,'augment',index,update_userCart)}}>+</button>
  306. </div>
  307. )}
  308. </Mutation>
  309. </div>
  310. </div>
  311. <WhiteSpace size="md" />
  312. </div>
  313. )
  314. })
  315. }
  316. </div>
  317. {
  318. listLength ?
  319. <div className="footer">
  320. <div className="jiesuan">
  321. <div className="jiesuan-checkbox">
  322. <Checkbox
  323. checked={isSelectAll}
  324. onChange={(e)=>{this.checkedAll(e,'')}}
  325. style={{marginLeft:15}}
  326. />
  327. <span className="jiesuan-checkbox_label">全选</span>
  328. </div>
  329. <div className="jiesuan-total">
  330. </div>
  331. <button
  332. className={classNames({
  333. 'jiesuan-button': true,
  334. 'jiesuan-disabled': !selectedCount
  335. })}
  336. onClick={()=>{
  337. if(selectedCount){
  338. this.delete(delete_userCart_by_id)
  339. }else {
  340. message.warning('请选择商品!')
  341. }
  342. }}
  343. >
  344. <span>删除({selectedCount})</span>
  345. </button>
  346. </div>
  347. </div>:''
  348. }
  349. </div>
  350. )}
  351. </Mutation>
  352. )
  353. }
  354. }
  355. export default withRouter(CartEdit)