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-04-29 18:23:46 +02:00
|
|
|
const handleDataAndError = (state, type, action) => {
|
|
|
|
if (action.target !== type) {
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
switch (action.type) {
|
|
|
|
case 'SET_DATA':
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
data: action.data[action.target],
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 23:39:59 +02:00
|
|
|
const activities = (state = initial.activities, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'PUSH_ACTIVITIES':
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
data: state.data.concat(action.activities),
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return handleDataAndError(state, 'activities', action)
|
|
|
|
}
|
|
|
|
}
|
2018-05-02 19:02:39 +02:00
|
|
|
|
2018-06-04 14:38:48 +02:00
|
|
|
const calendarActivities = (state = initial.calendarActivities, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'UPDATE_CALENDAR':
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
data: action.activities,
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return handleDataAndError(state, 'calendarActivities', action)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 15:33:56 +02:00
|
|
|
const chartData = (state = initial.chartData, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'SET_CHART_DATA':
|
|
|
|
return action.chartData
|
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-11 15:10:18 +02:00
|
|
|
timezone: action.user.timezone,
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-03 21:42:54 +02:00
|
|
|
const gpx = (state = initial.gpx, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'SET_GPX':
|
|
|
|
return action.gpxContent
|
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-29 20:08:34 +02:00
|
|
|
const loading = (state = initial.loading, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'SET_LOADING':
|
|
|
|
return !state
|
|
|
|
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':
|
2018-04-30 20:08:18 +02:00
|
|
|
case 'SET_ERROR':
|
2017-12-25 17:45:28 +01:00
|
|
|
return action.message
|
|
|
|
case 'LOGOUT':
|
|
|
|
case 'PROFILE_SUCCESS':
|
2018-04-30 20:08:18 +02:00
|
|
|
case 'SET_RESULTS':
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-20 13:12:35 +02:00
|
|
|
const records = (state = initial.records, action) =>
|
|
|
|
handleDataAndError(state, 'records', action)
|
|
|
|
|
2018-04-29 18:23:46 +02:00
|
|
|
const sports = (state = initial.sports, action) =>
|
|
|
|
handleDataAndError(state, 'sports', action)
|
|
|
|
|
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,
|
2018-06-11 15:10:18 +02:00
|
|
|
timezone: action.message.data.timezone
|
|
|
|
? action.message.data.timezone
|
|
|
|
: '',
|
2018-06-05 19:37:10 +02:00
|
|
|
nbActivities: action.message.data.nb_activities,
|
|
|
|
nbSports: action.message.data.nb_sports,
|
|
|
|
totalDistance: action.message.data.total_distance,
|
|
|
|
totalDuration: action.message.data.total_duration,
|
2017-12-25 17:45:28 +01:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-06 20:17:37 +02:00
|
|
|
const statistics = (state = initial.statistics, action) =>
|
|
|
|
handleDataAndError(state, 'statistics', action)
|
|
|
|
|
2017-12-17 14:02:41 +01:00
|
|
|
const reducers = combineReducers({
|
2018-05-02 19:02:39 +02:00
|
|
|
activities,
|
2018-06-04 14:38:48 +02:00
|
|
|
calendarActivities,
|
2018-05-28 15:33:56 +02:00
|
|
|
chartData,
|
2018-01-01 11:10:39 +01:00
|
|
|
formData,
|
2018-01-01 16:59:46 +01:00
|
|
|
formProfile,
|
2018-05-03 21:42:54 +02:00
|
|
|
gpx,
|
2018-05-29 20:08:34 +02:00
|
|
|
loading,
|
2017-12-25 17:45:28 +01:00
|
|
|
message,
|
2018-01-01 11:10:39 +01:00
|
|
|
messages,
|
2018-05-20 13:12:35 +02:00
|
|
|
records,
|
2018-01-14 19:56:04 +01:00
|
|
|
router: routerReducer,
|
2018-04-29 18:23:46 +02:00
|
|
|
sports,
|
2018-06-06 20:17:37 +02:00
|
|
|
statistics,
|
2017-12-17 14:02:41 +01:00
|
|
|
user,
|
|
|
|
})
|
|
|
|
|
|
|
|
export default reducers
|