Skip to content

Commit

Permalink
Added error notification features.
Browse files Browse the repository at this point in the history
  • Loading branch information
geekysuman authored and nrchandan committed May 17, 2017
1 parent 6a34983 commit 00bc309
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 27 deletions.
25 changes: 13 additions & 12 deletions app/actions/libraryActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { web as web3, lms as LMS } from '../web3'
import actionType from './actionTypes'
import { sessionService } from 'redux-react-session'
import axios from 'axios'
import NotificationType from '../components/notifications/NotificationTypes'

export const action = (type, flag) => {
return {
Expand All @@ -16,7 +17,7 @@ export const getAccounts = () => {
dispatch(action(actionType.GET_ACCOUNTS_LOADING, true))
web3.eth.getAccounts((err, accs) => {
if (err != null) {
dispatch(action(actionType.GET_ACCOUNTS_ERROR, err))
dispatch(action(actionType.GET_ACCOUNTS_ERROR, NotificationType.SHOW_ACCOUNTS_ERROR))
}
dispatch(action(actionType.GET_ACCOUNTS_SUCCESS, accs))
dispatch(action(actionType.GET_ACCOUNTS_LOADING, false))
Expand All @@ -34,7 +35,7 @@ export const getOwnerDetails = (response) => {
dispatch(action(actionType.GET_OWNERDETAILS_SUCCESS, user))
dispatch(action(actionType.GET_OWNERDETAILS_LOADING, false))
}).catch((e) => {
dispatch(action(actionType.GET_OWNERDETAILS_ERROR, e))
dispatch(action(actionType.GET_OWNERDETAILS_ERROR, NotificationType.SHOW_OWNERDETAILS_ERROR))
dispatch(action(actionType.GET_OWNERDETAILS_LOADING, false))
})
}
Expand All @@ -50,7 +51,7 @@ export const getAllBooks = () => {
dispatch(action(actionType.GET_ALL_BOOKS_SUCCESS, books))
dispatch(action(actionType.GET_ALL_BOOKS_LOADING, false))
}).catch((e) => {
dispatch(action(actionType.GET_ALL_BOOKS_ERROR, e))
dispatch(action(actionType.GET_ALL_BOOKS_ERROR, NotificationType.SHOW_ALL_BOOKS_ERROR))
dispatch(action(actionType.GET_ALL_BOOKS_LOADING, false))
})
}
Expand All @@ -65,7 +66,7 @@ export const getMyBooks = () => {
dispatch(action(actionType.GET_MY_BOOKS_SUCCESS, books))
dispatch(action(actionType.GET_MY_BOOKS_LOADING, false))
}).catch((e) => {
dispatch(action(actionType.GET_MY_BOOKS_ERROR, e))
dispatch(action(actionType.GET_MY_BOOKS_ERROR, NotificationType.SHOW_MY_BOOKS_ERROR))
dispatch(action(actionType.GET_MY_BOOKS_LOADING, false))
})
}
Expand All @@ -91,7 +92,7 @@ export const addBook = (book) => {
dispatch(action(actionType.GET_ADD_BOOKS_SUCCESS, book))
dispatch(action(actionType.GET_ADD_BOOKS_LOADING, false))
}).catch((e) => {
dispatch(action(actionType.GET_ADD_BOOKS_ERROR, e))
dispatch(action(actionType.GET_ADD_BOOKS_ERROR, NotificationType.SHOW_ADD_BOOKS_ERROR))
dispatch(action(actionType.GET_ADD_BOOKS_LOADING, false))
})
}
Expand All @@ -105,7 +106,7 @@ export const returnBook = (book) => {
}).then((response) => {
dispatch(action(actionType.GET_RETURN_BOOKS_SUCCESS, book))
}).catch((e) => {
dispatch(action(actionType.GET_RETURN_BOOKS_ERROR, e))
dispatch(action(actionType.GET_RETURN_BOOKS_ERROR, NotificationType.SHOW_GET_RETURN_BOOKS_ERROR))
}).then(() => {
dispatch(action(actionType.GET_RETURN_BOOKS_LOADING, false))
})
Expand All @@ -120,7 +121,7 @@ export const borrowBook = (book, ownerDetails) => {
}).then((response) => {
dispatch(action(actionType.GET_BORROW_BOOKS_SUCCESS, { book, owner: ownerDetails.account }))
}).catch((e) => {
dispatch(action(actionType.GET_BORROW_BOOKS_ERROR, e))
dispatch(action(actionType.GET_BORROW_BOOKS_ERROR, NotificationType.SHOW_GET_BORROW_BOOKS_ERROR))
}).then(() => {
dispatch(action(actionType.GET_BORROW_BOOKS_LOADING, false))
})
Expand All @@ -147,7 +148,7 @@ export const rateBook = (rating, comment, book, ownerDetails) => {
}))
dispatch(action(actionType.RATE_BOOK_LOADING, false))
}).catch((e) => {
dispatch(action(actionType.RATE_BOOK_ERROR, e))
dispatch(action(actionType.RATE_BOOK_ERROR, NotificationType.SHOW_GET_RATE_BOOK_ERROR))
dispatch(action(actionType.RATE_BOOK_LOADING, false))
})
}
Expand Down Expand Up @@ -183,7 +184,7 @@ export const getMemberDetailsByEmail = (response) => {
login(response, user)
dispatch(action(actionType.GET_MEMBER_DETAILS_EMAIL_SUCCESS, user))
}).catch((e) => {
dispatch(action(actionType.GET_MEMBER_DETAILS_EMAIL_ERROR, e))
dispatch(action(actionType.GET_MEMBER_DETAILS_EMAIL_ERROR, NotificationType.SHOW_GET_MEMBER_DETAILS_EMAIL_ERROR))
}).then(() => {
dispatch(action(actionType.GET_MEMBER_DETAILS_EMAIL_LOADING, false))
})
Expand All @@ -201,7 +202,7 @@ export const getRatings = (dispatch) => {
rateEvent.watch(function(err, result) {
rateEvent.stopWatching();
if (err) {
dispatch(action(actionType.GET_RATE_BOOK_ERROR, err))
dispatch(action(actionType.GET_RATE_BOOK_ERROR, NotificationType.SHOW_GET_RATE_BOOK_ERROR))
} else {
dispatch(action(actionType.GET_RATE_BOOK_SUCCESS, result.args))
}
Expand Down Expand Up @@ -229,7 +230,7 @@ export const createAccount = (session,password) => {
], dispatch, session.user.body)
})
.catch((error) => {
dispatch(action(actionType.CREATE_ACCOUNT_ERROR, error))
dispatch(action(actionType.CREATE_ACCOUNT_ERROR, NotificationType.SHOW_CREATE_ACCOUNT_ERROR))
}).then(() => {
dispatch(action(actionType.CREATE_ACCOUNT_LOADING, false))
});
Expand All @@ -253,7 +254,7 @@ export const addMember = (member, dispatch, session) => {
login(session, member)
}).catch((err) => {
logout()
dispatch(action(actionType.ADD_MEMBER_ERROR, err))
dispatch(action(actionType.ADD_MEMBER_ERROR, NotificationType.SHOW_ADD_MEMBER_ERROR))
}).then(() => {
dispatch(action(actionType.ADD_MEMBER_LOADING, false))
})
Expand Down
5 changes: 4 additions & 1 deletion app/components/BooksPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import SearchBook from './SearchBook'
import Modal from 'react-modal'
import LMSAuth from './LMSAuth'
import Loader from './Loader'
import NotifyMe from './notifications/NotifyMe'

