Commit c48f2b1d authored by Viacheslav Pavlov's avatar Viacheslav Pavlov
Browse files

Merge branch '7-error-handling-for-sagas' into 'master'

Error handling for sagas

Closes #7

See merge request grin-global/grin-global-ui!6
parents ec05fa46 6770f0b9
......@@ -31,9 +31,16 @@ function *appendAxiosConfig(action) {
},
},
};
let resp = yield call(action.method, ...action.params, xhrConfig);
if (action.onSuccess) {
resp = action.onSuccess(resp); // postprocess
try {
let resp = yield call(action.method, ...action.params, xhrConfig);
if (action.onSuccess) {
resp = action.onSuccess(resp); // postprocess
}
yield put({ type: action.target, payload: { apiCall: ApiCall.success(resp) } });
} catch (e) {
if (action.onFail) {
action.onFail(e); // postprocess
}
yield put({ type: action.target, payload: { apiCall: ApiCall.error(e) } });
}
yield put({ type: action.target, payload: { apiCall: ApiCall.success(resp) } });
}
......@@ -7,7 +7,7 @@ import {
} from 'user/constants';
// Model
import User from '@gringlobal/client/model/user/User';
import { IPageRequest, FilteredPage } from '@gringlobal/client/model/page';
import { IPageRequest } from '@gringlobal/client/model/page';
// Service
import UserService from '@gringlobal/client/service/UserService';
......@@ -46,13 +46,33 @@ export const listUsersAction = (filter: string = '', pageR: IPageRequest = { pag
},
});
// function * listUsersSaga(action) {
// try {
// console.log('listUsersSaga 1');
// const users = yield call(UserService.listUsers, action.payload.filter, action.payload.pageR);
// console.log('listUsersSaga 2');
// yield put(receiveUsersSuccess(users));
// } catch (e) {
// console.log('error in saga: ', e);
// }
// }
function * listUsersSaga(action) {
const users = yield call(UserService.listUsers, action.payload.filter, action.payload.pageR);
yield put(receiveUsersSuccess(users));
console.log('listUsersSaga 1');
yield put({
type: 'API',
target: RECEIVE_USERS,
method: UserService.listUsers,
params: [action.payload.filter, action.payload.pageR],
// onSuccess: (users: FilteredPage<User>) => {
// return users;
// },
});
}
const receiveUsersSuccess = (users: FilteredPage<User>) => ({
type: RECEIVE_USERS,
payload: { users },
});
// const receiveUsersSuccess = (users: FilteredPage<User>) => ({
// type: RECEIVE_USERS,
// payload: { users },
// });
......@@ -4,9 +4,10 @@ import { RECEIVE_USER, RECEIVE_USERS } from 'user/constants';
// Model
import User from '@gringlobal/client/model/user/User';
import { FilteredPage } from '@gringlobal/client/model/page';
import { ApiCall } from '@gringlobal/client/model/common';
const initialState: {
users: FilteredPage<User>,
users: ApiCall<FilteredPage<User>>,
user: User,
} = {
users: null,
......@@ -18,7 +19,7 @@ const userReducer = (state = initialState, action) => {
case RECEIVE_USER: {
const { user } = action.payload;
const { users } = state;
const updatedIndex = users && users.content && users.content.findIndex((userDetails) => userDetails.uuid === user.uuid) || -1;
const updatedIndex = users && users.data && users.data.content && users.data.content.findIndex((userDetails) => userDetails.uuid === user.uuid) || -1;
if (updatedIndex !== -1) {
return update(state, {
......@@ -34,9 +35,10 @@ const userReducer = (state = initialState, action) => {
}
}
case RECEIVE_USERS: {
const { users } = action.payload;
const { apiCall } = action.payload;
console.log('reducer users: ', apiCall);
return update(state, {
users: { $set: users },
users: { $set: apiCall },
});
}
default:
......
......@@ -8,12 +8,13 @@ import { listUsersAction } from 'user/action/public';
// Model
import User from '@gringlobal/client/model/user/User';
import { FilteredPage } from '@gringlobal/client/model/page';
import { ApiCall } from '@gringlobal/client/model/common';
// Ui
import BackButton from '@gringlobal/client/ui/common/button/BackButton';
interface IBrowsePageProps extends React.ClassAttributes<any>, WithTranslation {
listUsersAction: () => void;
users: FilteredPage<User>;
apiCall: ApiCall<FilteredPage<User>>;
}
class UserBrowsePage extends React.Component<IBrowsePageProps> {
......@@ -22,22 +23,23 @@ class UserBrowsePage extends React.Component<IBrowsePageProps> {
({}) => listUsersAction(),
];
public componentWillMount(): void {
const { users, listUsersAction } = this.props;
public componentDidMount(): void {
const { apiCall, listUsersAction } = this.props;
if (!users || !users.content || users.content.length === 0) {
if (!apiCall || !apiCall.loading || !apiCall.error || !apiCall.data) {
listUsersAction();
}
}
public render() {
const { users, t } = this.props;
const { apiCall, t } = this.props;
const { data } = apiCall || { data: undefined };
return (
<>
<h3>{ t('user.admin.p.browse.title') }</h3>
<ul>
{ users && users.content && users.content.length > 0 && users.content.map((user) => (
{ data && data.content && data.content.length > 0 && data.content.map((user) => (
<li key={ user.uuid }>
<Link to={ `/user/${ user.uuid }` }>{ `${ user.email } - ${ user.uuid } - ${ user.roles }` }</Link>
</li>
......@@ -52,7 +54,7 @@ class UserBrowsePage extends React.Component<IBrowsePageProps> {
const mapStateToProps = (state, ownProps) => ({
users: state.user.public.users,
apiCall: state.user.public.users,
});
const mapDispatchToProps = (dispatch) => bindActionCreators({
......
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