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