From f441166a3bf27c145a48ae2350cd8cd0931504e9 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Tue, 28 Aug 2018 15:47:02 +0500 Subject: Refactor and add error handling --- src/components/alert/index.js | 13 ++++++ src/containers/feed/index.js | 89 +++++++++++++++++++++++++++++------------ src/containers/feed/styles.css | 17 ++++++-- src/containers/options/index.js | 5 ++- src/redux/github/actions.js | 25 +++++------- src/redux/github/reducer.js | 8 +--- src/redux/github/types.js | 1 - 7 files changed, 105 insertions(+), 53 deletions(-) create mode 100644 src/components/alert/index.js diff --git a/src/components/alert/index.js b/src/components/alert/index.js new file mode 100644 index 0000000..7d04355 --- /dev/null +++ b/src/components/alert/index.js @@ -0,0 +1,13 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import classnames from 'classnames'; + +const Alert = (props) => ( +
{ props.children }
+); + +Alert.propTypes = { + type: PropTypes.string.isRequired +}; + +export default Alert; diff --git a/src/containers/feed/index.js b/src/containers/feed/index.js index 6cff00f..b33e192 100644 --- a/src/containers/feed/index.js +++ b/src/containers/feed/index.js @@ -12,10 +12,12 @@ import RepositoryGrid from '../../components/repository-grid'; import RepositoryList from '../../components/repository-list'; import { updateDateJump, updateLanguage, updateViewType } from '../../redux/preference/actions'; import GroupHeading from '../../components/group-heading'; +import Alert from '../../components/alert'; class FeedContainer extends React.Component { componentDidMount() { const existingRepositories = this.props.github.repositories || []; + // If there are no loaded repositories before, fetch them if (existingRepositories.length === 0) { this.fetchNextRepositories(); @@ -27,7 +29,7 @@ class FeedContainer extends React.Component { this.props.fetchTrending(filters); } - componentDidUpdate(prevProps, prevState, snapshot) { + componentDidUpdate(prevProps) { const currPreferences = this.props.preference; const prevPreferences = prevProps.preference; @@ -43,7 +45,6 @@ class FeedContainer extends React.Component { const filters = {}; filters.dateRange = this.getNextDateRange(); - if (this.props.preference.language) { filters.language = this.props.preference.language; } @@ -73,6 +74,60 @@ class FeedContainer extends React.Component { return dateRange; } + renderTokenWarning() { + return !this.props.preference.options.token && ( + + Although not required but make sure to + + add a token + + to avoid hitting the rate limit + + ); + } + + renderErrors() { + return this.props.github.error && ( + + { this.props.github.error } + + ); + } + + renderAlerts() { + const tokenWarning = this.renderTokenWarning(); + const error = this.renderErrors(); + + if (tokenWarning || error) { + return ( +
+ { tokenWarning } + { error } +
+ ); + } + + return null; + } + + renderRepositoriesList() { + if (this.props.preference.viewType === 'grid') { + return ; + } + + return ; + } + + hasRepositories() { + return this.props.github.repositories && this.props.github.repositories.length !== 0; + } + render() { return (
@@ -81,24 +136,12 @@ class FeedContainer extends React.Component { selectedDateJump={ this.props.preference.dateJump } /> - { - !this.props.preference.options.token && ( -

- Although not required but make sure to - - add a token - - to avoid hitting the rate limit -

- ) - } + { this.renderAlerts() }
{ - this.props.github.repositories && - this.props.github.repositories[0] && - { - this.props.github.repositories && - this.props.github.repositories[0] && -
- { - this.props.preference.viewType === 'grid' - ? - : - } + { this.renderRepositoriesList() } { this.props.github.processing && } { - !this.props.github.processing && ( + !this.props.github.processing && + this.hasRepositories() && + (