Commit 6dbc8998 authored by Quoc Huy Nguyen Dinh's avatar Quoc Huy Nguyen Dinh
Browse files

Closes #110

Adding try catch for badges to gracefully handle API errors
parent e0ac21d3
......@@ -36,6 +36,7 @@ class SubscriptionsList extends React.Component {
};
const hivebuzzBadges = _.get(badges, 'hivebuzz', []);
const peakdBadges = _.get(badges, 'peakd', []);
const hasBadges = !_.isEmpty(hivebuzzBadges) || !_.isEmpty(peakdBadges);
if (hivebuzzBadges) {
hivebuzzBadges.forEach(badge => {
const type = badge.get('type');
......@@ -114,8 +115,7 @@ class SubscriptionsList extends React.Component {
</div>
<div className="article_section">
<h4>{tt('g.badges_and_achievements')}</h4>
{(!_.isEmpty(hivebuzzBadges) ||
!_.isEmpty(peakdBadges.size)) && (
{hasBadges && (
<div>
<p>
{tt('g.badges_and_achievements_description')}{' '}
......@@ -186,8 +186,7 @@ class SubscriptionsList extends React.Component {
</div>
</div>
)}
{_.isEmpty(hivebuzzBadges) &&
!_.isEmpty(peakdBadges.size) && (
{!hasBadges && (
<p>{tt('g.badges_and_achievements_none')}</p>
)}
</div>
......
......@@ -5,13 +5,17 @@ const ADD_USER_PROFILE = 'user_profile/ADD';
const ADD_LISTED_ACCOUNTS = 'user_profile/LISTED';
const ADD_HIVEBUZZ_BADGES = 'user_profile/HIVEBUZZ_BADGES';
const ADD_PEAKD_BADGES = 'user_profile/PEAKD_BADGES';
const SET_ERROR = 'user_profile/ERROR';
const defaultState = fromJS({
profiles: {},
error: false,
hivebuzzBadges: [],
peakdBadges: [],
});
export default function reducer(state = defaultState, action) {
const payload = action.payload;
const { payload } = action;
switch (action.type) {
case ADD_USER_PROFILE: {
......@@ -80,13 +84,12 @@ export default function reducer(state = defaultState, action) {
return state;
}
case ADD_LISTED_ACCOUNTS: {
case SET_ERROR: {
if (payload) {
return state.setIn(
['listedAccounts', payload.username],
fromJS(payload.listed_accounts)
);
const { error } = payload;
return state.setIn(['error'], error);
}
return state;
}
......@@ -111,6 +114,11 @@ export const addPeakdBadges = payload => ({
payload,
});
export const setError = payload => ({
type: SET_ERROR,
payload,
});
export const addList = payload => ({
type: ADD_LISTED_ACCOUNTS,
payload,
......
......@@ -5,6 +5,7 @@ import PeakdApi from 'app/utils/peakdApi';
import * as userProfileActions from './UserProfilesReducer';
const FETCH_PROFILE = 'userProfilesSaga/FETCH_PROFILE';
const FETCH_LISTS = 'userProfilesSaga/FETCH_LISTS';
const FETCH_HIVEBUZZ_BADGES = 'userProfileSaga/FETCH_HIVEBUZZ_BADGES';
const FETCH_PEAKD_BADGES = 'userProfileSaga/FETCH_PEAKD_BADGES';
......@@ -29,28 +30,60 @@ export function* fetchUserProfile(action) {
export function* fetchUserHivebuzzBadges(action) {
const { account } = action.payload;
try {
const ret = yield call(HivebuzzApi.getBadges, { account });
if (!ret) throw new Error('Hivebuzz badges error');
if (!ret) {
console.error('Hivebuzz badges error');
yield put(
userProfileActions.setError({
error: true,
})
);
} else {
yield put(
userProfileActions.addHivebuzzBadges({
username: account,
badges: [...ret],
})
);
}
} catch (e) {
console.error('Hivebuzz badges error');
yield put(
userProfileActions.setError({
error: true,
})
);
}
}
export function* fetchUserPeakdBadges(action) {
const { account } = action.payload;
try {
const ret = yield call(PeakdApi.getBadges, { account });
if (!ret) throw new Error('Peakd badges error');
if (!ret) {
console.error('Peakd badges error');
yield put(
userProfileActions.setError({
error: true,
})
);
} else {
yield put(
userProfileActions.addPeakdBadges({
username: account,
badges: [...ret],
})
);
}
} catch (e) {
console.error('Peakd badges error');
yield put(
userProfileActions.setError({
error: true,
})
);
}
}
// Action creators
......
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