Client: refactor

This commit is contained in:
Sam
2018-06-12 13:12:18 +02:00
parent ca8a89915e
commit 85d335da97
9 changed files with 19 additions and 36 deletions

View File

@ -18,12 +18,11 @@ export const setLoading = loading => ({
loading
})
export const getData = (target, data) => dispatch => {
export const getOrUpdateData = (action, target, data) => dispatch => {
if (data && data.id && isNaN(data.id)) {
return dispatch(setError(target, `${target}: Incorrect id`))
}
return FitTrackeeApi
.getData(target, data)
return FitTrackeeApi[action](target, data)
.then(ret => {
if (ret.status === 'success') {
dispatch(setData(target, ret.data))
@ -45,22 +44,6 @@ export const addData = (target, data) => dispatch => FitTrackeeApi
})
.catch(error => dispatch(setError(`${target}: ${error}`)))
export const updateData = (target, data) => dispatch => {
if (isNaN(data.id)) {
return dispatch(setError(target, `${target}: Incorrect id`))
}
return FitTrackeeApi
.updateData(target, data)
.then(ret => {
if (ret.status === 'success') {
dispatch(setData(target, ret.data))
} else {
dispatch(setError(`${target}: ${ret.message}`))
}
})
.catch(error => dispatch(setError(`${target}: ${error}`)))
}
export const deleteData = (target, id) => dispatch => {
if (isNaN(id)) {
return dispatch(setError(target, `${target}: Incorrect id`))

View File

@ -2,7 +2,7 @@ import FitTrackeeGenericApi from '../fitTrackeeApi'
import FitTrackeeApi from '../fitTrackeeApi/user'
import { history } from '../index'
import { generateIds } from '../utils'
import { getData } from './index'
import { getOrUpdateData } from './index'
const AuthError = message => ({ type: 'AUTH_ERROR', message })
@ -32,7 +32,7 @@ export const getProfile = () => dispatch => FitTrackeeGenericApi
.getData('auth/profile')
.then(ret => {
if (ret.status === 'success') {
dispatch(getData('sports'))
dispatch(getOrUpdateData('getData', 'sports'))
ret.data.isAuthenticated = true
return dispatch(ProfileSuccess(ret.data))
}