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-06-12 11:47:01 +02:00
|
|
|
const ProfileSuccess = profil => ({ type: 'PROFILE_SUCCESS', profil })
|
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-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-06-12 11:47:01 +02:00
|
|
|
ret.data.isAuthenticated = true
|
|
|
|
return dispatch(ProfileSuccess(ret.data))
|
2018-05-04 23:04:44 +02:00
|
|
|
}
|
|
|
|
return dispatch(ProfileError(ret.message))
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
throw error
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-06-07 14:45:43 +02:00
|
|
|
export const register = formData => dispatch => FitTrackeeApi
|
2018-06-12 11:47:01 +02:00
|
|
|
.register(formData)
|
2018-05-04 23:04:44 +02:00
|
|
|
.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-06-12 11:47:01 +02:00
|
|
|
.login(formData)
|
2018-05-04 23:04:44 +02:00
|
|
|
.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.')
|
|
|
|
}
|
2018-06-12 11:47:01 +02:00
|
|
|
if (formData.password !== formData.password_conf) {
|
2018-01-01 11:10:39 +01:00
|
|
|
errMsg.push('Password and password confirmation don\'t match.')
|
|
|
|
}
|
|
|
|
if (formData.password.length < 8) {
|
|
|
|
errMsg.push('Password: 8 characters required.')
|
|
|
|
}
|
|
|
|
return errMsg
|
|
|
|
}
|
|
|
|
|
2018-06-12 11:47:01 +02:00
|
|
|
export const handleUserFormSubmit = (formData, formType) => dispatch => {
|
2018-05-04 23:04:44 +02:00
|
|
|
if (formType === 'Login') {
|
2018-06-12 11:47:01 +02:00
|
|
|
return dispatch(login(formData))
|
2017-12-25 17:45:28 +01:00
|
|
|
}
|
2018-05-04 23:04:44 +02:00
|
|
|
// formType === 'Register'
|
2018-06-12 11:47:01 +02:00
|
|
|
const ret = RegisterFormControl(formData)
|
2018-05-04 23:04:44 +02:00
|
|
|
if (ret.length === 0) {
|
2018-06-12 11:47:01 +02:00
|
|
|
return dispatch(register(formData))
|
2018-05-04 23:04:44 +02:00
|
|
|
}
|
|
|
|
return dispatch(AuthErrors(generateIds(ret)))
|
2017-12-25 17:45:28 +01:00
|
|
|
}
|
|
|
|
|
2018-06-12 11:47:01 +02:00
|
|
|
export const handleProfileFormSubmit = formData => dispatch => {
|
|
|
|
if (!formData.password === formData.password_conf) {
|
2018-05-04 23:04:44 +02:00
|
|
|
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-06-12 11:47:01 +02:00
|
|
|
.updateProfile(formData)
|
2018-05-04 23:04:44 +02:00
|
|
|
.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
|
|
|
|
})
|