Client: refactor

This commit is contained in:
Sam 2018-05-04 23:04:44 +02:00
parent 775662af6a
commit e0a0b5a090
4 changed files with 180 additions and 239 deletions

View File

@ -7,24 +7,20 @@ export const setGpx = gpxContent => ({
gpxContent, gpxContent,
}) })
export function addActivity(form) { export const addActivity = form => dispatch => mpwoApi
return function(dispatch) { .addActivity(form)
return mpwoApi .then(ret => {
.addActivity(form) if (ret.status === 'created') {
.then(ret => { history.push('/')
if (ret.status === 'created') { } else {
history.push('/') dispatch(setError(`activities: ${ret.message}`))
} else { }
dispatch(setError(`activities: ${ret.message}`)) })
} .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 => {
@ -35,9 +31,6 @@ 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))
} }

View File

@ -13,71 +13,61 @@ 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`))
}
return mpwoApi
.getData(target, id)
.then(ret => {
if (ret.status === 'success') {
dispatch(setData(target, ret.data))
} else {
dispatch(setError(`${target}: ${ret.status}`))
}
})
.catch(error => dispatch(setError(`${target}: ${error}`)))
} }
return mpwoApi
.getData(target, id)
.then(ret => {
if (ret.status === 'success') {
dispatch(setData(target, ret.data))
} else {
dispatch(setError(`${target}: ${ret.status}`))
}
})
.catch(error => dispatch(setError(`${target}: ${error}`)))
} }
export function addData(target, data) { export const addData = (target, data) => dispatch => mpwoApi
return function(dispatch) { .addData(target, data)
return mpwoApi .then(ret => {
.addData(target, data) if (ret.status === 'created') {
.then(ret => { history.push(`/admin/${target}`)
if (ret.status === 'created') { } else {
history.push(`/admin/${target}`) dispatch(setError(`${target}: ${ret.status}`))
} else { }
dispatch(setError(`${target}: ${ret.status}`)) })
} .catch(error => dispatch(setError(`${target}: ${error}`)))
})
.catch(error => dispatch(setError(`${target}: ${error}`))) export const updateData = (target, data) => dispatch => {
if (isNaN(data.id)) {
return dispatch(setError(target, `${target}: Incorrect id`))
} }
return mpwoApi
.updateData(target, data)
.then(ret => {
if (ret.status === 'success') {
dispatch(setData(target, ret.data))
} else {
dispatch(setError(`${target}: ${ret.status}`))
}
})
.catch(error => dispatch(setError(`${target}: ${error}`)))
} }
export function updateData(target, data) { export const deleteData = (target, id) => dispatch => {
return function(dispatch) { if (isNaN(id)) {
if (isNaN(data.id)) { return dispatch(setError(target, `${target}: Incorrect id`))
return dispatch(setError(target, `${target}: Incorrect id`))
}
return mpwoApi
.updateData(target, data)
.then(ret => {
if (ret.status === 'success') {
dispatch(setData(target, ret.data))
} else {
dispatch(setError(`${target}: ${ret.status}`))
}
})
.catch(error => dispatch(setError(`${target}: ${error}`)))
} }
} return mpwoApi
.deleteData(target, id)
export function deleteData(target, id) { .then(ret => {
return function(dispatch) { if (ret.status === 204) {
if (isNaN(id)) { history.push(`/admin/${target}`)
return dispatch(setError(target, `${target}: Incorrect id`)) } else {
dispatch(setError(`${target}: ${ret.status}`))
} }
return mpwoApi })
.deleteData(target, id) .catch(error => dispatch(setError(`${target}: ${error}`)))
.then(ret => {
if (ret.status === 204) {
history.push(`/admin/${target}`)
} else {
dispatch(setError(`${target}: ${ret.status}`))
}
})
.catch(error => dispatch(setError(`${target}: ${error}`)))
}
} }

View File

@ -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,75 +35,60 @@ 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') { return dispatch(ProfileSuccess(ret))
dispatch(ProfileSuccess(ret))
} else {
dispatch(ProfileError(ret.message))
}
})
.catch(error => {
throw error
})
}
export function register(formData) {
return function(dispatch) {
return mpwoApiUser
.register(
formData.username,
formData.email,
formData.password,
formData.passwordConf)
.then(ret => {
if (ret.status === 'success') {
window.localStorage.setItem('authToken', ret.auth_token)
getProfile(dispatch)
} else {
dispatch(AuthError(ret.message))
}
})
.catch(error => {
throw error
})
}
}
export function login(formData) {
return function(dispatch) {
return 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))
}
})
.catch(error => {
throw error
})
}
}
export function loadProfile() {
if (window.localStorage.getItem('authToken')) {
return function(dispatch) {
getProfile(dispatch)
} }
return dispatch(ProfileError(ret.message))
})
.catch(error => {
throw error
})
export const register = formData => dispatch => mpwoApiUser
.register(
formData.username,
formData.email,
formData.password,
formData.passwordConf)
.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 login = formData => dispatch => mpwoApiUser
.login(formData.email, formData.password)
.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' } 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') { return dispatch(login(formData.formData))
dispatch(login(formData.formData))
} else { // formType === 'Register'
const ret = RegisterFormControl(formData.formData)
if (ret.length === 0) {
dispatch(register(formData.formData))
} else {
dispatch(AuthErrors(generateIds(ret)))
}
}
} }
// formType === 'Register'
const ret = RegisterFormControl(formData.formData)
if (ret.length === 0) {
return dispatch(register(formData.formData))
}
return dispatch(AuthErrors(generateIds(ret)))
} }
export function initProfileForm () { export const initProfileForm = () => (dispatch, getState) => {
return (dispatch, getState) => { const state = getState()
const state = getState() return dispatch(initProfileFormData(state.user))
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) { return dispatch(ProfileUpdateError(
dispatch(ProfileUpdateError( 'Password and password confirmation don\'t match.'
'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))
}
})
.catch(error => {
throw error
})
}
} }
}
export function uploadPicture (event) {
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))
}
})
.catch(error => {
throw error
})
}
}
export function deletePicture() {
return function(dispatch) {
return mpwoApiUser return mpwoApiUser
.deletePicture() .updateProfile(state.formProfile.formProfile)
.then(ret => { .then(ret => {
if (ret.status === 'success') { if (ret.status === 'success') {
getProfile(dispatch) dispatch(getProfile())
} else { return history.push('/profile')
dispatch(PictureError(ret.message))
} }
dispatch(ProfileUpdateError(ret.message))
}) })
.catch(error => { .catch(error => {
throw error throw error
}) })
}
} }
export const uploadPicture = event => dispatch => {
event.preventDefault()
const form = new FormData()
form.append('file', event.target.picture.files[0])
event.target.reset()
return mpwoApiUser
.updatePicture(form)
.then(ret => {
if (ret.status === 'success') {
return dispatch(getProfile())
}
return dispatch(PictureError(ret.message))
})
.catch(error => {
throw error
})
}
export const deletePicture = () => dispatch => mpwoApiUser
.deletePicture()
.then(ret => {
if (ret.status === 'success') {
return dispatch(getProfile())
}
dispatch(PictureError(ret.message))
})
.catch(error => {
throw error
})

View File

@ -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) {