index.js 5.73 KB
Newer Older
zhanghaozhe committed
1 2 3
import React, { PureComponent } from "react"
import "./coupon.scss"
import classnames from "classnames"
zhanghaozhe committed
4
import { http } from "src/utils"
zhanghaozhe committed
5 6
import showAlert from "src/common/deposit/end-expansion-alert"
import { Toast } from "antd-mobile"
xuzhenghua committed
7 8
import { withRouter } from "react-router-dom"

zhanghaozhe committed
9
class Coupon extends PureComponent {
zhanghaozhe committed
10 11 12
  VOUCHER = 1 //代金券
  FREE = 2 //免单券
  EXPAND = 4 //膨胀全
zhanghaozhe committed
13

xuzhenghua committed
14
  pick = (e) => {
zhanghaozhe committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    let {
      useCoupon,
      invalid,
      start_amount,
      id,
      location,
      history,
      code,
    } = this.props
    const { state } = location
    if (
      state &&
      state.from &&
      state.from === "/my" &&
      typeof start_amount !== "undefined"
    ) {
xuzhenghua committed
31 32
      history.push(`/expand/index?deposit_code=${code}`)
      return
zhanghaozhe committed
33
    }
xuzhenghua committed
34 35 36 37
    if (start_amount) {
      this.stopExpanding(id, e)
    } else {
      !invalid && useCoupon && useCoupon(this.props.id)
xuzhenghua committed
38
    }
xuzhenghua committed
39 40 41 42 43 44 45 46 47 48
  }

  GoToUse = () => {
    this.props.useCoupon(this.props.id)
  }
  ExchangeCourse = (e) => {
    this.props.toExchangeCourse(e, this.props.code)
  }

  stopExpanding = (id, e) => {
zhanghaozhe committed
49
    const { amount, limit_amount } = this.props
xuzhenghua committed
50 51 52 53 54
    e.stopPropagation()
    showAlert({
      amount,
      limit_amount,
      onConfirm: () => {
zhanghaozhe committed
55 56 57 58 59 60
        http
          .post(`${API.home}/m/end_expansion`, {
            id,
          })
          .then((res) => {
            const { data } = res
zhanghaozhe committed
61
            if (data.code === 200) {
xuzhenghua committed
62 63 64 65 66
              this.props.endExpansion(id, data.data.coupon_id)
            } else {
              Toast.info(data.msg)
            }
          })
zhanghaozhe committed
67
      },
xuzhenghua committed
68 69 70 71 72 73 74 75 76 77 78 79
    })
  }

