Commit 98181fab by FE

solve quesiont that reload when click btn of add cart

parent 8395e193
import {api, getParam, http} from "@/utils";
export const RECEIVE_COURSES_DETAIL = 'RECEIVE_COURSES_DETAIL'
export const ADD_COURSES_TO_CART = 'ADD_COURSES_TO_CART';
export const receiveCourseDetail = payload => ({
type: RECEIVE_COURSES_DETAIL,
payload
})
// 加入购物车
export const addCourseToCart = payload => ({
type: ADD_COURSES_TO_CART,
payload
})
export const fetchCoursesListIfNeeded = () => (dispatch, getState) => {
dispatch(getCourses({
courseInfo: getState().detailInfo
......
......@@ -51,6 +51,7 @@ class BtnStatus extends Component {
// 加入购物车 type:1 加入购物车,2加入购物车并跳转到购物车页面去支付
toCart = (type) => {
const { history, addCourseToCart } = this.props;
let data = {
course_id: getParam('id')
};
......@@ -59,14 +60,13 @@ class BtnStatus extends Component {
if (type === 1) {
Toast.info('已加入购物车', 2)
// this.props.getCourses()
document.location.reload()
// document.location.reload()
addCourseToCart();
} else {
this.props.history.replace('/shopcart');
history.replace('/shopcart');ß
}
} else if (res.data.code === 15001) {
this.props.history.replace('/shopcart');
history.replace('/shopcart');
} else {
Toast.info(res.data.msg, 2);
}
......
......@@ -10,10 +10,10 @@ import Single from "./single"
import BtnStatus from "./btnstatus"
import Carouselw from "./carousel"
import {connect} from "react-redux"
import {fetchCoursesListIfNeeded} from "./actions"
import {getCourses, addCourseToCart} from "./actions"
import {getParam, http, browser} from "@/utils"
import {Toast} from 'antd-mobile'
import {compose} from "redux";
import {compose, bindActionCreators} from "redux";
class Detail extends Component {
......@@ -33,7 +33,7 @@ class Detail extends Component {
}
componentDidMount() {
this.props.fetchCoursesListIfNeeded();
this.props.getCourses();
}
// 点击子组件试听按钮
......@@ -273,6 +273,7 @@ class Detail extends Component {
barInfo={this.state.barInfo}
getBargainInfo={this.getBargainInfo}
invitedFriends={this.invitedFriends}
addCourseToCart={this.props.addCourseToCart}
history={this.props.history}></BtnStatus>
{
......@@ -306,7 +307,18 @@ const mapStateToProps = (state) => {
}
}
const mapDispatchToProps = {
fetchCoursesListIfNeeded
// const mapDispatchToProps = {
// fetchCoursesListIfNeeded
// }
const mapDispatchToProps = (dispatch) => {
return bindActionCreators(
{
getCourses,
addCourseToCart
},
dispatch
)
}
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 = {
......@@ -13,6 +13,12 @@ export default function detailInfo(state = initialState, action) {
...state,
...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:
return state
}
......
......@@ -279,20 +279,20 @@ class Order extends Component {
this.publicLocalStorage();
});
} else {
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 => {
console.log(res);
let preorder = res[1],
orderList = preorder.data.data.course;
if(preorder.data.errno !== 200) {
Toast.info(preorder.data.msg, 2);
return;
}
// 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 => {
// console.log(res);
// let preorder = res[1],
// orderList = preorder.data.data.course;
// if(preorder.data.errno !== 200) {
// Toast.info(preorder.data.msg, 2);
// return;
// }
this.publicGetData(preorder);
this.publicLocalStorage();
})
}else{
// this.publicGetData(preorder);
// this.publicLocalStorage();
// })
// }else{
http.get(`${API['base-api']}/m/order/preorder`).then((res) => {
console.log('为了验证什么条件下会走这段代码');
if (res.data.errno !== 200) {
......@@ -303,7 +303,7 @@ class Order extends Component {
this.publicGetData(res);
this.publicLocalStorage();
})
}
// }
}
};
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