Skip to content

Commit

Permalink
[#1] - Moved all article related actions and actionCreators into arti…
Browse files Browse the repository at this point in the history
…cle reducer.
  • Loading branch information
JimmyLv committed Sep 25, 2016
1 parent c6e3682 commit 05f44f0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/components/Header/ToolBar.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { Component, PropTypes } from 'react'
import { browserHistory, withRouter } from 'react-router'

import { randomArticle } from '../../redux/actions'
import { randomArticle } from '../../redux/reducers/article'

@withRouter
export default class ToolBar extends Component {
Expand Down
2 changes: 2 additions & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export const GITHUB = {

export const BASE_URL = `${GITHUB.host}/${GITHUB.user}/${GITHUB.repo}/${GITHUB.branch}`

export const API_URL = `https://raw.githubusercontent.com/${GITHUB.user}/${GITHUB.repo}/${GITHUB.branch}/${GITHUB.folder}`

export async function getConfig() {
try {
const yaml = await (await fetch(`${BASE_URL}/_config.yml`)).text()
Expand Down
2 changes: 1 addition & 1 deletion src/containers/blog/BlogContentContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { GITHUB, SUB_TITLE } from '../../constants'

import Article from '../../components/Blog/Article'
import SocialShare from '../../components/Blog/SocialShare'
import { fetchArticleIfNeeded } from '../../redux/actions'
import { fetchArticleIfNeeded } from '../../redux/reducers/article'
import '../../components/Blog/Article.less'

@connect(
Expand Down
37 changes: 1 addition & 36 deletions src/redux/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,16 @@
import 'whatwg-fetch'
import { hideLoading, showLoading } from 'react-redux-loading-bar'

import { GITHUB } from '../../constants'

const API_URL = `https://raw.githubusercontent.com/${GITHUB.user}/${GITHUB.repo}/${GITHUB.branch}/${GITHUB.folder}`

// TODO: move to src/constants/actionTypes.js

export const FETCH_ARTICLE: string = 'FETCH_ARTICLE'
export const FETCH_ARTICLE_ERROR: string = 'FETCH_ARTICLE_ERROR'
export const FETCH_ARTICLE_SUMMARY: string = 'FETCH_ARTICLE_SUMMARY'
export const FETCH_ARTICLE_SUMMARY_ERROR: string = 'FETCH_ARTICLE_SUMMARY_ERROR'
export const TOGGLE_SIDEBAR: string = 'TOGGLE_SIDEBAR'
export const TOGGLE_CONTENT: string = 'TOGGLE_CONTENT'

// TODO: refactoring to use dux modules, export default reducer but export as for action creators

const shouldFetchArticle =
(state, id) => !(state.article.id && state.article.id === id)

const receiveArticle = (content, id) => dispatch => {
dispatch({
type: FETCH_ARTICLE,
payload: { id, content }
})
dispatch(hideLoading())
}

const fetchArticle = (category, id) => dispatch => {
dispatch(showLoading())
return fetch(`${API_URL}/${category}/${id}.md`)
.then(res => res.text())
.then(content => dispatch(receiveArticle(content, id)))
.catch(err => dispatch({ type: FETCH_ARTICLE_ERROR, payload: err }))
}

export const fetchArticleIfNeeded = (category, id) => (dispatch, getState) => {
if (shouldFetchArticle(getState(), id)) {
return dispatch(fetchArticle(category, id))
}
}

export const fetchArticleSummary = () =>
dispatch => fetch('http://jimmylv.github.io/api/index.json')
dispatch => fetch('https://jimmylv.github.io/api/index.json')
.then(res => res.json())
.then(json => dispatch({ type: FETCH_ARTICLE_SUMMARY, payload: json }))
.catch(err => dispatch({ type: FETCH_ARTICLE_SUMMARY_ERROR, payload: err }))
Expand All @@ -54,6 +22,3 @@ export const toggleSideBar = () => ({
export const toggleContent = () => ({
type: TOGGLE_CONTENT
})

export const randomArticle = (category, id) => dispatch =>
dispatch(fetchArticle(category, id))
36 changes: 34 additions & 2 deletions src/redux/reducers/article.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
// @flow

import jsyaml from 'js-yaml'

import { FETCH_ARTICLE, FETCH_ARTICLE_ERROR } from '../actions'
import { ArticleType } from '../flow/types.x'
import { API_URL } from '../../constants/'

import { hideLoading, showLoading } from 'react-redux-loading-bar'

const FETCH_ARTICLE: string = 'FETCH_ARTICLE'
const FETCH_ARTICLE_ERROR: string = 'FETCH_ARTICLE_ERROR'

const shouldFetchArticle =
(state, id) => !(state.article.id && state.article.id === id)

const receiveArticle = (content, id) => dispatch => {
dispatch({
type: FETCH_ARTICLE,
payload: { id, content }
})
dispatch(hideLoading())
}

const fetchArticle = (category, id) => dispatch => {
dispatch(showLoading())
return fetch(`${API_URL}/${category}/${id}.md`)
.then(res => res.text())
.then(content => dispatch(receiveArticle(content, id)))
.catch(err => dispatch({ type: FETCH_ARTICLE_ERROR, payload: err }))
}

export const fetchArticleIfNeeded = (category, id) => (dispatch, getState) => {
if (shouldFetchArticle(getState(), id)) {
return dispatch(fetchArticle(category, id))
}
}

export const randomArticle = (category, id) => dispatch =>
dispatch(fetchArticle(category, id))

const initialArticle = {
id: '/2011-11-11-hello-world/',
Expand Down

0 comments on commit 05f44f0

Please sign in to comment.