Client: refactor

This commit is contained in:
Sam 2018-06-04 14:55:09 +02:00
parent 017d92d7a6
commit ed2bf8898a
7 changed files with 24 additions and 24 deletions

View File

@ -117,7 +117,7 @@ export const editActivity = form => dispatch => mpwoGenericApi
export const getMoreActivities = page => dispatch => mpwoGenericApi export const getMoreActivities = page => dispatch => mpwoGenericApi
.getData('activities', null, page) .getData('activities', { page })
.then(ret => { .then(ret => {
if (ret.status === 'success') { if (ret.status === 'success') {
if (ret.data.activities.length > 0) { if (ret.data.activities.length > 0) {
@ -130,7 +130,7 @@ export const getMoreActivities = page => dispatch => mpwoGenericApi
.catch(error => dispatch(setError(`activities: ${error}`))) .catch(error => dispatch(setError(`activities: ${error}`)))
export const getMonthActivities = (start, end) => dispatch => mpwoGenericApi export const getMonthActivities = (start, end) => dispatch => mpwoGenericApi
.getData('activities', null, null, start, end, 'asc') .getData('activities', { start, end, order: 'asc' })
.then(ret => { .then(ret => {
if (ret.status === 'success') { if (ret.status === 'success') {
if (ret.data.activities.length > 0) { if (ret.data.activities.length > 0) {

View File

@ -17,12 +17,12 @@ export const setLoading = () => ({
type: 'SET_LOADING', type: 'SET_LOADING',
}) })
export const getData = (target, id = null, data = null) => dispatch => { export const getData = (target, data) => dispatch => {
if (id !== null && isNaN(id)) { if (data && data.id && isNaN(data.id)) {
return dispatch(setError(target, `${target}: Incorrect id`)) return dispatch(setError(target, `${target}: Incorrect id`))
} }
return mpwoApi return mpwoApi
.getData(target, id, 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))

View File

@ -123,7 +123,7 @@ export default connect(
}), }),
dispatch => ({ dispatch => ({
loadActivity: activityId => { loadActivity: activityId => {
dispatch(getData('activities', activityId)) dispatch(getData('activities', { id: activityId }))
}, },
onDeleteActivity: activityId => { onDeleteActivity: activityId => {
dispatch(deleteActivity(activityId)) dispatch(deleteActivity(activityId))

View File

@ -38,7 +38,7 @@ export default connect(
}), }),
dispatch => ({ dispatch => ({
loadActivity: activityId => { loadActivity: activityId => {
dispatch(getData('activities', activityId)) dispatch(getData('activities', { id: activityId }))
}, },
}) })
)(ActivityEdit) )(ActivityEdit)

View File

@ -33,7 +33,7 @@ export default connect(
}), }),
dispatch => ({ dispatch => ({
loadSport: sportId => { loadSport: sportId => {
dispatch(getData('sports', sportId)) dispatch(getData('sports', { id: sportId }))
}, },
}) })
)(AdminSports) )(AdminSports)

View File

@ -86,7 +86,7 @@ export default connect(
}), }),
dispatch => ({ dispatch => ({
loadActivities: () => { loadActivities: () => {
dispatch(getData('activities', null, 1)) dispatch(getData('activities', { page: 1 }))
dispatch(getData('records')) dispatch(getData('records'))
}, },
loadMoreActivities: page => { loadMoreActivities: page => {

View File

@ -3,28 +3,28 @@ import { apiUrl, createRequest } from '../utils'
export default class MpwoApi { export default class MpwoApi {
static getData(target, static getData(target,
id = null, data = {}) {
page = null,
start = null,
end = null,
order = null) {
let url = `${apiUrl}${target}` let url = `${apiUrl}${target}`
if (id) { if (data.id) {
url = `${url}/${id}` url = `${url}/${data.id}`
} else if (page) { } else if (data.page) {
url = `${url}?page=${page}` url = `${url}?page=${data.page}`
} }
if (start || end) { if (data.start || data.end) {
url = `${url}${ url = `${url}${
page ? '' : '?' data.page ? '' : '?'
}${ }${
start && `&from=${start}` data.start && `&from=${data.start}`
}${ }${
end && `&to=${end}` data.end && `&to=${data.end}`
}` }`
} }
if (order) { if (data.order) {
url = `${url}${(page || start || end) ? '' : '?'}${`&order=${order}`}` url = `${url}${
(data.page || data.start || data.end) ? '' : '?'
}${
`&order=${data.order}`
}`
} }
const params = { const params = {
url: url, url: url,