FitTrackee/fittrackee_client/src/actions/user.js

132 lines
3.6 KiB
JavaScript
Raw Normal View History

2018-06-12 12:51:23 +02:00
import FitTrackeeGenericApi from '../fitTrackeeApi'
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 })
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 })
2018-05-04 23:04:44 +02:00
const ProfileError = message => ({ type: 'PROFILE_ERROR', message })
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-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' }
}
export const getProfile = () => dispatch => FitTrackeeGenericApi
.getData('auth/profile')
2018-05-04 23:04:44 +02:00
.then(ret => {
if (ret.status === 'success') {
2018-06-12 13:12:18 +02:00
dispatch(getOrUpdateData('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-12 12:51:23 +02:00
export const loginOrRegister = (target, formData) => dispatch => FitTrackeeApi
.loginOrRegister(target, 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-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-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))
}
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.'
))
}
delete formData.id
2018-06-12 12:51:23 +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
})
}
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-12 12:51:23 +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
export const deletePicture = () => dispatch => FitTrackeeApi
2018-05-04 23:04:44 +02:00
.deletePicture()
.then(ret => {
2018-06-12 12:51:23 +02:00
if (ret.status === 204) {
2018-05-04 23:04:44 +02:00
return dispatch(getProfile())
}
2018-06-12 12:51:23 +02:00
return dispatch(PictureError(ret.message))
2018-05-04 23:04:44 +02:00
})
.catch(error => {
throw error
})