Commit 71bac820 by zhanghaozhe

study redux

parent dbfb701d
import React, { Component } from 'react'; import React, { PureComponent } from 'react';
export default function (WrappedComponent) { export default function (WrappedComponent) {
return class extends Component { return class extends PureComponent {
componentDidMount() { componentDidMount() {
const height = window.innerHeight const height = window.innerHeight
document.body.style.height = `${height}px` document.body.style.height = `${height}px`
......
import React, { Component } from 'react' import React, { PureComponent } from 'react'
import NavBar from '@/common/NavBar' import NavBar from '@/common/NavBar'
export default function WithTab(WrappedComponent) { export default function WithTab(WrappedComponent) {
return class extends Component { return class extends PureComponent {
render() { render() {
return ( return (
<> <>
......
...@@ -6,27 +6,47 @@ import MyCourses from "./myCourses" ...@@ -6,27 +6,47 @@ import MyCourses from "./myCourses"
import FreeCourses from './freeCourses' import FreeCourses from './freeCourses'
import WithTab from '@/HOCs/WithTab' import WithTab from '@/HOCs/WithTab'
import WithFullSize from '@/HOCs/WithFullSize' import WithFullSize from '@/HOCs/WithFullSize'
import {compose} from 'redux' import { compose } from 'redux'
class Study extends Component { class Study extends Component {
state = {
}
list;
storeScrollPosition(tab){
}
render() { render() {
const {match} = this.props; const {match} = this.props;
return ( return (
<section id='study'> <section id='study'>
<div className='tab'> <div className='tab'>
<div> <div>
<NavLink to={`${match.url}/my-course`} activeClassName='active'>我的课程</NavLink> <NavLink to={`${match.url}/my-course`}
replace
activeClassName='active'
>我的课程</NavLink>
</div> </div>
<div> <div>
<NavLink to={`${match.url}/free-course`} activeClassName='active'>免费课程</NavLink> <NavLink to={`${match.url}/free-course`}
replace
activeClassName='active'
>免费课程</NavLink>
</div> </div>
</div> </div>
<div className="study-container"> <div className="study-container" ref={el => this.list = el}>
<Switch> <Switch>
<Redirect exact from='/study' to='study/my-course'/> <Redirect exact from='/study' to='study/my-course'/>
<Route path={`${this.props.match.path}/my-course`} component={MyCourses}/> {/*<Route path={`${this.props.match.path}/my-course`} component={MyCourses}/>
<Route path={`${this.props.match.path}/free-course`} component={FreeCourses}/>*/}
<Route path={`${this.props.match.path}/my-course`} render={props => {
<MyCourses storeScrollPosition={this.storeScrollPosition}/>
}}/>
<Route path={`${this.props.match.path}/free-course`} component={FreeCourses}/> <Route path={`${this.props.match.path}/free-course`} component={FreeCourses}/>
</Switch> </Switch>
</div> </div>
...@@ -36,7 +56,6 @@ class Study extends Component { ...@@ -36,7 +56,6 @@ class Study extends Component {
} }
// export default WithFullSize(WithTab(Study))
export default compose( export default compose(
WithFullSize, WithFullSize,
WithTab WithTab
......
...@@ -8,23 +8,31 @@ export const receiveMyCourses = payload => ({ ...@@ -8,23 +8,31 @@ export const receiveMyCourses = payload => ({
}) })
const PAGE_INTERVAL = 1
const NUM_INTERVAL = 10
export const fetchCoursesListIfNeeded = () => (dispatch, getState) => {
const myCourses = getState().myCourses
const {shouldFetch, page, num} = myCourses
if (shouldFetch) {
dispatch(getMyCourses({
page: page + PAGE_INTERVAL,
num: num + NUM_INTERVAL
}))
}
}
export const getMyCourses = payload => dispatch => { export const getMyCourses = payload => dispatch => {
return api.get(`/m/my_course/${payload.page}/${payload.num}`) return api.get(`/m/my_course/${payload.page}/${payload.num}`)
.then(res => { .then(res => {
dispatch(receiveMyCourses(res.data)) dispatch(receiveMyCourses({
courseData: res.data,
page: payload.page,
num: payload.num
}))
dispatch(invalidateCourseDate())
}) })
} }
const NUM_INTERVAL = 10
export const fetchCoursesListIfNeeded = payload => (dispatch, getState) => {
const {shouldFetch, currentPage, currentNum} = getState().myCourses
if(!shouldFetch){
dispatch(getMyCourses)
}
}
export const INVALIDATE_COURSEDATA = 'INVALIDATE_COURSEDATA' export const INVALIDATE_COURSEDATA = 'INVALIDATE_COURSEDATA'
export const invalidateCourseDate = () => ({ export const invalidateCourseDate = () => ({
type: INVALIDATE_COURSEDATA type: INVALIDATE_COURSEDATA
......
...@@ -3,7 +3,11 @@ import { VList } from '@/common' ...@@ -3,7 +3,11 @@ 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 { getMyCourses } from "./actions" import { fetchCoursesListIfNeeded } from "./actions"
function getStudyTime(seconds) { function getStudyTime(seconds) {
return { return {
...@@ -13,7 +17,6 @@ function getStudyTime(seconds) { ...@@ -13,7 +17,6 @@ function getStudyTime(seconds) {
} }
} }
function AddCourse(props) { function AddCourse(props) {
return ( return (
<div className='add-course'> <div className='add-course'>
...@@ -49,12 +52,19 @@ function Record({record: {seconds, lesson_name}}) { ...@@ -49,12 +52,19 @@ function Record({record: {seconds, lesson_name}}) {
) )
} }
class MyCourses extends PureComponent { class MyCourses extends PureComponent {
state = { state = {
addVisible: false addVisible: false,
courseData: []
} }
list;
handleClick = () => { handleClick = () => {
console.log(1); console.log(1);
} }
...@@ -63,7 +73,11 @@ class MyCourses extends PureComponent { ...@@ -63,7 +73,11 @@ class MyCourses extends PureComponent {
} }
componentDidMount() { componentDidMount() {
this.props.getMyCourses({page: 1, num: 10}); this.props.fetchCoursesListIfNeeded();
}
componentWillUnmount() {
console.log('will unmount')
} }
render() { render() {
...@@ -71,7 +85,7 @@ class MyCourses extends PureComponent { ...@@ -71,7 +85,7 @@ class MyCourses extends PureComponent {
if (data && data.length !== 0) { if (data && data.length !== 0) {
return ( return (
<> <>
<ul> <ul ref={el => this.list = el}>
{ {
data.map((item, index) => { data.map((item, index) => {
...@@ -124,6 +138,6 @@ const mapStateToProps = state => ({ ...@@ -124,6 +138,6 @@ const mapStateToProps = state => ({
courseData: state.myCourses.courseData courseData: state.myCourses.courseData
}) })
const mapDispatchToProps = {getMyCourses} const mapDispatchToProps = {fetchCoursesListIfNeeded}
export default connect(mapStateToProps, mapDispatchToProps)(MyCourses) export default connect(mapStateToProps, mapDispatchToProps)(MyCourses)
\ No newline at end of file
...@@ -4,25 +4,19 @@ import { ...@@ -4,25 +4,19 @@ import {
} from './actions' } from './actions'
const initialState = { const initialState = {
shouldFetch: false, shouldFetch: true,
courseData: {}, courseData: [],
currentPage: 1, page: 0,
currentNum: 10 num: 0
} }
export default function myCourses(state = initialState, action) { export default function myCourses(state = initialState, action) {
switch (action.type) { switch (action.type) {
case RECEIVE_MY_COURSES: case RECEIVE_MY_COURSES:
return Object.assign( return {...state, ...action.payload}
{},
state,
{courseData: action.payload})
case INVALIDATE_COURSEDATA: case INVALIDATE_COURSEDATA:
return Object.assign( return {...state, shouldFetch: false}
{},
state,
{shouldFetch: true})
default: default:
return state return state
} }
......
...@@ -3,7 +3,7 @@ import ReactDOM from 'react-dom'; ...@@ -3,7 +3,7 @@ import ReactDOM from 'react-dom';
import { createStore, applyMiddleware } from 'redux' import { createStore, applyMiddleware } from 'redux'
import { Provider } from 'react-redux' import { Provider } from 'react-redux'
import thunk from 'redux-thunk' import thunk from 'redux-thunk'
import { logger } from 'redux-logger' import logger from 'redux-logger'
import Routes from './router'; import Routes from './router';
import rootReducers from './store' import rootReducers from './store'
......
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