index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import React, {Component} from 'react'
  2. import {NavBar, Icon, ActivityIndicator, Grid} from 'antd-mobile'
  3. import {Input} from 'antd'
  4. import './index.css'
  5. import {productbyprops} from "../../../utils/gql"
  6. import {Query} from "react-apollo"
  7. import gql from "graphql-tag"
  8. import {withRouter} from 'react-router-dom'
  9. const Search = Input.Search
  10. class Kind extends Component {
  11. constructor(props) {
  12. super(props)
  13. this.state = {
  14. id: ''
  15. }
  16. }
  17. componentWillMount() {
  18. let {location} = this.props
  19. if(location && location.state) {
  20. this.setState({
  21. id: location.state.id
  22. })
  23. }
  24. }
  25. render() {
  26. let {id} = this.state
  27. let contentHeight = window.innerHeight - 105
  28. return (
  29. <div className='kind-wrap' style={{height: contentHeight}}>
  30. <div className='kind-navbar-wrap'>
  31. <NavBar
  32. className='kind-navbar'
  33. mode="light"
  34. icon={<Icon type="left"/>}
  35. onLeftClick={() => {this.props.history.push({pathname: '/'})}}
  36. >商品分类</NavBar>
  37. </div>
  38. <div className='kind-search-wrap'>
  39. <Search
  40. className='kind-search'
  41. placeholder="请输入搜索内容"
  42. onSearch={value => console.log(value)}
  43. />
  44. </div>
  45. <Query query={gql(productbyprops)} variables={{category: id}}>
  46. {
  47. ({loading, error, data}) => {
  48. if (loading) {
  49. return (
  50. <div className="loading-center">
  51. <ActivityIndicator text="Loading..." size="large"/>
  52. </div>
  53. )
  54. }
  55. if (error) {
  56. return 'error!'
  57. }
  58. return (
  59. <KindRender
  60. data={data.productbyprops}
  61. history={this.props.history}
  62. />
  63. )
  64. }
  65. }
  66. </Query>
  67. </div>
  68. )
  69. }
  70. }
  71. export default withRouter(Kind)
  72. class KindRender extends Component {
  73. constructor(props) {
  74. super(props)
  75. this.state = {}
  76. }
  77. render() {
  78. let {data} = this.props
  79. return (
  80. <div className='kind-wrapper'>
  81. {
  82. data.length === 0?
  83. '这个分类还没有商品呢'
  84. :
  85. <Grid data={data}
  86. columnNum={2}
  87. hasLine={false}
  88. onClick={(product) => {
  89. this.props.history.push({
  90. pathname: '/home/detail',
  91. state: {
  92. id: product.id
  93. }
  94. })
  95. }}
  96. renderItem={dataItem => (
  97. <div key={dataItem.id} className='kind-item'>
  98. <div className='kind-item-img'
  99. style={{backgroundImage: "url('" + dataItem.img + "')"}}/>
  100. <div className='kind-item-description'>
  101. <div className='kind-item-name'>{dataItem.name}</div>
  102. <div className='kind-item-price'>{dataItem.price}</div>
  103. </div>
  104. </div>
  105. )}
  106. />
  107. }
  108. </div>
  109. )
  110. }
  111. }