From ed2bf8898ae4b1aa4dcba2e89726323e3b0df37b Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 4 Jun 2018 14:55:09 +0200 Subject: [PATCH] Client: refactor --- mpwo_client/src/actions/activities.js | 4 +-- mpwo_client/src/actions/index.js | 6 ++-- .../Activities/ActivityDisplay/index.jsx | 2 +- .../components/Activities/ActivityEdit.jsx | 2 +- .../components/Admin/Sports/AdminSport.jsx | 2 +- .../src/components/Dashboard/index.jsx | 2 +- mpwo_client/src/mwpoApi/index.js | 30 +++++++++---------- 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/mpwo_client/src/actions/activities.js b/mpwo_client/src/actions/activities.js index 3a334fe4..df5c8306 100644 --- a/mpwo_client/src/actions/activities.js +++ b/mpwo_client/src/actions/activities.js @@ -117,7 +117,7 @@ export const editActivity = form => dispatch => mpwoGenericApi export const getMoreActivities = page => dispatch => mpwoGenericApi - .getData('activities', null, page) + .getData('activities', { page }) .then(ret => { if (ret.status === 'success') { if (ret.data.activities.length > 0) { @@ -130,7 +130,7 @@ export const getMoreActivities = page => dispatch => mpwoGenericApi .catch(error => dispatch(setError(`activities: ${error}`))) export const getMonthActivities = (start, end) => dispatch => mpwoGenericApi - .getData('activities', null, null, start, end, 'asc') + .getData('activities', { start, end, order: 'asc' }) .then(ret => { if (ret.status === 'success') { if (ret.data.activities.length > 0) { diff --git a/mpwo_client/src/actions/index.js b/mpwo_client/src/actions/index.js index 7730976b..2564c2bb 100644 --- a/mpwo_client/src/actions/index.js +++ b/mpwo_client/src/actions/index.js @@ -17,12 +17,12 @@ export const setLoading = () => ({ type: 'SET_LOADING', }) -export const getData = (target, id = null, data = null) => dispatch => { - if (id !== null && isNaN(id)) { +export const getData = (target, data) => dispatch => { + if (data && data.id && isNaN(data.id)) { return dispatch(setError(target, `${target}: Incorrect id`)) } return mpwoApi - .getData(target, id, data) + .getData(target, data) .then(ret => { if (ret.status === 'success') { dispatch(setData(target, ret.data)) diff --git a/mpwo_client/src/components/Activities/ActivityDisplay/index.jsx b/mpwo_client/src/components/Activities/ActivityDisplay/index.jsx index 2b60be40..902ab23c 100644 --- a/mpwo_client/src/components/Activities/ActivityDisplay/index.jsx +++ b/mpwo_client/src/components/Activities/ActivityDisplay/index.jsx @@ -123,7 +123,7 @@ export default connect( }), dispatch => ({ loadActivity: activityId => { - dispatch(getData('activities', activityId)) + dispatch(getData('activities', { id: activityId })) }, onDeleteActivity: activityId => { dispatch(deleteActivity(activityId)) diff --git a/mpwo_client/src/components/Activities/ActivityEdit.jsx b/mpwo_client/src/components/Activities/ActivityEdit.jsx index cf030649..9fd57b0d 100644 --- a/mpwo_client/src/components/Activities/ActivityEdit.jsx +++ b/mpwo_client/src/components/Activities/ActivityEdit.jsx @@ -38,7 +38,7 @@ export default connect( }), dispatch => ({ loadActivity: activityId => { - dispatch(getData('activities', activityId)) + dispatch(getData('activities', { id: activityId })) }, }) )(ActivityEdit) diff --git a/mpwo_client/src/components/Admin/Sports/AdminSport.jsx b/mpwo_client/src/components/Admin/Sports/AdminSport.jsx index 495a017d..ff794d65 100644 --- a/mpwo_client/src/components/Admin/Sports/AdminSport.jsx +++ b/mpwo_client/src/components/Admin/Sports/AdminSport.jsx @@ -33,7 +33,7 @@ export default connect( }), dispatch => ({ loadSport: sportId => { - dispatch(getData('sports', sportId)) + dispatch(getData('sports', { id: sportId })) }, }) )(AdminSports) diff --git a/mpwo_client/src/components/Dashboard/index.jsx b/mpwo_client/src/components/Dashboard/index.jsx index 80e06468..47c2ec45 100644 --- a/mpwo_client/src/components/Dashboard/index.jsx +++ b/mpwo_client/src/components/Dashboard/index.jsx @@ -86,7 +86,7 @@ export default connect( }), dispatch => ({ loadActivities: () => { - dispatch(getData('activities', null, 1)) + dispatch(getData('activities', { page: 1 })) dispatch(getData('records')) }, loadMoreActivities: page => { diff --git a/mpwo_client/src/mwpoApi/index.js b/mpwo_client/src/mwpoApi/index.js index a7c1c7a2..e1d0c38e 100644 --- a/mpwo_client/src/mwpoApi/index.js +++ b/mpwo_client/src/mwpoApi/index.js @@ -3,28 +3,28 @@ import { apiUrl, createRequest } from '../utils' export default class MpwoApi { static getData(target, - id = null, - page = null, - start = null, - end = null, - order = null) { + data = {}) { let url = `${apiUrl}${target}` - if (id) { - url = `${url}/${id}` - } else if (page) { - url = `${url}?page=${page}` + if (data.id) { + url = `${url}/${data.id}` + } else if (data.page) { + url = `${url}?page=${data.page}` } - if (start || end) { + if (data.start || data.end) { url = `${url}${ - page ? '' : '?' + data.page ? '' : '?' }${ - start && `&from=${start}` + data.start && `&from=${data.start}` }${ - end && `&to=${end}` + data.end && `&to=${data.end}` }` } - if (order) { - url = `${url}${(page || start || end) ? '' : '?'}${`&order=${order}`}` + if (data.order) { + url = `${url}${ + (data.page || data.start || data.end) ? '' : '?' + }${ + `&order=${data.order}` + }` } const params = { url: url,