Skip to content

Commit

Permalink
Shuffle books in landing page (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
geekysuman authored and nrchandan committed May 24, 2017
1 parent 3ded800 commit b26922b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/actions/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ const actionType = {
UNLOCK_ACCOUNT_ERROR: 'UNLOCK_ACCOUNT_ERROR',
GET_USER_BALANCE_LOADING: 'GET_USER_BALANCE_LOADING',
GET_USER_BALANCE_SUCCESS: 'GET_USER_BALANCE_SUCCESS',
GET_USER_BALANCE_ERROR: 'GET_USER_BALANCE_ERROR'
GET_USER_BALANCE_ERROR: 'GET_USER_BALANCE_ERROR',
SHUFFLE_ALL_BOOKS: 'SHUFFLE_ALL_BOOKS'
}

export default actionType
8 changes: 8 additions & 0 deletions app/actions/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function shuffleArray(array){
let a = array
for (let i = a.length; i; i--) {
let j = Math.floor(Math.random() * i);
[a[i - 1], a[j]] = [a[j], a[i - 1]];
}
return a
}
10 changes: 9 additions & 1 deletion app/actions/libraryActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import actionType from './actionTypes'
import { sessionService } from 'redux-react-session'
import axios from 'axios'
import NotificationType from '../components/notifications/NotificationTypes'

import {shuffleArray} from './helpers'
export const action = (type, flag) => {
return {
type: type,
Expand Down Expand Up @@ -316,3 +316,11 @@ export const unlockAccount = (session, user, password, flag) => {
})
}
}

export const shuffleAllBooks = (books) =>{
return (dispatch) => {
console.log("books",books)
let shuffleBookList = books.length ? shuffleArray(books) : books
dispatch(action(actionType.SHUFFLE_ALL_BOOKS,shuffleBookList))
}
}
1 change: 1 addition & 0 deletions app/components/BooksPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class BooksPage extends React.Component {
if(!this.props.books.allBooks.length) {
this.props.getAllBooks()
}
this.props.shuffleAllBooks(this.props.books.allBooks)
}
componentWillReceiveProps (nextProps) {
if(!nextProps.isExistingMember.user) {
Expand Down
8 changes: 8 additions & 0 deletions app/components/utils/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ export default class Image extends React.Component {
}
this.imageEl = ''
}
componentWillReceiveProps (nextProps) {
console.log(nextProps)
if(nextProps) {
this.setState({
src: nextProps.src
})
}
}
handleLoad () {
this.setState({
loading: false
Expand Down
6 changes: 6 additions & 0 deletions app/reducers/libraryReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ export const allBooksReducers = (state = [], action) => {
allBooks : books
}
}
case 'SHUFFLE_ALL_BOOKS': {
return {
...state,
allBooks: action.payload
}
}
default:
return state
}
Expand Down

0 comments on commit b26922b

Please sign in to comment.