  render() {
    let {
      ctype,
      amount,
      course_title,
      showUseButton,
      id,
      format_expire_time,
      start_amount,
FE committed
80
      limit_amount,
zhanghaozhe committed
81
      limit_course,
xuzhenghua committed
82 83 84
    } = this.props

    let top, bottom
zhanghaozhe committed
85
    /* eslint-disable default-case */
xuzhenghua committed
86 87 88 89
    switch (ctype) {
      case this.VOUCHER:
        top = (
          <>
zhanghaozhe committed
90 91 92 93
            <p className="denomination">
              {amount} <span></span>
            </p>
            <p className="expire">有效期至:{format_expire_time}</p>
xuzhenghua committed
94 95 96 97
          </>
        )
        bottom = (
          <>
zhanghaozhe committed
98
            <span className={"limit"}>
zhanghaozhe committed
99
              {Number(limit_course) === 0
zhanghaozhe committed
100 101 102
                ? "可用于大于代金券金额的课程"
                : `仅适用于《${course_title}》`}
            </span>
zhanghaozhe committed
103
            {showUseButton && Number(ctype) === this.VOUCHER && (
zhanghaozhe committed
104 105
              <button className="use" onClick={this.GoToUse}>
                立即使用
xuzhenghua committed
106
              </button>
zhanghaozhe committed
107
            )}
xuzhenghua committed
108 109 110 111 112 113
          </>
        )
        break
      case this.FREE:
        top = (
          <>
zhanghaozhe committed
114 115
            <p className="course-title">{course_title}</p>
            <p className="expire">有效期至:{format_expire_time}</p>
xuzhenghua committed
116 117 118 119
          </>
        )
        bottom = (
          <>
zhanghaozhe committed
120
            <span className={"limit"}>{`仅适用于《${course_title}》`}</span>
zhanghaozhe committed
121
            {showUseButton && Number(ctype) === this.FREE && (
zhanghaozhe committed
122 123
              <button className="use" onClick={(e) => this.ExchangeCourse(e)}>
                立即兑换
xuzhenghua committed
124
              </button>
zhanghaozhe committed
125
            )}
xuzhenghua committed
126 127 128 129 130 131 132
          </>
        )
        break
      case this.EXPAND:
        if (!start_amount) {
          top = (
            <>
zhanghaozhe committed
133 134 135 136
              <p className="denomination">
                {amount} <span></span>
              </p>
              <p className="expire">有效期至:{format_expire_time}</p>
xuzhenghua committed
137 138 139 140 141 142
            </>
          )
        } else {
          top = (
            <>
              <div className="progress-container">
zhanghaozhe committed
143 144 145 146 147 148 149 150 151 152 153 154
                <p className="denomination">
                  {amount} <span></span>
                </p>
                <div className={"progress-bar-container"}>
                  <span
                    className={"progress-bar"}
                    style={{
                      width: `${Math.round(
                        ((amount - start_amount) / limit_amount) * 100
                      )}%`,
                    }}
                  ></span>
xuzhenghua committed
155
                </div>
zhanghaozhe committed
156
                <div className={"money"}>
xuzhenghua committed
157 158
                  <span>{start_amount}</span>
                  <span>{limit_amount}</span>
zhanghaozhe committed
159
                </div>
xuzhenghua committed
160 161 162 163 164 165
              </div>
            </>
          )
        }
        bottom = (
          <>
zhanghaozhe committed
166 167 168 169 170 171 172 173 174
            <span className={"limit"}>全场通用</span>
            {start_amount && (
              <button
                className="stop-expanding"
                onClick={this.stopExpanding.bind(this, id)}
              >
                立即结束膨胀
              </button>
            )}
xuzhenghua committed
175 176 177
          </>
        )
        break
zhanghaozhe committed
178
    }
zhanghaozhe committed
179
    /* eslint-enable default-case */
xuzhenghua committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

    return (
      <BaseCoupon
        {...this.props}
        top={top}
        VOUCHER={this.VOUCHER}
        FREE={this.FREE}
        EXPAND={this.EXPAND}
        pick={this.pick}
        bottom={bottom}
      />
    )
  }
}

zhanghaozhe committed
195 196 197 198 199 200 201 202 203 204 205 206
function BaseCoupon({
  pick,
  ctype,
  invalid,
  top,
  VOUCHER,
  FREE,
  EXPAND,
  selectedCouponId,
  id,
  bottom,
}) {
xuzhenghua committed
207
  let couponTypes = {
zhanghaozhe committed
208 209 210
    [VOUCHER]: "代金券",
    [FREE]: "课程券",
    [EXPAND]: "膨胀券",
xuzhenghua committed
211 212
  }
  return (
zhanghaozhe committed
213 214 215 216 217 218 219 220
    <li className="coupon" onClick={pick}>
      <div
        className={classnames(
          "coupon-info",
          invalid ? "invalid" : `coupon-type${ctype}`
        )}
      >
        <p className="type">{couponTypes[ctype]}</p>
xuzhenghua committed
221
        {top}
zhanghaozhe committed
222 223 224 225 226
        {selectedCouponId === id && (
          <i
            className={classnames("iconfont icondanseshixintubiao-5", {
              check: !invalid,
            })}
xuzhenghua committed
227
          />
zhanghaozhe committed
228
        )}
xuzhenghua committed
229
        <ul>
zhanghaozhe committed
230 231 232
          {new Array(19).fill("a").map((item, index) => {
            return <li key={index} />
          })}
xuzhenghua committed
233 234
        </ul>
      </div>
zhanghaozhe committed
235
      <div className="coupon-des">{bottom}</div>
xuzhenghua committed
236 237
    </li>
  )
zhanghaozhe committed
238
}
zhanghaozhe committed
239

xuzhenghua committed
240
export default withRouter(Coupon)