149 lines
4.1 KiB
JavaScript
Raw Normal View History

import { parse } from 'date-fns'
import mpwoGenericApi from '../mwpoApi'
2018-05-01 19:29:21 +02:00
import mpwoApi from '../mwpoApi/activities'
import { history } from '../index'
import { formatChartData } from '../utils'
import { setError, setLoading } from './index'
2018-05-01 19:29:21 +02:00
export const pushActivities = activities => ({
type: 'PUSH_ACTIVITIES',
activities,
})
export const updateCalendar = activities => ({
type: 'UPDATE_CALENDAR',
activities,
})
2018-05-03 21:42:54 +02:00
export const setGpx = gpxContent => ({
type: 'SET_GPX',
gpxContent,
})
2018-05-01 19:29:21 +02:00
export const setChartData = chartData => ({
type: 'SET_CHART_DATA',
chartData,
})
2018-05-04 23:04:44 +02:00
export const addActivity = form => dispatch => mpwoApi
.addActivity(form)
.then(ret => {
if (ret.status === 'created') {
if (ret.data.activities.length === 0) {
dispatch(setError('activities: no correct file'))
} else if (ret.data.activities.length === 1) {
history.push(`/activities/${ret.data.activities[0].id}`)
} else { // ret.data.activities.length > 1
history.push('/')
}
2018-05-04 23:04:44 +02:00
} else {
dispatch(setError(`activities: ${ret.message}`))
}
dispatch(setLoading())
2018-05-04 23:04:44 +02:00
})
.catch(error => dispatch(setError(`activities: ${error}`)))
2018-05-03 21:42:54 +02:00
2018-05-04 23:04:44 +02:00
2018-05-08 18:20:41 +02:00
export const addActivityWithoutGpx = form => dispatch => mpwoApi
.addActivityWithoutGpx(form)
.then(ret => {
if (ret.status === 'created') {
2018-05-08 21:35:37 +02:00
history.push(`/activities/${ret.data.activities[0].id}`)
2018-05-08 18:20:41 +02:00
} else {
dispatch(setError(`activities: ${ret.message}`))
}
})
.catch(error => dispatch(setError(`activities: ${error}`)))
2018-05-04 23:04:44 +02:00
export const getActivityGpx = activityId => dispatch => {
2018-05-04 22:30:11 +02:00
if (activityId) {
2018-05-03 21:42:54 +02:00
return mpwoApi
.getActivityGpx(activityId)
.then(ret => {
if (ret.status === 'success') {
dispatch(setGpx(ret.data.gpx))
} else {
dispatch(setError(`activities: ${ret.message}`))
}
})
.catch(error => dispatch(setError(`activities: ${error}`)))
}
2018-05-04 23:04:44 +02:00
dispatch(setGpx(null))
2018-05-03 21:42:54 +02:00
}
2018-05-10 16:56:45 +02:00
export const getActivityChartData = activityId => dispatch => {
if (activityId) {
return mpwoApi
.getActivityChartData(activityId)
.then(ret => {
if (ret.status === 'success') {
dispatch(setChartData(formatChartData(ret.data.chart_data)))
} else {
dispatch(setError(`activities: ${ret.message}`))
}
})
.catch(error => dispatch(setError(`activities: ${error}`)))
}
dispatch(setChartData(null))
}
export const deleteActivity = id => dispatch => mpwoGenericApi
.deleteData('activities', id)
.then(ret => {
if (ret.status === 204) {
history.push('/')
} else {
dispatch(setError(`activities: ${ret.status}`))
}
})
.catch(error => dispatch(setError(`activities: ${error}`)))
2018-05-10 16:56:45 +02:00
export const editActivity = form => dispatch => mpwoGenericApi
.updateData('activities', form)
.then(ret => {
if (ret.status === 'success') {
history.push(`/activities/${ret.data.activities[0].id}`)
} else {
dispatch(setError(`activities: ${ret.message}`))
}
dispatch(setLoading())
2018-05-10 16:56:45 +02:00
})
.catch(error => dispatch(setError(`activities: ${error}`)))
export const getMoreActivities = page => dispatch => mpwoGenericApi
2018-06-04 14:55:09 +02:00
.getData('activities', { page })
.then(ret => {
if (ret.status === 'success') {
if (ret.data.activities.length > 0) {
dispatch(pushActivities(ret.data.activities))
}
} else {
dispatch(setError(`activities: ${ret.message}`))
}
})
.catch(error => dispatch(setError(`activities: ${error}`)))
export const getMonthActivities = (start, end) => dispatch => mpwoGenericApi
.getData('activities', { start, end, order: 'asc', per_page: 100 })
.then(ret => {
if (ret.status === 'success') {
if (ret.data.activities.length > 0) {
for (let i = 0; i < ret.data.activities.length; i++) {
ret.data.activities[i].activity_date = parse(
ret.data.activities[i].activity_date
)
}
dispatch(updateCalendar(ret.data.activities))
}
} else {
dispatch(setError(`activities: ${ret.message}`))
}
})
.catch(error => dispatch(setError(`activities: ${error}`)))