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