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 loading
}) })
export const getData = (target, data) => dispatch => { export const getOrUpdateData = (action, target, data) => dispatch => {
if (data && data.id && isNaN(data.id)) { if (data && data.id && isNaN(data.id)) {
return dispatch(setError(target, `${target}: Incorrect id`)) return dispatch(setError(target, `${target}: Incorrect id`))
} }
return FitTrackeeApi return FitTrackeeApi[action](target, data)
.getData(target, data)
.then(ret => { .then(ret => {
if (ret.status === 'success') { if (ret.status === 'success') {
dispatch(setData(target, ret.data)) dispatch(setData(target, ret.data))
@ -45,22 +44,6 @@ export const addData = (target, data) => dispatch => FitTrackeeApi
}) })
.catch(error => dispatch(setError(`${target}: ${error}`))) .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 => { export const deleteData = (target, id) => dispatch => {
if (isNaN(id)) { if (isNaN(id)) {
return dispatch(setError(target, `${target}: Incorrect id`)) return dispatch(setError(target, `${target}: Incorrect id`))

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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