2018-01-01 22:36:44 +01:00
|
|
|
import { format } from 'date-fns'
|
2018-01-14 19:56:04 +01:00
|
|
|
import { routerReducer } from 'react-router-redux'
|
2017-12-17 14:02:41 +01:00
|
|
|
import { combineReducers } from 'redux'
|
|
|
|
|
2017-12-25 17:45:28 +01:00
|
|
|
import initial from './initial'
|
|
|
|
|
2018-01-01 11:10:39 +01:00
|
|
|
const formData = (state = initial.formData, action) => {
|
|
|
|
switch (action.type) {
|
2018-01-01 16:59:46 +01:00
|
|
|
case 'UPDATE_USER_FORMDATA':
|
2018-01-01 11:10:39 +01:00
|
|
|
return {
|
|
|
|
formData: {
|
|
|
|
...state.formData,
|
2018-01-01 16:59:46 +01:00
|
|
|
[action.target]: action.value
|
2018-01-01 11:10:39 +01:00
|
|
|
},
|
|
|
|
}
|
2018-01-01 16:59:46 +01:00
|
|
|
case 'PROFILE_SUCCESS':
|
2018-01-14 19:56:04 +01:00
|
|
|
case 'EMPTY_USER_FORMDATA':
|
2018-01-01 16:59:46 +01:00
|
|
|
return initial.formData
|
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const formProfile = (state = initial.formProfile, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'UPDATE_PROFILE_FORMDATA':
|
2018-01-01 11:10:39 +01:00
|
|
|
return {
|
2018-01-01 16:59:46 +01:00
|
|
|
formProfile: {
|
|
|
|
...state.formProfile,
|
|
|
|
[action.target]: action.value
|
2018-01-01 11:10:39 +01:00
|
|
|
},
|
|
|
|
}
|
2018-01-01 16:59:46 +01:00
|
|
|
case 'INIT_PROFILE_FORM':
|
2018-01-01 11:10:39 +01:00
|
|
|
return {
|
2018-01-01 16:59:46 +01:00
|
|
|
formProfile: {
|
|
|
|
...state.formProfile,
|
|
|
|
firstName: action.user.firstName,
|
|
|
|
lastName: action.user.lastName,
|
|
|
|
birthDate: action.user.birthDate,
|
|
|
|
location: action.user.location,
|
|
|
|
bio: action.user.bio,
|
2018-01-01 11:10:39 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
case 'PROFILE_SUCCESS':
|
2018-01-01 16:59:46 +01:00
|
|
|
return initial.formProfile
|
2018-01-01 11:10:39 +01:00
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-25 17:45:28 +01:00
|
|
|
const message = (state = initial.message, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'AUTH_ERROR':
|
|
|
|
case 'PROFILE_ERROR':
|
2018-01-01 22:36:44 +01:00
|
|
|
case 'PROFILE_UPDATE_ERROR':
|
2018-01-01 21:54:03 +01:00
|
|
|
case 'PICTURE_ERROR':
|
2017-12-25 17:45:28 +01:00
|
|
|
return action.message
|
|
|
|
case 'LOGOUT':
|
|
|
|
case 'PROFILE_SUCCESS':
|
2018-01-01 11:10:39 +01:00
|
|
|
case '@@router/LOCATION_CHANGE':
|
2017-12-25 17:45:28 +01:00
|
|
|
return ''
|
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-01 11:10:39 +01:00
|
|
|
const messages = (state = initial.messages, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'AUTH_ERRORS':
|
|
|
|
return action.messages
|
|
|
|
case 'LOGOUT':
|
|
|
|
case 'PROFILE_SUCCESS':
|
|
|
|
case '@@router/LOCATION_CHANGE':
|
|
|
|
return []
|
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-25 17:45:28 +01:00
|
|
|
const user = (state = initial.user, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'AUTH_ERROR':
|
|
|
|
case 'PROFILE_ERROR':
|
|
|
|
case 'LOGOUT':
|
|
|
|
window.localStorage.removeItem('authToken')
|
|
|
|
return initial.user
|
2018-01-28 13:01:26 +01:00
|
|
|
case 'PROFILE_SUCCESS':
|
2017-12-25 17:45:28 +01:00
|
|
|
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,
|
2018-01-01 16:59:46 +01:00
|
|
|
isAuthenticated: true,
|
|
|
|
firstName: action.message.data.first_name
|
|
|
|
? action.message.data.first_name
|
|
|
|
: '',
|
|
|
|
lastName: action.message.data.last_name
|
|
|
|
? action.message.data.last_name
|
|
|
|
: '',
|
|
|
|
bio: action.message.data.bio
|
|
|
|
? action.message.data.bio
|
|
|
|
: '',
|
|
|
|
location: action.message.data.location
|
|
|
|
? action.message.data.location
|
|
|
|
: '',
|
|
|
|
birthDate: action.message.data.birth_date
|
2018-01-01 22:36:44 +01:00
|
|
|
? format(new Date(action.message.data.birth_date),
|
|
|
|
'DD/MM/YYYY')
|
2018-01-01 16:59:46 +01:00
|
|
|
: '',
|
2018-01-01 21:54:03 +01:00
|
|
|
picture: action.message.data.picture === true
|
|
|
|
? action.message.data.picture
|
|
|
|
: false,
|
2017-12-25 17:45:28 +01:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 14:02:41 +01:00
|
|
|
const reducers = combineReducers({
|
2018-01-01 11:10:39 +01:00
|
|
|
formData,
|
2018-01-01 16:59:46 +01:00
|
|
|
formProfile,
|
2017-12-25 17:45:28 +01:00
|
|
|
message,
|
2018-01-01 11:10:39 +01:00
|
|
|
messages,
|
2018-01-14 19:56:04 +01:00
|
|
|
router: routerReducer,
|
2017-12-17 14:02:41 +01:00
|
|
|
user,
|
|
|
|
})
|
|
|
|
|
|
|
|
export default reducers
|