Commit 98181fab by FE

solve quesiont that reload when click btn of add cart

parent 8395e193
import {api, getParam, http} from "@/utils"; import {api, getParam, http} from "@/utils";
export const RECEIVE_COURSES_DETAIL = 'RECEIVE_COURSES_DETAIL' export const RECEIVE_COURSES_DETAIL = 'RECEIVE_COURSES_DETAIL'
export const ADD_COURSES_TO_CART = 'ADD_COURSES_TO_CART';
export const receiveCourseDetail = payload => ({ export const receiveCourseDetail = payload => ({
type: RECEIVE_COURSES_DETAIL, type: RECEIVE_COURSES_DETAIL,
payload payload
}) })
// 加入购物车
export const addCourseToCart = payload => ({
type: ADD_COURSES_TO_CART,
payload
})
export const fetchCoursesListIfNeeded = () => (dispatch, getState) => { export const fetchCoursesListIfNeeded = () => (dispatch, getState) => {
dispatch(getCourses({ dispatch(getCourses({
courseInfo: getState().detailInfo courseInfo: getState().detailInfo
......
...@@ -51,6 +51,7 @@ class BtnStatus extends Component { ...@@ -51,6 +51,7 @@ class BtnStatus extends Component {
// 加入购物车 type:1 加入购物车,2加入购物车并跳转到购物车页面去支付 // 加入购物车 type:1 加入购物车,2加入购物车并跳转到购物车页面去支付
toCart = (type) => { toCart = (type) => {
const { history, addCourseToCart } = this.props;
let data = { let data = {
course_id: getParam('id') course_id: getParam('id')
}; };
...@@ -59,14 +60,13 @@ class BtnStatus extends Component { ...@@ -59,14 +60,13 @@ class BtnStatus extends Component {
if (type === 1) { if (type === 1) {
Toast.info('已加入购物车', 2) Toast.info('已加入购物车', 2)
// this.props.getCourses() // this.props.getCourses()
document.location.reload() // document.location.reload()
addCourseToCart();
} else { } else {
this.props.history.replace('/shopcart'); history.replace('/shopcart');ß
} }
} else if (res.data.code === 15001) { } else if (res.data.code === 15001) {
this.props.history.replace('/shopcart'); history.replace('/shopcart');
} else { } else {
Toast.info(res.data.msg, 2); Toast.info(res.data.msg, 2);
} }
......
...@@ -10,10 +10,10 @@ import Single from "./single" ...@@ -10,10 +10,10 @@ import Single from "./single"
import BtnStatus from "./btnstatus" import BtnStatus from "./btnstatus"
import Carouselw from "./carousel" import Carouselw from "./carousel"
import {connect} from "react-redux" import {connect} from "react-redux"
import {fetchCoursesListIfNeeded} from "./actions" import {getCourses, addCourseToCart} from "./actions"
import {getParam, http, browser} from "@/utils" import {getParam, http, browser} from "@/utils"
import {Toast} from 'antd-mobile' import {Toast} from 'antd-mobile'
import {compose} from "redux"; import {compose, bindActionCreators} from "redux";
class Detail extends Component { class Detail extends Component {
...@@ -33,7 +33,7 @@ class Detail extends Component { ...@@ -33,7 +33,7 @@ class Detail extends Component {
} }
componentDidMount() { componentDidMount() {
this.props.fetchCoursesListIfNeeded(); this.props.getCourses();
} }
// 点击子组件试听按钮 // 点击子组件试听按钮
...@@ -273,6 +273,7 @@ class Detail extends Component { ...@@ -273,6 +273,7 @@ class Detail extends Component {
barInfo={this.state.barInfo} barInfo={this.state.barInfo}
getBargainInfo={this.getBargainInfo} getBargainInfo={this.getBargainInfo}
invitedFriends={this.invitedFriends} invitedFriends={this.invitedFriends}
addCourseToCart={this.props.addCourseToCart}
history={this.props.history}></BtnStatus> history={this.props.history}></BtnStatus>
{ {
...@@ -306,7 +307,18 @@ const mapStateToProps = (state) => { ...@@ -306,7 +307,18 @@ const mapStateToProps = (state) => {
} }
} }
const mapDispatchToProps = { // const mapDispatchToProps = {
fetchCoursesListIfNeeded // fetchCoursesListIfNeeded
// }
const mapDispatchToProps = (dispatch) => {
return bindActionCreators(
{
getCourses,
addCourseToCart
},
dispatch
)
} }
export default connect(mapStateToProps, mapDispatchToProps)(Detail) export default connect(mapStateToProps, mapDispatchToProps)(Detail)
\ No newline at end of file
import {RECEIVE_COURSES_DETAIL} from './actions' import {RECEIVE_COURSES_DETAIL, ADD_COURSES_TO_CART} from './actions'
const initialState = { const initialState = {
...@@ -13,6 +13,12 @@ export default function detailInfo(state = initialState, action) { ...@@ -13,6 +13,12 @@ export default function detailInfo(state = initialState, action) {
...state, ...state,
...courseInfo ...courseInfo
} }
case ADD_COURSES_TO_CART:
const { course_info } = state;
const obj = {
course_info: Object.assign({}, course_info, {in_cart: true})
};
return Object.assign({}, state, obj);
default: default:
return state return state
} }
......
...@@ -279,20 +279,20 @@ class Order extends Component { ...@@ -279,20 +279,20 @@ class Order extends Component {
this.publicLocalStorage(); this.publicLocalStorage();
}); });
} else { } else {
if(getParam('id')) { // if(getParam('id')) {
Promise.all([http.get(`${API.home}/m/del_cart_order/${getParam('id')}`), http.get(`${API['base-api']}/m/order/preorder`)]).then(res => { // Promise.all([http.get(`${API.home}/m/del_cart_order/${getParam('id')}`), http.get(`${API['base-api']}/m/order/preorder`)]).then(res => {
console.log(res); // console.log(res);
let preorder = res[1], // let preorder = res[1],
orderList = preorder.data.data.course; // orderList = preorder.data.data.course;
if(preorder.data.errno !== 200) { // if(preorder.data.errno !== 200) {
Toast.info(preorder.data.msg, 2); // Toast.info(preorder.data.msg, 2);
return; // return;
} // }
this.publicGetData(preorder); // this.publicGetData(preorder);
this.publicLocalStorage(); // this.publicLocalStorage();
}) // })
}else{ // }else{
http.get(`${API['base-api']}/m/order/preorder`).then((res) => { http.get(`${API['base-api']}/m/order/preorder`).then((res) => {
console.log('为了验证什么条件下会走这段代码'); console.log('为了验证什么条件下会走这段代码');
if (res.data.errno !== 200) { if (res.data.errno !== 200) {
...@@ -303,7 +303,7 @@ class Order extends Component { ...@@ -303,7 +303,7 @@ class Order extends Component {
this.publicGetData(res); this.publicGetData(res);
this.publicLocalStorage(); this.publicLocalStorage();
}) })
} // }
} }
}; };
render() { render() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment