From ca8a89915ec402cf387540fbef4326583e8e5328 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 12 Jun 2018 12:51:23 +0200 Subject: [PATCH] Client: refactor --- fittrackee_api/fittrackee_api/users/auth.py | 5 +- fittrackee_client/src/actions/activities.js | 17 +++-- fittrackee_client/src/actions/stats.js | 6 +- fittrackee_client/src/actions/user.js | 68 +++++++------------ fittrackee_client/src/components/App.jsx | 8 +-- .../src/components/User/Form.jsx | 8 ++- .../src/fitTrackeeApi/activities.js | 40 ----------- fittrackee_client/src/fitTrackeeApi/index.js | 19 ++++++ fittrackee_client/src/fitTrackeeApi/stats.js | 18 ----- fittrackee_client/src/fitTrackeeApi/user.js | 43 +----------- 10 files changed, 69 insertions(+), 163 deletions(-) delete mode 100644 fittrackee_client/src/fitTrackeeApi/activities.js delete mode 100644 fittrackee_client/src/fitTrackeeApi/stats.js diff --git a/fittrackee_api/fittrackee_api/users/auth.py b/fittrackee_api/fittrackee_api/users/auth.py index 4418036c..6f6faaf3 100644 --- a/fittrackee_api/fittrackee_api/users/auth.py +++ b/fittrackee_api/fittrackee_api/users/auth.py @@ -287,10 +287,9 @@ def del_picture(user_id): db.session.commit() response_object = { - 'status': 'success', - 'message': 'User picture delete.' + 'status': 'no content' } - return jsonify(response_object), 200 + return jsonify(response_object), 204 except (exc.IntegrityError, ValueError) as e: db.session.rollback() diff --git a/fittrackee_client/src/actions/activities.js b/fittrackee_client/src/actions/activities.js index 0cc90cb9..a166891b 100644 --- a/fittrackee_client/src/actions/activities.js +++ b/fittrackee_client/src/actions/activities.js @@ -1,7 +1,6 @@ import { parse } from 'date-fns' import FitTrackeeGenericApi from '../fitTrackeeApi' -import FitTrackeeApi from '../fitTrackeeApi/activities' import { history } from '../index' import { formatChartData } from '../utils' import { setError, setLoading } from './index' @@ -27,8 +26,8 @@ export const setChartData = chartData => ({ chartData, }) -export const addActivity = form => dispatch => FitTrackeeApi - .addActivity(form) +export const addActivity = form => dispatch => FitTrackeeGenericApi + .addDataWithFile('activities', form) .then(ret => { if (ret.status === 'created') { if (ret.data.activities.length === 0) { @@ -48,8 +47,8 @@ export const addActivity = form => dispatch => FitTrackeeApi .catch(error => dispatch(setError(`activities: ${error}`))) -export const addActivityWithoutGpx = form => dispatch => FitTrackeeApi - .addActivityWithoutGpx(form) +export const addActivityWithoutGpx = form => dispatch => FitTrackeeGenericApi + .addData('activities/no_gpx', form) .then(ret => { if (ret.status === 'created') { dispatch(loadProfile()) @@ -63,8 +62,8 @@ export const addActivityWithoutGpx = form => dispatch => FitTrackeeApi export const getActivityGpx = activityId => dispatch => { if (activityId) { - return FitTrackeeApi - .getActivityGpx(activityId) + return FitTrackeeGenericApi + .getData(`activities/${activityId}/gpx`) .then(ret => { if (ret.status === 'success') { dispatch(setGpx(ret.data.gpx)) @@ -80,8 +79,8 @@ export const getActivityGpx = activityId => dispatch => { export const getActivityChartData = activityId => dispatch => { if (activityId) { - return FitTrackeeApi - .getActivityChartData(activityId) + return FitTrackeeGenericApi + .getData(`activities/${activityId}/chart_data`) .then(ret => { if (ret.status === 'success') { dispatch(setChartData(formatChartData(ret.data.chart_data))) diff --git a/fittrackee_client/src/actions/stats.js b/fittrackee_client/src/actions/stats.js index 4fe2fee3..ef2c7c6b 100644 --- a/fittrackee_client/src/actions/stats.js +++ b/fittrackee_client/src/actions/stats.js @@ -1,8 +1,8 @@ -import FitTrackeeApi from '../fitTrackeeApi/stats' +import FitTrackeeGenericApi from '../fitTrackeeApi' import { setData, setError } from './index' -export const getStats = (userId, type, data) => dispatch => FitTrackeeApi - .getStats(userId, type, data) +export const getStats = (userId, type, data) => dispatch => FitTrackeeGenericApi + .getData(`stats/${userId}/${type}`, data) .then(ret => { if (ret.status === 'success') { dispatch(setData('statistics', ret.data)) diff --git a/fittrackee_client/src/actions/user.js b/fittrackee_client/src/actions/user.js index d90ca41d..a59317b1 100644 --- a/fittrackee_client/src/actions/user.js +++ b/fittrackee_client/src/actions/user.js @@ -1,3 +1,4 @@ +import FitTrackeeGenericApi from '../fitTrackeeApi' import FitTrackeeApi from '../fitTrackeeApi/user' import { history } from '../index' import { generateIds } from '../utils' @@ -18,8 +19,17 @@ const ProfileUpdateError = message => ({ type: 'PROFILE_UPDATE_ERROR', message }) -export const getProfile = () => dispatch => FitTrackeeApi - .getProfile() +export const logout = () => ({ type: 'LOGOUT' }) + +export const loadProfile = () => dispatch => { + if (window.localStorage.getItem('authToken')) { + return dispatch(getProfile()) + } + return { type: 'LOGOUT' } +} + +export const getProfile = () => dispatch => FitTrackeeGenericApi + .getData('auth/profile') .then(ret => { if (ret.status === 'success') { dispatch(getData('sports')) @@ -32,9 +42,8 @@ export const getProfile = () => dispatch => FitTrackeeApi throw error }) - -export const register = formData => dispatch => FitTrackeeApi - .register(formData) +export const loginOrRegister = (target, formData) => dispatch => FitTrackeeApi + .loginOrRegister(target, formData) .then(ret => { if (ret.status === 'success') { window.localStorage.setItem('authToken', ret.auth_token) @@ -46,29 +55,6 @@ export const register = formData => dispatch => FitTrackeeApi throw error }) - -export const login = formData => dispatch => FitTrackeeApi - .login(formData) - .then(ret => { - if (ret.status === 'success') { - window.localStorage.setItem('authToken', ret.auth_token) - return dispatch(getProfile()) - } - return dispatch(AuthError(ret.message)) - }) - .catch(error => { - throw error - }) - -export const loadProfile = () => dispatch => { - if (window.localStorage.getItem('authToken')) { - return dispatch(getProfile()) - } - return { type: 'LOGOUT' } -} - -export const logout = () => ({ type: 'LOGOUT' }) - const RegisterFormControl = formData => { const errMsg = [] if (formData.username.length < 3 || formData.username.length > 12) { @@ -84,15 +70,13 @@ const RegisterFormControl = formData => { } export const handleUserFormSubmit = (formData, formType) => dispatch => { - if (formType === 'Login') { - return dispatch(login(formData)) + if (formType === 'register') { + const ret = RegisterFormControl(formData) + if (ret.length > 0) { + return dispatch(AuthErrors(generateIds(ret))) + } } - // formType === 'Register' - const ret = RegisterFormControl(formData) - if (ret.length === 0) { - return dispatch(register(formData)) - } - return dispatch(AuthErrors(generateIds(ret))) + return dispatch(loginOrRegister(formType, formData)) } export const handleProfileFormSubmit = formData => dispatch => { @@ -101,8 +85,8 @@ export const handleProfileFormSubmit = formData => dispatch => { 'Password and password confirmation don\'t match.' )) } - return FitTrackeeApi - .updateProfile(formData) + return FitTrackeeGenericApi + .postData('auth/profile/edit', formData) .then(ret => { if (ret.status === 'success') { dispatch(getProfile()) @@ -120,8 +104,8 @@ export const uploadPicture = event => dispatch => { const form = new FormData() form.append('file', event.target.picture.files[0]) event.target.reset() - return FitTrackeeApi - .updatePicture(form) + return FitTrackeeGenericApi + .addDataWithFile('auth/picture', form) .then(ret => { if (ret.status === 'success') { return dispatch(getProfile()) @@ -136,10 +120,10 @@ export const uploadPicture = event => dispatch => { export const deletePicture = () => dispatch => FitTrackeeApi .deletePicture() .then(ret => { - if (ret.status === 'success') { + if (ret.status === 204) { return dispatch(getProfile()) } - dispatch(PictureError(ret.message)) + return dispatch(PictureError(ret.message)) }) .catch(error => { throw error diff --git a/fittrackee_client/src/components/App.jsx b/fittrackee_client/src/components/App.jsx index 0308a795..016c7bae 100644 --- a/fittrackee_client/src/components/App.jsx +++ b/fittrackee_client/src/components/App.jsx @@ -43,7 +43,7 @@ export default class App extends React.Component { ) : ( ) )} @@ -55,7 +55,7 @@ export default class App extends React.Component { ) : ( ) )} @@ -68,7 +68,7 @@ export default class App extends React.Component { ) : ( ) )} @@ -80,7 +80,7 @@ export default class App extends React.Component { ) : ( ) )} diff --git a/fittrackee_client/src/components/User/Form.jsx b/fittrackee_client/src/components/User/Form.jsx index a4fd16cc..421fd921 100644 --- a/fittrackee_client/src/components/User/Form.jsx +++ b/fittrackee_client/src/components/User/Form.jsx @@ -7,7 +7,9 @@ export default function Form (props) { FitTrackee - {props.formType} -

{props.formType}

+

+ {`${props.formType.charAt(0).toUpperCase()}${props.formType.slice(1)}`} +

@@ -16,7 +18,7 @@ export default function Form (props) {
props.handleUserFormSubmit(event, props.formType)} > - {props.formType === 'Register' && + {props.formType === 'register' &&
- {props.formType === 'Register' && + {props.formType === 'register' &&
0) { - url += '?' - Object.keys(data).map(key => url += `&${key}=${data[key]}`) - } - const params = { - url: url, - method: 'GET', - } - return createRequest(params) - } - -} diff --git a/fittrackee_client/src/fitTrackeeApi/user.js b/fittrackee_client/src/fitTrackeeApi/user.js index 9a08b9b3..c589465d 100644 --- a/fittrackee_client/src/fitTrackeeApi/user.js +++ b/fittrackee_client/src/fitTrackeeApi/user.js @@ -2,9 +2,9 @@ import { apiUrl, createRequest } from '../utils' export default class FitTrackeeApi { - static login(data) { + static loginOrRegister(target, data) { const params = { - url: `${apiUrl}auth/login`, + url: `${apiUrl}auth/${target}`, method: 'POST', noAuthorization: true, body: data, @@ -13,45 +13,6 @@ export default class FitTrackeeApi { return createRequest(params) } - static register(data) { - const params = { - url: `${apiUrl}auth/register`, - method: 'POST', - noAuthorization: true, - body: data, - type: 'application/json', - } - return createRequest(params) - } - - static getProfile() { - const params = { - url: `${apiUrl}auth/profile`, - method: 'GET', - type: 'application/json', - } - return createRequest(params) - } - - static updateProfile(data) { - const params = { - url: `${apiUrl}auth/profile/edit`, - method: 'POST', - body: data, - type: 'application/json', - } - return createRequest(params) - } - - static updatePicture(form) { - const params = { - url: `${apiUrl}auth/picture`, - method: 'POST', - body: form, - } - return createRequest(params) - } - static deletePicture() { const params = { url: `${apiUrl}auth/picture`,