index.js 8.61 KB
Newer Older
xuzhenghua committed
1 2 3 4 5 6 7 8 9
import React, { PureComponent } from 'react';
import './index.scss'

import RedeemBar from "../RedeemBar";
import Coupon from '../Coupon'
import { http, getParam } from '@/utils'
import { WithFullSize } from '@/HOCs'
import { Toast } from 'antd-mobile'
import { isEmpty } from 'lodash'
zhanghaozhe committed
10
import { connect } from 'react-redux';
xuzhenghua committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

@connect()
class UseCoupon extends PureComponent {

    state = {
        selectedCouponId: 0,
        redeemCode: '',
        couponList: [],
        valid_coupons: [],
        invalid_coupons: [],
        courseId: getParam('id'),
        showUseButton: false
    }

    componentDidMount() {

        const {history, location} = this.props

        const {state} = this.props.location
        if (state && state.from) {
            if (state.from === '/my') {
                this.getMyCoupons();
                this.setState({
                    showUseButton: true
                })
            } else {
                if (!this.state.courseId) {
                    location.state && location.state.from ? history.replace(location.state.from) : history.goBack()
                }
                this.getAllCoupons();
            }
        }
    }

    handleChange = e => {
        let value = e ? e.target.value : ''
        this.setState({redeemCode: value})
    }

    exchange = () => {
zhanghaozhe committed
51
        const {location: {state = {}}} = this.props;
xuzhenghua committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        if (this.state.redeemCode !== '') {
            http.post(`${API.home}/m/coupon/exchange`, {
                code: this.state.redeemCode
            })
                .then(res => {
                    const data = res.data
                    if (data.code === 200) {

                        const coupon = data.data

                        if (this.state.showUseButton) {
                            this.setState({
                                couponList: [...this.state.couponList, coupon],
                                redeemCode: ''
                            })
                        } else {

                            const coupon = data.data

                            if (coupon['ctype'] == 2
                                && coupon['limit_course'] != this.state.courseId) {
                                this.setState({
                                    invalid_coupons: [...this.state.invalid_coupons, coupon],
                                    showUseButton: null
                                });
                            } else {
                                this.setState({
                                    valid_coupons: [...this.state.valid_coupons, coupon],
                                    redeemCode: ''
                                })
                            }

                        }
                        Toast.info('兑换成功')
                        if (state.from === '/my') {
                            this.getMyCoupons()
                        }
zhanghaozhe committed
89
                        if (state.from === '/order') {
xuzhenghua committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
                            this.getAllCoupons()
                        }
                    } else {
                        Toast.info(data.msg)
                    }
                })
        } else {
            Toast.info('请输入兑换码')
        }
    }

    getMyCoupons = () => {
        http.get(`${API.home}/m/coupon/all`)
            .then(res => {
                const data = res.data
                if (data.code === 200) {
                    this.setState({
                        couponList: isEmpty(data.data) ? [] : data.data
                    })
                } else {
                    Toast.info(data.msg)
                }
            })
    }

    getAllCoupons = () => {
        http.post(`${API.home}/m/coupon/select`, {course_id: this.state.courseId})
            .then(res => {
                const data = res.data
                if (data.code === 200) {

                    const inuse_coupon = data.data['inuse_coupon'];


                    this.setState({
                        valid_coupons: inuse_coupon
                            ? [...inuse_coupon, ...data.data.valid_coupons]
                            : data.data.valid_coupons,
                        invalid_coupons: data.data.invalid_coupons,
                        selectedCouponId: inuse_coupon.length ? inuse_coupon[0].id : 0
                    })

                } else {
                    Toast.info(data.msg)
                }
            })
    }

    useCoupon = val => {
        const {history, dispatch} = this.props
        const coupon = this.state.couponList.find(item => item.id === val)

        if (val) {
            if (this.state.showUseButton) {

                if (coupon['ctype'] === 1) {
                    history.push(`/classify`)
                } else {
                    // dispatch(getCourses(coupon['limit_course'], () => {
                        history.push(`/detail?id=${coupon['limit_course']}`);
                        return false;
                    // }));
                }

            } else {

                const {courseId, selectedCouponId} = this.state

                if (selectedCouponId === val) {


                    http.post(`${API.home}/m/coupon/cancel`, {
                        course_id: courseId
                    }).then(res => {
                        const data = res.data
                        if (data.code === 200) {


                            this.setState({
                                selectedCouponId: 0
                            })


                        } else {
                            Toast.info(data.msg)
                        }
                    })


                } else {
                    http.post(`${API.home}/m/coupon/use`, {
                        course_id: this.state.courseId,
                        coupon_id: val
                    })
                        .then(res => {
                            const data = res && res.data
                            if (data.code === 200) {

                                this.setState({selectedCouponId: val})
                                this.props.history.goBack()


                            } else {
                                Toast.info(data.msg)
                            }
                        })

                }


            }

        } else {
            Toast.info('未知错误')
            location.reload()
        }

    }

    render() {
        const {state} = this.props.location
        const {showUseButton, selectedCouponId} = this.state
        return (
            <div className='use-coupon'>
                <RedeemBar onChange={this.handleChange}
                           exchange={this.exchange}
                           redeemCode={this.state.redeemCode}/>
                <div className="coupons-area">
                    <Content
                        coupons={
                            state
                            && state.from
                            && state.from === '/my'
                                ? this.state.couponList
                                : this.state.valid_coupons
                        }
                        showUseButton={showUseButton}
                        selectedCouponId={selectedCouponId}
                        select={this.select}
                        useCoupon={this.useCoupon}
                    />
                    {
                        this.state.invalid_coupons.length > 0 &&
                        (
                            <>
                                <div className='invalid-title'>- 不可使用的优惠券 -</div>
                                <Content
                                    coupons={this.state.invalid_coupons}
                                    selectedCouponId={selectedCouponId}
                                    select={this.select}
                                    purpose={'use'}
                                    invalid={'invalid'}
                                />
                            </>
                        )
                    }
                </div>
            </div>
        );
    }
}

function Content({coupons, ...rest}) {
    if (coupons.length === 0) {
        return (
            <div className='empty'>
                <p>暂无可使用的优惠券</p>
            </div>
        )
    }
    return (
        <ul>
            {
                coupons.map(item => {
                    return (
                        <Coupon
                            key={item.id}
                            {...item}
                            id={item.id}
                            {...rest}
                        />
                    )
                })
            }
        </ul>
    )
}

export default WithFullSize(UseCoupon);