2018-06-07 14:45:43 +02:00
|
|
|
import FitTrackeeApi from '../fitTrackeeApi/user'
|
2018-01-01 16:59:46 +01:00
|
|
|
import { history } from '../index'
|
2018-01-14 12:48:52 +01:00
|
|
|
import { generateIds } from '../utils'
|
2018-05-10 20:17:03 +02:00
|
|
|
import { getData } from './index'
|
2018-01-14 12:48:52 +01:00
|
|
|
|
2017-12-25 17:45:28 +01:00
|
|
|
|
2018-05-04 23:04:44 +02:00
|
|
|
const AuthError = message => ({ type: 'AUTH_ERROR', message })
|
2017-12-25 17:45:28 +01:00
|
|
|
|
2018-05-04 23:04:44 +02:00
|
|
|
const AuthErrors = messages => ({ type: 'AUTH_ERRORS', messages })
|
2018-01-01 11:10:39 +01:00
|
|
|
|
2018-05-04 23:04:44 +02:00
|
|
|
const PictureError = message => ({ type: 'PICTURE_ERROR', message })
|
2018-01-01 21:54:03 +01:00
|
|
|
|
2018-05-04 23:04:44 +02:00
|
|
|
const ProfileSuccess = message => ({ type: 'PROFILE_SUCCESS', message })
|
2017-12-25 17:45:28 +01:00
|
|
|
|
2018-05-04 23:04:44 +02:00
|
|
|
const ProfileError = message => ({ type: 'PROFILE_ERROR', message })
|
2017-12-25 17:45:28 +01:00
|
|
|
|
2018-05-04 23:04:44 +02:00
|
|
|
const ProfileUpdateError = message => ({
|
|
|
|
type: 'PROFILE_UPDATE_ERROR', message
|
|
|
|
})
|
2018-01-01 17:50:12 +01:00
|
|
|
|
2018-05-04 23:04:44 +02:00
|
|
|
const initProfileFormData = user => ({ type: 'INIT_PROFILE_FORM', user })
|
2018-01-01 17:50:12 +01:00
|
|
|
|
2018-01-14 19:56:04 +01:00
|
|
|
export const emptyForm = () => ({
|
|
|
|
type: 'EMPTY_USER_FORMDATA'
|
|
|
|
})
|
|
|
|
|
2018-01-01 16:59:46 +01:00
|
|
|
export const handleFormChange = (target, value) => ({
|
|
|
|
type: 'UPDATE_USER_FORMDATA',
|
|
|
|
target,
|
|
|
|
value,
|
2017-12-25 17:45:28 +01:00
|
|
|
})
|
|
|
|
|
2018-01-01 16:59:46 +01:00
|
|
|
export const updateProfileFormData = (target, value) => ({
|
|
|
|
type: 'UPDATE_PROFILE_FORMDATA',
|
|
|
|
target,
|
|
|
|
value,
|
2017-12-25 17:45:28 +01:00
|
|
|
})
|
|
|
|
|
2018-06-07 14:45:43 +02:00
|
|
|
export const getProfile = () => dispatch => FitTrackeeApi
|
2018-05-04 23:04:44 +02:00
|
|
|
.getProfile()
|
|
|
|
.then(ret => {
|
|
|
|
if (ret.status === 'success') {
|
2018-05-10 20:17:03 +02:00
|
|
|
dispatch(getData('sports'))
|
2018-05-04 23:04:44 +02:00
|
|
|
return dispatch(ProfileSuccess(ret))
|
|
|
|
}
|
|
|
|
return dispatch(ProfileError(ret.message))
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
throw error
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-06-07 14:45:43 +02:00
|
|
|
export const register = formData => dispatch => FitTrackeeApi
|
2018-05-04 23:04:44 +02:00
|
|
|
.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
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-06-07 14:45:43 +02:00
|
|
|
export const login = formData => dispatch => FitTrackeeApi
|
2018-05-04 23:04:44 +02:00
|
|
|
.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
|
|
|
|
})
|
2017-12-25 17:45:28 +01:00
|
|
|
|
2018-05-04 23:04:44 +02:00
|
|
|
export const loadProfile = () => dispatch => {
|
2017-12-25 17:45:28 +01:00
|
|
|
if (window.localStorage.getItem('authToken')) {
|
2018-05-04 23:04:44 +02:00
|
|
|
return dispatch(getProfile())
|
2017-12-25 17:45:28 +01:00
|
|
|
}
|
|
|
|
return { type: 'LOGOUT' }
|
|
|
|
}
|
|
|
|
|
2018-05-04 23:04:44 +02:00
|
|
|
export const logout = () => ({ type: 'LOGOUT' })
|
2017-12-25 17:45:28 +01:00
|
|
|
|
2018-05-04 23:04:44 +02:00
|
|
|
const RegisterFormControl = formData => {
|
2018-01-01 11:10:39 +01:00
|
|
|
const errMsg = []
|
|
|
|
if (formData.username.length < 3 || formData.username.length > 12) {
|
|
|
|
errMsg.push('Username: 3 to 12 characters required.')
|
|
|
|
}
|
|
|
|
if (formData.password !== formData.passwordConf) {
|
|
|
|
errMsg.push('Password and password confirmation don\'t match.')
|
|
|
|
}
|
|
|
|
if (formData.password.length < 8) {
|
|
|
|
errMsg.push('Password: 8 characters required.')
|
|
|
|
}
|
|
|
|
return errMsg
|
|
|
|
}
|
|
|
|
|
2018-05-04 23:04:44 +02:00
|
|
|
export const handleUserFormSubmit = (event, formType) => (
|
|
|
|
dispatch,
|
|
|
|
getState
|
|
|
|
) => {
|
2017-12-25 17:45:28 +01:00
|
|
|
event.preventDefault()
|
2018-05-04 23:04:44 +02:00
|
|
|
const state = getState()
|
|
|
|
const { formData } = state.formData
|
|
|
|
formData.formData = state.formData.formData
|
|
|
|
if (formType === 'Login') {
|
|
|
|
return dispatch(login(formData.formData))
|
2017-12-25 17:45:28 +01:00
|
|
|
}
|
2018-05-04 23:04:44 +02:00
|
|
|
// formType === 'Register'
|
|
|
|
const ret = RegisterFormControl(formData.formData)
|
|
|
|
if (ret.length === 0) {
|
|
|
|
return dispatch(register(formData.formData))
|
|
|
|
}
|
|
|
|
return dispatch(AuthErrors(generateIds(ret)))
|
2017-12-25 17:45:28 +01:00
|
|
|
}
|
|
|
|
|
2018-05-04 23:04:44 +02:00
|
|
|
export const initProfileForm = () => (dispatch, getState) => {
|
|
|
|
const state = getState()
|
|
|
|
return dispatch(initProfileFormData(state.user))
|
2018-01-01 16:59:46 +01:00
|
|
|
}
|
|
|
|
|
2018-05-04 23:04:44 +02:00
|
|
|
export const handleProfileFormSubmit = event => (dispatch, getState) => {
|
2018-01-01 16:59:46 +01:00
|
|
|
event.preventDefault()
|
2018-05-04 23:04:44 +02:00
|
|
|
const state = getState()
|
|
|
|
if (!state.formProfile.formProfile.password ===
|
|
|
|
state.formProfile.formProfile.passwordConf) {
|
|
|
|
return dispatch(ProfileUpdateError(
|
|
|
|
'Password and password confirmation don\'t match.'
|
|
|
|
))
|
2017-12-25 17:45:28 +01:00
|
|
|
}
|
2018-06-07 14:45:43 +02:00
|
|
|
return FitTrackeeApi
|
2018-05-04 23:04:44 +02:00
|
|
|
.updateProfile(state.formProfile.formProfile)
|
|
|
|
.then(ret => {
|
|
|
|
if (ret.status === 'success') {
|
|
|
|
dispatch(getProfile())
|
|
|
|
return history.push('/profile')
|
|
|
|
}
|
|
|
|
dispatch(ProfileUpdateError(ret.message))
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
throw error
|
|
|
|
})
|
2017-12-25 17:45:28 +01:00
|
|
|
}
|
2018-01-01 21:54:03 +01:00
|
|
|
|
2018-05-04 23:04:44 +02:00
|
|
|
export const uploadPicture = event => dispatch => {
|
2018-01-01 21:54:03 +01:00
|
|
|
event.preventDefault()
|
|
|
|
const form = new FormData()
|
|
|
|
form.append('file', event.target.picture.files[0])
|
|
|
|
event.target.reset()
|
2018-06-07 14:45:43 +02:00
|
|
|
return FitTrackeeApi
|
2018-05-04 23:04:44 +02:00
|
|
|
.updatePicture(form)
|
2018-01-01 21:54:03 +01:00
|
|
|
.then(ret => {
|
|
|
|
if (ret.status === 'success') {
|
2018-05-04 23:04:44 +02:00
|
|
|
return dispatch(getProfile())
|
2018-01-01 21:54:03 +01:00
|
|
|
}
|
2018-05-04 23:04:44 +02:00
|
|
|
return dispatch(PictureError(ret.message))
|
2018-01-01 21:54:03 +01:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
throw error
|
|
|
|
})
|
|
|
|
}
|
2018-05-04 23:04:44 +02:00
|
|
|
|
2018-06-07 14:45:43 +02:00
|
|
|
export const deletePicture = () => dispatch => FitTrackeeApi
|
2018-05-04 23:04:44 +02:00
|
|
|
.deletePicture()
|
|
|
|
.then(ret => {
|
|
|
|
if (ret.status === 'success') {
|
|
|
|
return dispatch(getProfile())
|
|
|
|
}
|
|
|
|
dispatch(PictureError(ret.message))
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
throw error
|
|
|
|
})
|