2018-06-12 12:51:23 +02:00
|
|
|
import FitTrackeeGenericApi from '../fitTrackeeApi'
|
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-06-12 13:12:18 +02:00
|
|
|
import { getOrUpdateData } from './index'
|
2018-01-14 12:48:52 +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 => ({
|
2019-08-28 15:35:22 +02:00
|
|
|
type: 'PROFILE_UPDATE_ERROR',
|
|
|
|
message,
|
2018-05-04 23:04:44 +02:00
|
|
|
})
|
2018-01-01 17:50:12 +01:00
|
|
|
|
2018-06-12 12:51:23 +02:00
|
|
|
export const logout = () => ({ type: 'LOGOUT' })
|
|
|
|
|
|
|
|
export const loadProfile = () => dispatch => {
|
|
|
|
if (window.localStorage.getItem('authToken')) {
|
|
|
|
return dispatch(getProfile())
|
|
|
|
}
|
|
|
|
return { type: 'LOGOUT' }
|
|
|
|
}
|
|
|
|
|
2019-08-28 15:35:22 +02:00
|
|
|
export const getProfile = () => dispatch =>
|
|
|
|
FitTrackeeGenericApi.getData('auth/profile')
|
|
|
|
.then(ret => {
|
|
|
|
if (ret.status === 'success') {
|
|
|
|
dispatch(getOrUpdateData('getData', 'sports'))
|
|
|
|
ret.data.isAuthenticated = true
|
|
|
|
return dispatch(ProfileSuccess(ret.data))
|
|
|
|
}
|
|
|
|
return dispatch(ProfileError(ret.message))
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
throw error
|
|
|
|
})
|
2018-05-04 23:04:44 +02:00
|
|
|
|
2019-08-28 15:35:22 +02:00
|
|
|
export const loginOrRegister = (target, formData) => dispatch =>
|
|
|
|
FitTrackeeApi.loginOrRegister(target, formData)
|
|
|
|
.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
|
|
|
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) {
|
2019-08-28 15:35:22 +02:00
|
|
|
errMsg.push("Password and password confirmation don't match.")
|
2018-01-01 11:10:39 +01:00
|
|
|
}
|
|
|
|
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-06-12 12:51:23 +02:00
|
|
|
if (formType === 'register') {
|
|
|
|
const ret = RegisterFormControl(formData)
|
|
|
|
if (ret.length > 0) {
|
|
|
|
return dispatch(AuthErrors(generateIds(ret)))
|
|
|
|
}
|
2018-05-04 23:04:44 +02:00
|
|
|
}
|
2018-06-12 12:51:23 +02:00
|
|
|
return dispatch(loginOrRegister(formType, formData))
|
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) {
|
2019-08-28 15:35:22 +02:00
|
|
|
return dispatch(
|
|
|
|
ProfileUpdateError("Password and password confirmation don't match.")
|
|
|
|
)
|
2017-12-25 17:45:28 +01:00
|
|
|
}
|
2018-06-14 13:39:18 +02:00
|
|
|
delete formData.id
|
2019-08-28 15:35:22 +02:00
|
|
|
return FitTrackeeGenericApi.postData('auth/profile/edit', 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()
|
2019-08-28 15:35:22 +02:00
|
|
|
return FitTrackeeGenericApi.addDataWithFile('auth/picture', 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
|
|
|
|
2019-08-28 15:35:22 +02:00
|
|
|
export const deletePicture = () => dispatch =>
|
|
|
|
FitTrackeeApi.deletePicture()
|
|
|
|
.then(ret => {
|
|
|
|
if (ret.status === 204) {
|
|
|
|
return dispatch(getProfile())
|
|
|
|
}
|
|
|
|
return dispatch(PictureError(ret.message))
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
throw error
|
|
|
|
})
|