2017-12-17 14:02:41 +01:00
|
|
|
import { combineReducers } from 'redux'
|
|
|
|
|
2017-12-25 17:45:28 +01:00
|
|
|
import initial from './initial'
|
|
|
|
|
|
|
|
const message = (state = initial.message, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'AUTH_ERROR':
|
|
|
|
case 'PROFILE_ERROR':
|
|
|
|
return action.message
|
|
|
|
case 'LOGOUT':
|
|
|
|
return ''
|
|
|
|
case 'PROFILE_SUCCESS':
|
|
|
|
return ''
|
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = (state = initial.user, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'AUTH_ERROR':
|
|
|
|
case 'PROFILE_ERROR':
|
|
|
|
case 'LOGOUT':
|
|
|
|
window.localStorage.removeItem('authToken')
|
|
|
|
return initial.user
|
|
|
|
case 'PROFILE_SUCCESS':
|
|
|
|
return {
|
|
|
|
id: action.message.data.id,
|
|
|
|
username: action.message.data.username,
|
|
|
|
email: action.message.data.email,
|
|
|
|
isAdmin: action.message.data.admin,
|
|
|
|
createdAt: action.message.data.created_at,
|
|
|
|
isAuthenticated: true
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const formData = (state = initial.formData, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'UPDATE_FORMDATA_EMAIL':
|
|
|
|
return {
|
|
|
|
formData: {
|
|
|
|
...state.formData,
|
|
|
|
email: action.email
|
|
|
|
},
|
|
|
|
}
|
|
|
|
case 'UPDATE_FORMDATA_USERNAME':
|
|
|
|
return {
|
|
|
|
formData: {
|
|
|
|
...state.formData,
|
|
|
|
username: action.username
|
|
|
|
},
|
|
|
|
}
|
|
|
|
case 'UPDATE_FORMDATA_PASSWORD':
|
|
|
|
return {
|
|
|
|
formData: {
|
|
|
|
...state.formData,
|
|
|
|
password: action.password
|
|
|
|
},
|
|
|
|
}
|
|
|
|
case 'PROFILE_SUCCESS':
|
|
|
|
return initial.formData
|
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
}
|
2017-12-17 14:02:41 +01:00
|
|
|
|
|
|
|
const reducers = combineReducers({
|
2017-12-25 17:45:28 +01:00
|
|
|
message,
|
2017-12-17 14:02:41 +01:00
|
|
|
user,
|
2017-12-25 17:45:28 +01:00
|
|
|
formData,
|
2017-12-17 14:02:41 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
export default reducers
|