const style = {
marginTop : '15px'
Expand All @@ -30,7 +31,8 @@ const mapStateToProps = (state, ownProps) => {
ownerDetails: state.session.user,
filteredBooks: state.filteredBooks,
loading: state.loading,
session: state.session
session: state.session,
error: state.error
}
}

Expand Down Expand Up @@ -106,6 +108,7 @@ export class BooksPage extends React.Component {
{
this.renderLoader()
}
<NotifyMe message={this.props.error}/>
<div className='container'>
<div className='row'>
<div className='col-md-7'>
Expand Down
5 changes: 4 additions & 1 deletion app/components/Dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as libraryActions from '../actions/libraryActions'
import Book from './Book'
import BooksForm from './BooksForm'
import Loader from './Loader'
import NotifyMe from './notifications/NotifyMe'

const modalStyle = {
content : {
Expand All @@ -22,7 +23,8 @@ const mapStateToProps = (state, ownProps) => {
allBooks: state.books.allBooks,
ownerDetails: state.session.user,
loading: state.loading,
session: state.session
session: state.session,
error: state.error
}
}

Expand Down Expand Up @@ -68,6 +70,7 @@ export class Dashboard extends React.Component {
const borrowedBooks = this.props.allBooks.filter((book) => book.borrower === this.props.ownerDetails.account)
return (
<div>
<NotifyMe message={this.props.error}/>
<div className='add-btn'>
<button className='btn btn-default' onClick={() => this.toggleModal('addBook')}>Add Book</button>
<Modal
Expand Down
54 changes: 54 additions & 0 deletions app/components/notifications/NotificationTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const NotificationType = {
SHOW_ADD_BOOKS_ERROR : {
type : 'error',
title : 'Oops',
text : 'Error, in Adding Book.'
},
SHOW_ACCOUNTS_ERROR : {
type : 'error',
title : 'Oops',
text : 'Not getting User Account.'
},
SHOW_OWNERDETAILS_ERROR : {
type : 'error',
title : 'Oops',
text : 'Not getting Owner Details.'
},
SHOW_ALL_BOOKS_ERROR : {
type : 'error',
title : 'Oops',
text : 'Error, in getting all books.'
},
SHOW_MY_BOOKS_ERROR : {
type : 'error',
title : 'Oops',
text : 'Error, in getting my books.'
},
SHOW_RATE_BOOK_ERROR : {
type : 'error',
title : 'Oops',
text : 'Error, in rating book.'
},
SHOW_GET_BORROW_BOOKS_ERROR : {
type : 'error',
title : 'Oops',
text : 'Error, in borrowing book.'
},
SHOW_GET_RATE_BOOK_ERROR : {
type : 'error',
title : 'Oops',
text : 'Error, in getting rate book.'
},
SHOW_CREATE_ACCOUNT_ERROR : {
type : 'error',
title : 'Oops',
text : 'Error, in creating account.'
},
SHOW_ADD_MEMBER_ERROR : {
type : 'error',
title : 'Oops',
text : 'Error, in adding member.'
}
}

export default NotificationType
61 changes: 61 additions & 0 deletions app/components/notifications/NotifyMe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import ReactDOM from 'react-dom';
import Notifications from 'react-notification-system-redux';

class NotifyMe extends React.Component {
componentWillReceiveProps(nextProps) {
if (JSON.stringify(this.props.message) !== JSON.stringify(nextProps.message)) {
const { type } = nextProps.message.message;
switch (type) {
case 'success':
this.dispatchNotification(Notifications.success, 250, nextProps);
case 'error':
this.dispatchNotification(Notifications.error, 250, nextProps);
default: break
}
}
}
dispatchNotification(fn, timeout, nextProps) {
const { title, text } = nextProps.message.message;
const notificationOpts = {
// uid: 'once-please', // you can specify your own uid if required
title: title,
message: text,
position: 'br',
autoDismiss: 0,
action: {
label: 'Close!!',
// callback: () => alert('clicked!')
}
};
setTimeout(() => {
this.context.store.dispatch(fn(notificationOpts));
}, timeout);
}

render() {
const { notifications } = this.props;

return (
<div>
<Notifications notifications={notifications} />
</div>
);
}
}

// export default NotifyMe;

NotifyMe.contextTypes = {
store: PropTypes.object
};

NotifyMe.propTypes = {
notifications: PropTypes.array
};

export default connect(
state => ({ notifications: state.notifications })
)(NotifyMe);
4 changes: 3 additions & 1 deletion app/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import {
myBooksReducers
} from './libraryReducer'
import { sessionReducer } from 'redux-react-session'
import {reducer as notifications} from 'react-notification-system-redux';

const rootReducer = combineReducers({
accounts: libraryReducer,
loading: loadingReducer,
error: errorReducer,
books: allBooksReducers,
myBooks: myBooksReducers,
session: sessionReducer
session: sessionReducer,
notifications
})

export default rootReducer
60 changes: 54 additions & 6 deletions app/reducers/libraryReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,65 @@ export const loadingReducer = (state = {}, action) => {
export const errorReducer = (state = [], action) => {
switch (action.type) {
case 'GET_ACCOUNTS_ERROR':
return action.payload
return {
...state,
message : action.payload
}
case 'GET_OWNERDETAILS_ERROR':
return action.payload
return {
...state,
message : action.payload
}
case 'GET_ALL_BOOKS_ERROR':
return action.payload
return {
...state,
message : action.payload
}
case 'GET_MY_BOOKS_ERROR':
return action.payload
return {
...state,
message : action.payload
}
case 'GET_ADD_BOOKS_ERROR':
return action.payload
return {
...state,
message : action.payload
}
case 'RATE_BOOK_ERROR':
return action.payload
return {
...state,
message : action.payload
}
case 'GET_RETURN_BOOKS_ERROR':
return {
...state,
message : action.payload
}
case 'GET_BORROW_BOOKS_ERROR':
return {
...state,
message : action.payload
}
case 'GET_MEMBER_DETAILS_EMAIL_ERROR':
return {
...state,
message : action.payload
}
case 'GET_RATE_BOOK_ERROR':
return {
...state,
message : action.payload
}
case 'CREATE_ACCOUNT_ERROR':
return {
...state,
message : action.payload
}
case 'ADD_MEMBER_ERROR':
return {
...state,
message : action.payload
}
default:
return state
}
Expand Down
Loading

0 comments on commit 00bc309

Please sign in to comment.