Client: refactor
This commit is contained in:
parent
775662af6a
commit
e0a0b5a090
@ -7,9 +7,7 @@ export const setGpx = gpxContent => ({
|
||||
gpxContent,
|
||||
})
|
||||
|
||||
export function addActivity(form) {
|
||||
return function(dispatch) {
|
||||
return mpwoApi
|
||||
export const addActivity = form => dispatch => mpwoApi
|
||||
.addActivity(form)
|
||||
.then(ret => {
|
||||
if (ret.status === 'created') {
|
||||
@ -19,12 +17,10 @@ export function addActivity(form) {
|
||||
}
|
||||
})
|
||||
.catch(error => dispatch(setError(`activities: ${error}`)))
|
||||
}
|
||||
}
|
||||
|
||||
export function getActivityGpx(activityId) {
|
||||
|
||||
export const getActivityGpx = activityId => dispatch => {
|
||||
if (activityId) {
|
||||
return function(dispatch) {
|
||||
return mpwoApi
|
||||
.getActivityGpx(activityId)
|
||||
.then(ret => {
|
||||
@ -36,8 +32,5 @@ export function getActivityGpx(activityId) {
|
||||
})
|
||||
.catch(error => dispatch(setError(`activities: ${error}`)))
|
||||
}
|
||||
}
|
||||
return function(dispatch) {
|
||||
dispatch(setGpx(null))
|
||||
}
|
||||
}
|
||||
|
@ -13,8 +13,7 @@ export const setError = message => ({
|
||||
message,
|
||||
})
|
||||
|
||||
export function getData(target, id = null) {
|
||||
return function(dispatch) {
|
||||
export const getData = (target, id = null) => dispatch => {
|
||||
if (id !== null && isNaN(id)) {
|
||||
return dispatch(setError(target, `${target}: Incorrect id`))
|
||||
}
|
||||
@ -29,11 +28,8 @@ export function getData(target, id = null) {
|
||||
})
|
||||
.catch(error => dispatch(setError(`${target}: ${error}`)))
|
||||
}
|
||||
}
|
||||
|
||||
export function addData(target, data) {
|
||||
return function(dispatch) {
|
||||
return mpwoApi
|
||||
export const addData = (target, data) => dispatch => mpwoApi
|
||||
.addData(target, data)
|
||||
.then(ret => {
|
||||
if (ret.status === 'created') {
|
||||
@ -43,11 +39,8 @@ export function addData(target, data) {
|
||||
}
|
||||
})
|
||||
.catch(error => dispatch(setError(`${target}: ${error}`)))
|
||||
}
|
||||
}
|
||||
|
||||
export function updateData(target, data) {
|
||||
return function(dispatch) {
|
||||
export const updateData = (target, data) => dispatch => {
|
||||
if (isNaN(data.id)) {
|
||||
return dispatch(setError(target, `${target}: Incorrect id`))
|
||||
}
|
||||
@ -62,10 +55,8 @@ export function updateData(target, data) {
|
||||
})
|
||||
.catch(error => dispatch(setError(`${target}: ${error}`)))
|
||||
}
|
||||
}
|
||||
|
||||
export function deleteData(target, id) {
|
||||
return function(dispatch) {
|
||||
export const deleteData = (target, id) => dispatch => {
|
||||
if (isNaN(id)) {
|
||||
return dispatch(setError(target, `${target}: Incorrect id`))
|
||||
}
|
||||
@ -80,4 +71,3 @@ export function deleteData(target, id) {
|
||||
})
|
||||
.catch(error => dispatch(setError(`${target}: ${error}`)))
|
||||
}
|
||||
}
|
||||
|
@ -3,33 +3,21 @@ import { history } from '../index'
|
||||
import { generateIds } from '../utils'
|
||||
|
||||
|
||||
function AuthError(message) {
|
||||
return { type: 'AUTH_ERROR', message }
|
||||
}
|
||||
const AuthError = message => ({ type: 'AUTH_ERROR', message })
|
||||
|
||||
function AuthErrors(messages) {
|
||||
return { type: 'AUTH_ERRORS', messages }
|
||||
}
|
||||
const AuthErrors = messages => ({ type: 'AUTH_ERRORS', messages })
|
||||
|
||||
function PictureError(message) {
|
||||
return { type: 'PICTURE_ERROR', message }
|
||||
}
|
||||
const PictureError = message => ({ type: 'PICTURE_ERROR', message })
|
||||
|
||||
function ProfileSuccess(message) {
|
||||
return { type: 'PROFILE_SUCCESS', message }
|
||||
}
|
||||
const ProfileSuccess = message => ({ type: 'PROFILE_SUCCESS', message })
|
||||
|
||||
function ProfileError(message) {
|
||||
return { type: 'PROFILE_ERROR', message }
|
||||
}
|
||||
const ProfileError = message => ({ type: 'PROFILE_ERROR', message })
|
||||
|
||||
function ProfileUpdateError(message) {
|
||||
return { type: 'PROFILE_UPDATE_ERROR', message }
|
||||
}
|
||||
const ProfileUpdateError = message => ({
|
||||
type: 'PROFILE_UPDATE_ERROR', message
|
||||
})
|
||||
|
||||
function initProfileFormData(user) {
|
||||
return { type: 'INIT_PROFILE_FORM', user }
|
||||
}
|
||||
const initProfileFormData = user => ({ type: 'INIT_PROFILE_FORM', user })
|
||||
|
||||
export const emptyForm = () => ({
|
||||
type: 'EMPTY_USER_FORMDATA'
|
||||
@ -47,24 +35,20 @@ export const updateProfileFormData = (target, value) => ({
|
||||
value,
|
||||
})
|
||||
|
||||
export function getProfile(dispatch) {
|
||||
return mpwoApiUser
|
||||
export const getProfile = () => dispatch => mpwoApiUser
|
||||
.getProfile()
|
||||
.then(ret => {
|
||||
if (ret.status === 'success') {
|
||||
dispatch(ProfileSuccess(ret))
|
||||
} else {
|
||||
dispatch(ProfileError(ret.message))
|
||||
return dispatch(ProfileSuccess(ret))
|
||||
}
|
||||
return dispatch(ProfileError(ret.message))
|
||||
})
|
||||
.catch(error => {
|
||||
throw error
|
||||
})
|
||||
}
|
||||
|
||||
export function register(formData) {
|
||||
return function(dispatch) {
|
||||
return mpwoApiUser
|
||||
|
||||
export const register = formData => dispatch => mpwoApiUser
|
||||
.register(
|
||||
formData.username,
|
||||
formData.email,
|
||||
@ -73,49 +57,38 @@ export function register(formData) {
|
||||
.then(ret => {
|
||||
if (ret.status === 'success') {
|
||||
window.localStorage.setItem('authToken', ret.auth_token)
|
||||
getProfile(dispatch)
|
||||
} else {
|
||||
dispatch(AuthError(ret.message))
|
||||
return dispatch(getProfile())
|
||||
}
|
||||
return dispatch(AuthError(ret.message))
|
||||
})
|
||||
.catch(error => {
|
||||
throw error
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export function login(formData) {
|
||||
return function(dispatch) {
|
||||
return mpwoApiUser
|
||||
|
||||
export const login = formData => dispatch => mpwoApiUser
|
||||
.login(formData.email, formData.password)
|
||||
.then(ret => {
|
||||
if (ret.status === 'success') {
|
||||
window.localStorage.setItem('authToken', ret.auth_token)
|
||||
getProfile(dispatch)
|
||||
} else {
|
||||
dispatch(AuthError(ret.message))
|
||||
return dispatch(getProfile())
|
||||
}
|
||||
return dispatch(AuthError(ret.message))
|
||||
})
|
||||
.catch(error => {
|
||||
throw error
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export function loadProfile() {
|
||||
export const loadProfile = () => dispatch => {
|
||||
if (window.localStorage.getItem('authToken')) {
|
||||
return function(dispatch) {
|
||||
getProfile(dispatch)
|
||||
}
|
||||
return dispatch(getProfile())
|
||||
}
|
||||
return { type: 'LOGOUT' }
|
||||
}
|
||||
|
||||
export function logout() {
|
||||
return { type: 'LOGOUT' }
|
||||
}
|
||||
export const logout = () => ({ type: 'LOGOUT' })
|
||||
|
||||
function RegisterFormControl (formData) {
|
||||
const RegisterFormControl = formData => {
|
||||
const errMsg = []
|
||||
if (formData.username.length < 3 || formData.username.length > 12) {
|
||||
errMsg.push('Username: 3 to 12 characters required.')
|
||||
@ -129,94 +102,79 @@ function RegisterFormControl (formData) {
|
||||
return errMsg
|
||||
}
|
||||
|
||||
export function handleUserFormSubmit(event, formType) {
|
||||
export const handleUserFormSubmit = (event, formType) => (
|
||||
dispatch,
|
||||
getState
|
||||
) => {
|
||||
event.preventDefault()
|
||||
return (dispatch, getState) => {
|
||||
const state = getState()
|
||||
const { formData } = state.formData
|
||||
formData.formData = state.formData.formData
|
||||
if (formType === 'Login') {
|
||||
dispatch(login(formData.formData))
|
||||
} else { // formType === 'Register'
|
||||
return dispatch(login(formData.formData))
|
||||
}
|
||||
// formType === 'Register'
|
||||
const ret = RegisterFormControl(formData.formData)
|
||||
if (ret.length === 0) {
|
||||
dispatch(register(formData.formData))
|
||||
} else {
|
||||
dispatch(AuthErrors(generateIds(ret)))
|
||||
}
|
||||
}
|
||||
return dispatch(register(formData.formData))
|
||||
}
|
||||
return dispatch(AuthErrors(generateIds(ret)))
|
||||
}
|
||||
|
||||
export function initProfileForm () {
|
||||
return (dispatch, getState) => {
|
||||
export const initProfileForm = () => (dispatch, getState) => {
|
||||
const state = getState()
|
||||
dispatch(initProfileFormData(state.user))
|
||||
}
|
||||
return dispatch(initProfileFormData(state.user))
|
||||
}
|
||||
|
||||
export function handleProfileFormSubmit(event) {
|
||||
export const handleProfileFormSubmit = event => (dispatch, getState) => {
|
||||
event.preventDefault()
|
||||
return (dispatch, getState) => {
|
||||
const state = getState()
|
||||
if (!state.formProfile.formProfile.password ===
|
||||
state.formProfile.formProfile.passwordConf) {
|
||||
dispatch(ProfileUpdateError(
|
||||
return dispatch(ProfileUpdateError(
|
||||
'Password and password confirmation don\'t match.'
|
||||
))
|
||||
} else {
|
||||
}
|
||||
return mpwoApiUser
|
||||
.updateProfile(state.formProfile.formProfile)
|
||||
.then(ret => {
|
||||
if (ret.status === 'success') {
|
||||
getProfile(dispatch)
|
||||
history.push('/profile')
|
||||
} else {
|
||||
dispatch(ProfileUpdateError(ret.message))
|
||||
dispatch(getProfile())
|
||||
return history.push('/profile')
|
||||
}
|
||||
dispatch(ProfileUpdateError(ret.message))
|
||||
})
|
||||
.catch(error => {
|
||||
throw error
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export function uploadPicture (event) {
|
||||
export const uploadPicture = event => dispatch => {
|
||||
event.preventDefault()
|
||||
const form = new FormData()
|
||||
form.append('file', event.target.picture.files[0])
|
||||
event.target.reset()
|
||||
return function(dispatch) {
|
||||
return mpwoApiUser
|
||||
.updatePicture(form)
|
||||
.then(ret => {
|
||||
if (ret.status === 'success') {
|
||||
getProfile(dispatch)
|
||||
} else {
|
||||
dispatch(PictureError(ret.message))
|
||||
return dispatch(getProfile())
|
||||
}
|
||||
return dispatch(PictureError(ret.message))
|
||||
})
|
||||
.catch(error => {
|
||||
throw error
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export function deletePicture() {
|
||||
return function(dispatch) {
|
||||
return mpwoApiUser
|
||||
export const deletePicture = () => dispatch => mpwoApiUser
|
||||
.deletePicture()
|
||||
.then(ret => {
|
||||
if (ret.status === 'success') {
|
||||
getProfile(dispatch)
|
||||
} else {
|
||||
dispatch(PictureError(ret.message))
|
||||
return dispatch(getProfile())
|
||||
}
|
||||
dispatch(PictureError(ret.message))
|
||||
})
|
||||
.catch(error => {
|
||||
throw error
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ export const thunderforestApiKey = `${
|
||||
|
||||
export const isLoggedIn = () => !!window.localStorage.authToken
|
||||
|
||||
export function generateIds(arr) {
|
||||
export const generateIds = arr => {
|
||||
let i = 0
|
||||
return arr.map(val => {
|
||||
const obj = { id: i, value: val }
|
||||
@ -17,7 +17,7 @@ export function generateIds(arr) {
|
||||
})
|
||||
}
|
||||
|
||||
export function getGeoJson(gpxContent) {
|
||||
export const getGeoJson = gpxContent => {
|
||||
let jsonData, bboxCorners
|
||||
let bounds = [[0, 0], [0, 0]]
|
||||
if (gpxContent) {
|
||||
|
Loading…
Reference in New Issue
Block a user