145 lines
3.1 KiB
JavaScript
Raw Normal View History

import { connectRouter } from 'connected-react-router'
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),
}
2019-01-04 18:31:34 +01:00
case 'REMOVE_ACTIVITY':
return {
...state,
data: state.data.filter(activity => activity.id !== action.activityId),
}
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-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':
2018-06-12 11:47:01 +02:00
return action.profil
default:
return state
}
}
const statistics = (state = initial.statistics, action) =>
handleDataAndError(state, 'statistics', action)
export default history => combineReducers({
2018-05-02 19:02:39 +02:00
activities,
calendarActivities,
chartData,
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: connectRouter(history),
2018-04-29 18:23:46 +02:00
sports,
statistics,
2017-12-17 14:02:41 +01:00
user,
})