FitTrackee/fittrackee_client/src/reducers/index.js

223 lines
5.4 KiB
JavaScript
Raw Normal View History

2018-01-01 22:36:44 +01:00
import { format } from 'date-fns'
import { routerReducer } from 'react-router-redux'
2017-12-17 14:02:41 +01:00
import { combineReducers } from 'redux'
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
}
}
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
const calendarActivities = (state = initial.calendarActivities, action) => {
switch (action.type) {
case 'UPDATE_CALENDAR':
return {
...state,
data: action.activities,
}
default:
return handleDataAndError(state, 'calendarActivities', action)
}
}
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':
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,
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
}
}
const loading = (state = initial.loading, action) => {
switch (action.type) {
case 'SET_LOADING':
2018-06-11 19:15:38 +02:00
return action.loading
default:
return state
}
}
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':
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':
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)
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':
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,
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,
}
default:
return state
}
}
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,
calendarActivities,
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,
loading,
message,
2018-01-01 11:10:39 +01:00
messages,
2018-05-20 13:12:35 +02:00
records,
router: routerReducer,
2018-04-29 18:23:46 +02:00
sports,
statistics,
2017-12-17 14:02:41 +01:00
user,
})
export default reducers