Commit d9f5c936 by zhanghaozhe

study redux

parent f67e6ca8
...@@ -12,8 +12,8 @@ const PAGE_INTERVAL = 1 ...@@ -12,8 +12,8 @@ const PAGE_INTERVAL = 1
export const NUM_INTERVAL = 10 export const NUM_INTERVAL = 10
export const fetchCoursesListIfNeeded = () => (dispatch, getState) => { export const fetchCoursesListIfNeeded = () => (dispatch, getState) => {
const myCourses = getState().myCourses const myCourses = getState().myCourses
const {shouldFetch, page} = myCourses const {switchTab, page} = myCourses
if (shouldFetch) { if (!switchTab) {
dispatch(getMyCourses({ dispatch(getMyCourses({
page: page + PAGE_INTERVAL, page: page + PAGE_INTERVAL,
num: NUM_INTERVAL num: NUM_INTERVAL
...@@ -32,16 +32,16 @@ export const getMyCourses = payload => dispatch => { ...@@ -32,16 +32,16 @@ export const getMyCourses = payload => dispatch => {
page: payload.page, page: payload.page,
num: payload.num num: payload.num
})) }))
dispatch(invalidateCourseDate())
}) })
} }
export const INVALIDATE_COURSEDATA = 'INVALIDATE_COURSEDATA' export const SWITCH_TAB = 'SWITCH_TAB'
export const invalidateCourseDate = () => ({ export const switchTab = payload => ({
type: INVALIDATE_COURSEDATA type: SWITCH_TAB,
payload
}) })
export const VALIDATE_COURSEDATA = 'VALIDATE_COURSEDATA' export const NOMORE_COURSE = 'NOMORE_COURSES'
export const validateCourseData = () => ({ export const nomoreCourse = () => ({
type: VALIDATE_COURSEDATA type: NOMORE_COURSE
}) })
\ No newline at end of file
...@@ -3,9 +3,9 @@ import { VList } from '@/common' ...@@ -3,9 +3,9 @@ import { VList } from '@/common'
import './my-courses.scss' import './my-courses.scss'
import { isToday, format } from "date-fns" import { isToday, format } from "date-fns"
import { connect } from "react-redux" import { connect } from "react-redux"
import { fetchCoursesListIfNeeded, validateCourseData } from "./actions" import { fetchCoursesListIfNeeded, switchTab } from "./actions"
import InfiniteScroll from 'react-infinite-scroller' import InfiniteScroll from 'react-infinite-scroller'
import {debounce} from 'lodash' import { debounce } from 'lodash'
function getStudyTime(seconds) { function getStudyTime(seconds) {
...@@ -78,16 +78,22 @@ class MyCourses extends PureComponent { ...@@ -78,16 +78,22 @@ class MyCourses extends PureComponent {
} }
componentDidMount() { componentDidMount() {
console.log('did mount')
this.props.switchTab(false)
this.props.fetchCoursesListIfNeeded(); this.props.fetchCoursesListIfNeeded();
} }
componentWillUnmount(){
console.log('will unmount')
this.props.switchTab(true);
}
componentDidUpdate() { componentDidUpdate() {
console.log('updated') console.log('updated')
} }
loadFunc = debounce(() => { loadFunc = debounce(() => {
if(this.props.courseList.length % 10 === 0){ if (this.props.courseList.length % 10 === 0) {
this.props.validateCourseData();
this.props.fetchCoursesListIfNeeded(); this.props.fetchCoursesListIfNeeded();
} }
}, 200) }, 200)
...@@ -153,7 +159,7 @@ const mapStateToProps = state => ({ ...@@ -153,7 +159,7 @@ const mapStateToProps = state => ({
const mapDispatchToProps = { const mapDispatchToProps = {
fetchCoursesListIfNeeded, fetchCoursesListIfNeeded,
validateCourseData switchTab
} }
export default connect(mapStateToProps, mapDispatchToProps)(MyCourses) export default connect(mapStateToProps, mapDispatchToProps)(MyCourses)
\ No newline at end of file
import { import {
RECEIVE_MY_COURSES, RECEIVE_MY_COURSES,
INVALIDATE_COURSEDATA, SWITCH_TAB,
VALIDATE_COURSEDATA NOMORE_COURSE
} from './actions' } from './actions'
const initialState = { const initialState = {
shouldFetch: true, switchTab: false,
courseList: [], courseList: [],
page: 0, page: 0,
statusCode: 0, statusCode: 0,
msg: '', msg: '',
noMore: false noMore: false,
} }
...@@ -19,10 +19,9 @@ export default function myCourses(state = initialState, action) { ...@@ -19,10 +19,9 @@ export default function myCourses(state = initialState, action) {
case RECEIVE_MY_COURSES: case RECEIVE_MY_COURSES:
let {courseList, ...rest} = action.payload let {courseList, ...rest} = action.payload
return {...state, ...rest, courseList: state.courseList.concat(courseList)} return {...state, ...rest, courseList: state.courseList.concat(courseList)}
case INVALIDATE_COURSEDATA: case SWITCH_TAB:
return {...state, shouldFetch: false} return {...state, switchTab: action.payload}
case VALIDATE_COURSEDATA: case NOMORE_COURSE:
return {...state, shouldFetch: true}
default: default:
return state return state
} }
......
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