FitTrackee/fittrackee_client/src/actions/index.js

62 lines
1.5 KiB
JavaScript
Raw Normal View History

import FitTrackeeApi from '../fitTrackeeApi/index'
2018-04-30 21:38:09 +02:00
import { history } from '../index'
2018-04-29 18:23:46 +02:00
export const setData = (target, data) => ({
type: 'SET_DATA',
data,
target,
})
2018-04-30 20:08:18 +02:00
export const setError = message => ({
2018-04-29 18:23:46 +02:00
type: 'SET_ERROR',
2018-04-30 20:08:18 +02:00
message,
2018-04-29 18:23:46 +02:00
})
2018-06-11 19:15:38 +02:00
export const setLoading = loading => ({
type: 'SET_LOADING',
2018-06-11 19:15:38 +02:00
loading
})
2018-06-12 13:12:18 +02:00
export const getOrUpdateData = (action, target, data) => dispatch => {
2018-06-04 14:55:09 +02:00
if (data && data.id && isNaN(data.id)) {
2018-05-04 23:04:44 +02:00
return dispatch(setError(target, `${target}: Incorrect id`))
2018-04-30 21:38:09 +02:00
}
2018-06-12 13:12:18 +02:00
return FitTrackeeApi[action](target, data)
2018-05-04 23:04:44 +02:00
.then(ret => {
if (ret.status === 'success') {
dispatch(setData(target, ret.data))
} else {
2019-01-18 19:34:38 +01:00
dispatch(setError(`${target}: ${ret.message || ret.status}`))
2018-05-04 23:04:44 +02:00
}
})
.catch(error => dispatch(setError(`${target}: ${error}`)))
2018-04-30 21:38:09 +02:00
}
export const addData = (target, data) => dispatch => FitTrackeeApi
2018-05-04 23:04:44 +02:00
.addData(target, data)
.then(ret => {
if (ret.status === 'created') {
history.push(`/admin/${target}`)
} else {
dispatch(setError(`${target}: ${ret.status}`))
2018-04-30 20:08:18 +02:00
}
2018-05-04 23:04:44 +02:00
})
.catch(error => dispatch(setError(`${target}: ${error}`)))
export const deleteData = (target, id) => dispatch => {
if (isNaN(id)) {
return dispatch(setError(target, `${target}: Incorrect id`))
2018-04-30 21:38:09 +02:00
}
return FitTrackeeApi
2018-05-04 23:04:44 +02:00
.deleteData(target, id)
.then(ret => {
if (ret.status === 204) {
history.push(`/admin/${target}`)
} else {
2019-01-18 19:34:38 +01:00
dispatch(setError(`${target}: ${ret.message || ret.status}`))
2018-05-04 23:04:44 +02:00
}
})
.catch(error => dispatch(setError(`${target}: ${error}`)))
2018-04-30 21:38:09 +02:00
}