@ -0,0 +1,32 @@ | |||
import api from '../api'; | |||
export const TRENDS_FETCH_REQUEST = 'TRENDS_FETCH_REQUEST'; | |||
export const TRENDS_FETCH_SUCCESS = 'TRENDS_FETCH_SUCCESS'; | |||
export const TRENDS_FETCH_FAIL = 'TRENDS_FETCH_FAIL'; | |||
export const fetchTrends = () => (dispatch, getState) => { | |||
dispatch(fetchTrendsRequest()); | |||
api(getState) | |||
.get('/api/v1/trends') | |||
.then(({ data }) => dispatch(fetchTrendsSuccess(data))) | |||
.catch(err => dispatch(fetchTrendsFail(err))); | |||
}; | |||
export const fetchTrendsRequest = () => ({ | |||
type: TRENDS_FETCH_REQUEST, | |||
skipLoading: true, | |||
}); | |||
export const fetchTrendsSuccess = trends => ({ | |||
type: TRENDS_FETCH_SUCCESS, | |||
trends, | |||
skipLoading: true, | |||
}); | |||
export const fetchTrendsFail = error => ({ | |||
type: TRENDS_FETCH_FAIL, | |||
error, | |||
skipLoading: true, | |||
skipAlert: true, | |||
}); |
@ -0,0 +1,43 @@ | |||
import React from 'react'; | |||
import ImmutablePureComponent from 'react-immutable-pure-component'; | |||
import PropTypes from 'prop-types'; | |||
import ImmutablePropTypes from 'react-immutable-proptypes'; | |||
import Hashtag from 'mastodon/components/hashtag'; | |||
export default class Trends extends ImmutablePureComponent { | |||
static defaultProps = { | |||
loading: false, | |||
}; | |||
static propTypes = { | |||
trends: ImmutablePropTypes.list, | |||
fetchTrends: PropTypes.func.isRequired, | |||
}; | |||
componentDidMount () { | |||
this.props.fetchTrends(); | |||
this.refreshInterval = setInterval(() => this.props.fetchTrends(), 36000); | |||
} | |||
componentWillUnmount () { | |||
if (this.refreshInterval) { | |||
clearInterval(this.refreshInterval); | |||
} | |||
} | |||
render () { | |||
const { trends } = this.props; | |||
if (!trends || trends.isEmpty()) { | |||
return null; | |||
} | |||
return ( | |||
<div className='getting-started__trends'> | |||
{trends.take(3).map(hashtag => <Hashtag key={hashtag.get('name')} hashtag={hashtag} />)} | |||
</div> | |||
); | |||
} | |||
} |
@ -0,0 +1,13 @@ | |||
import { connect } from 'react-redux'; | |||
import { fetchTrends } from '../../../actions/trends'; | |||
import Trends from '../components/trends'; | |||
const mapStateToProps = state => ({ | |||
trends: state.getIn(['trends', 'items']), | |||
}); | |||
const mapDispatchToProps = dispatch => ({ | |||
fetchTrends: () => dispatch(fetchTrends()), | |||
}); | |||
export default connect(mapStateToProps, mapDispatchToProps)(Trends); |
@ -0,0 +1,23 @@ | |||
import { TRENDS_FETCH_REQUEST, TRENDS_FETCH_SUCCESS, TRENDS_FETCH_FAIL } from '../actions/trends'; | |||
import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable'; | |||
const initialState = ImmutableMap({ | |||
items: ImmutableList(), | |||
isLoading: false, | |||
}); | |||
export default function trendsReducer(state = initialState, action) { | |||
switch(action.type) { | |||
case TRENDS_FETCH_REQUEST: | |||
return state.set('isLoading', true); | |||
case TRENDS_FETCH_SUCCESS: | |||
return state.withMutations(map => { | |||
map.set('items', fromJS(action.trends)); | |||
map.set('isLoading', false); | |||
}); | |||
case TRENDS_FETCH_FAIL: | |||
return state.set('isLoading', false); | |||
default: | |||
return state; | |||
} | |||
}; |