156 lines
3.4 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
}
2019-08-28 15:51:04 +02:00
if (action.type === 'SET_DATA') {
return {
...state,
data: action.data[action.target],
}
2018-04-29 18:23:46 +02:00
}
2019-08-28 15:51:04 +02:00
return state
2018-04-29 18:23:46 +02:00
}
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) => {
2019-08-28 15:51:04 +02:00
if (action.type === 'UPDATE_CALENDAR') {
return {
...state,
data: action.activities,
}
}
2019-08-28 15:51:04 +02:00
return handleDataAndError(state, 'calendarActivities', action)
}
const chartData = (state = initial.chartData, action) => {
2019-08-28 15:51:04 +02:00
if (action.type === 'SET_CHART_DATA') {
return action.chartData
}
2019-08-28 15:51:04 +02:00
return state
}
2018-05-03 21:42:54 +02:00
const gpx = (state = initial.gpx, action) => {
2019-08-28 15:51:04 +02:00
if (action.type === 'SET_GPX') {
return action.gpxContent
2018-05-03 21:42:54 +02:00
}
2019-08-28 15:51:04 +02:00
return state
2018-05-03 21:42:54 +02:00
}
2019-09-08 17:14:48 +02:00
const language = (state = initial.language, action) => {
if (action.type === 'SET_LANGUAGE') {
return action.language
}
return state
}
const loading = (state = initial.loading, action) => {
2019-08-28 15:51:04 +02:00
if (action.type === 'SET_LOADING') {
return action.loading
}
2019-08-28 15:51:04 +02:00
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)
const sports = (state = initial.sports, action) => {
if (action.type === 'UPDATE_SPORT_DATA') {
return {
...state,
data: state.data.map(sport => {
if (sport.id === action.data.id) {
sport.is_active = action.data.is_active
}
return sport
}),
}
}
return handleDataAndError(state, 'sports', action)
}
2018-04-29 18:23:46 +02: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':
2018-06-12 11:47:01 +02:00
return action.profil
default:
return state
}
}
const statistics = (state = initial.statistics, action) =>
handleDataAndError(state, 'statistics', action)
2019-08-28 15:35:22 +02:00
export default history =>
combineReducers({
activities,
calendarActivities,
chartData,
gpx,
2019-09-08 17:14:48 +02:00
language,
2019-08-28 15:35:22 +02:00
loading,
message,
messages,
records,
router: connectRouter(history),
sports,
statistics,
user,
})