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))
}

View File

@ -5,7 +5,7 @@ import { Link } from 'react-router-dom'
import ActivitiesFilter from './ActivitiesFilter'
import ActivitiesList from './ActivitiesList'
import { getData } from '../../actions'
import { getOrUpdateData } from '../../actions'
import { getMoreActivities } from '../../actions/activities'
@ -105,7 +105,7 @@ export default connect(
}),
dispatch => ({
loadActivities: params => {
dispatch(getData('activities', params))
dispatch(getOrUpdateData('getData', 'activities', params))
},
loadMoreActivities: params => {
dispatch(getMoreActivities(params))

View File

@ -7,7 +7,7 @@ import ActivityCharts from './ActivityCharts'
import ActivityDetails from './ActivityDetails'
import ActivityMap from './ActivityMap'
import CustomModal from './../../Others/CustomModal'
import { getData } from '../../../actions'
import { getOrUpdateData } from '../../../actions'
import { deleteActivity } from '../../../actions/activities'
class ActivityDisplay extends React.Component {
@ -124,7 +124,7 @@ export default connect(
}),
dispatch => ({
loadActivity: activityId => {
dispatch(getData('activities', { id: activityId }))
dispatch(getOrUpdateData('getData', 'activities', { id: activityId }))
},
onDeleteActivity: activityId => {
dispatch(deleteActivity(activityId))

View File

@ -2,7 +2,7 @@ import React from 'react'
import { connect } from 'react-redux'
import ActivityAddOrEdit from './ActivityAddOrEdit'
import { getData } from '../../actions'
import { getOrUpdateData } from '../../actions'
class ActivityEdit extends React.Component {
@ -38,7 +38,7 @@ export default connect(
}),
dispatch => ({
loadActivity: activityId => {
dispatch(getData('activities', { id: activityId }))
dispatch(getOrUpdateData('getData', 'activities', { id: activityId }))
},
})
)(ActivityEdit)

View File

@ -1,7 +1,7 @@
import React from 'react'
import { connect } from 'react-redux'
import { getData } from '../../../actions'
import { getOrUpdateData } from '../../../actions'
import AdminDetail from '../generic/AdminDetail'
class AdminSports extends React.Component {
@ -33,7 +33,7 @@ export default connect(
}),
dispatch => ({
loadSport: sportId => {
dispatch(getData('sports', { id: sportId }))
dispatch(getOrUpdateData('getData', 'sports', { id: sportId }))
},
})
)(AdminSports)

View File

@ -1,7 +1,7 @@
import React from 'react'
import { connect } from 'react-redux'
import { getData } from '../../../actions'
import { getOrUpdateData } from '../../../actions'
import AdminPage from '../generic/AdminPage'
class AdminSports extends React.Component {
@ -29,7 +29,7 @@ export default connect(
}),
dispatch => ({
loadSports: () => {
dispatch(getData('sports'))
dispatch(getOrUpdateData('getData', 'sports'))
},
})
)(AdminSports)

View File

@ -2,7 +2,7 @@ import React from 'react'
import { Helmet } from 'react-helmet'
import { connect } from 'react-redux'
import { deleteData, updateData } from '../../../actions/index'
import { deleteData, getOrUpdateData } from '../../../actions/index'
import { history } from '../../../index'
class AdminDetail extends React.Component {
@ -149,7 +149,7 @@ export default connect(
}
return map
}, {})
dispatch(updateData(target, data))
dispatch(getOrUpdateData('updateData', target, data))
},
})
)(AdminDetail)

View File

@ -8,7 +8,7 @@ import Calendar from './../Others/Calendar'
import Records from './Records'
import Statistics from './Statistics'
import UserStatistics from './UserStatistics'
import { getData } from '../../actions'
import { getOrUpdateData } from '../../actions'
import { getMoreActivities } from '../../actions/activities'
class DashBoard extends React.Component {
@ -98,8 +98,8 @@ export default connect(
}),
dispatch => ({
loadActivities: () => {
dispatch(getData('activities', { page: 1 }))
dispatch(getData('records'))
dispatch(getOrUpdateData('getData', 'activities', { page: 1 }))
dispatch(getOrUpdateData('getData', 'records'))
},
loadMoreActivities: page => {
dispatch(getMoreActivities({ page }))