2019-09-16 14:19:21 +02:00
|
|
|
import i18next from 'i18next'
|
|
|
|
|
2018-06-07 14:45:43 +02:00
|
|
|
import FitTrackeeApi from '../fitTrackeeApi/index'
|
2018-04-30 21:38:09 +02:00
|
|
|
import { history } from '../index'
|
2018-04-29 18:23:46 +02:00
|
|
|
|
2021-02-20 16:59:31 +01:00
|
|
|
export const emptyMessages = () => ({
|
|
|
|
type: 'CLEAN_ALL_MESSAGES',
|
|
|
|
})
|
|
|
|
|
2018-04-29 18:23:46 +02:00
|
|
|
export const setData = (target, data) => ({
|
|
|
|
type: 'SET_DATA',
|
|
|
|
data,
|
|
|
|
target,
|
|
|
|
})
|
2021-02-20 16:59:31 +01:00
|
|
|
|
2020-05-02 18:00:17 +02:00
|
|
|
export const setPaginatedData = (target, data, pagination) => ({
|
|
|
|
type: 'SET_PAGINATED_DATA',
|
|
|
|
data,
|
|
|
|
pagination,
|
|
|
|
target,
|
|
|
|
})
|
2018-04-29 18:23:46 +02:00
|
|
|
|
2018-04-30 20:08:18 +02:00
|
|
|
export const setError = message => ({
|
2018-04-29 18:23:46 +02:00
|
|
|
type: 'SET_ERROR',
|
2018-04-30 20:08:18 +02:00
|
|
|
message,
|
2018-04-29 18:23:46 +02:00
|
|
|
})
|
|
|
|
|
2019-09-08 17:14:48 +02:00
|
|
|
export const setLanguage = language => ({
|
|
|
|
type: 'SET_LANGUAGE',
|
|
|
|
language,
|
|
|
|
})
|
|
|
|
|
2018-06-11 19:15:38 +02:00
|
|
|
export const setLoading = loading => ({
|
2018-05-29 20:08:34 +02:00
|
|
|
type: 'SET_LOADING',
|
2019-08-28 15:35:22 +02:00
|
|
|
loading,
|
2018-05-29 20:08:34 +02:00
|
|
|
})
|
|
|
|
|
2019-09-22 23:03:56 +02:00
|
|
|
export const updateSportsData = data => ({
|
|
|
|
type: 'UPDATE_SPORT_DATA',
|
|
|
|
data,
|
|
|
|
})
|
2020-05-01 12:12:48 +02:00
|
|
|
|
|
|
|
export const updateUsersData = data => ({
|
|
|
|
type: 'UPDATE_USER_DATA',
|
|
|
|
data,
|
|
|
|
})
|
|
|
|
|
2021-05-22 17:25:14 +02:00
|
|
|
export const getOrUpdateData =
|
|
|
|
(action, target, data, canDispatch = true) =>
|
|
|
|
dispatch => {
|
|
|
|
dispatch(setLoading(true))
|
|
|
|
if (data && data.id && target !== 'workouts' && isNaN(data.id)) {
|
|
|
|
dispatch(setLoading(false))
|
|
|
|
return dispatch(setError(`${target}|Incorrect id`))
|
|
|
|
}
|
|
|
|
dispatch(emptyMessages())
|
|
|
|
return FitTrackeeApi[action](target, data)
|
|
|
|
.then(ret => {
|
|
|
|
if (ret.status === 'success') {
|
|
|
|
if (canDispatch) {
|
|
|
|
if (target === 'users' && action === 'getData') {
|
|
|
|
return dispatch(
|
|
|
|
setPaginatedData(target, ret.data, ret.pagination)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
dispatch(setData(target, ret.data))
|
|
|
|
} else if (action === 'updateData' && target === 'sports') {
|
|
|
|
dispatch(updateSportsData(ret.data.sports[0]))
|
|
|
|
} else if (action === 'updateData' && target === 'users') {
|
|
|
|
dispatch(updateUsersData(ret.data.users[0]))
|
2020-05-02 18:00:17 +02:00
|
|
|
}
|
2021-05-22 17:25:14 +02:00
|
|
|
} else {
|
|
|
|
dispatch(setError(`${target}|${ret.message || ret.status}`))
|
2019-09-22 23:03:56 +02:00
|
|
|
}
|
2021-05-22 17:25:14 +02:00
|
|
|
dispatch(setLoading(false))
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
dispatch(setLoading(false))
|
|
|
|
dispatch(setError(`${target}|${error}`))
|
|
|
|
})
|
|
|
|
}
|
2018-04-30 21:38:09 +02:00
|
|
|
|
2019-08-28 15:35:22 +02:00
|
|
|
export const addData = (target, data) => dispatch =>
|
|
|
|
FitTrackeeApi.addData(target, data)
|
|
|
|
.then(ret => {
|
|
|
|
if (ret.status === 'created') {
|
|
|
|
history.push(`/admin/${target}`)
|
|
|
|
} else {
|
2019-09-16 18:55:58 +02:00
|
|
|
dispatch(setError(`${target}|${ret.status}`))
|
2019-08-28 15:35:22 +02:00
|
|
|
}
|
|
|
|
})
|
2019-09-16 18:55:58 +02:00
|
|
|
.catch(error => dispatch(setError(`${target}|${error}`)))
|
2018-05-04 23:04:44 +02:00
|
|
|
|
|
|
|
export const deleteData = (target, id) => dispatch => {
|
|
|
|
if (isNaN(id)) {
|
2019-09-16 18:55:58 +02:00
|
|
|
return dispatch(setError(target, `${target}|Incorrect id`))
|
2018-04-30 21:38:09 +02:00
|
|
|
}
|
2019-08-28 15:35:22 +02:00
|
|
|
return FitTrackeeApi.deleteData(target, id)
|
|
|
|
.then(ret => {
|
|
|
|
if (ret.status === 204) {
|
|
|
|
history.push(`/admin/${target}`)
|
|
|
|
} else {
|
2019-09-16 18:55:58 +02:00
|
|
|
dispatch(setError(`${target}|${ret.message || ret.status}`))
|
2019-08-28 15:35:22 +02:00
|
|
|
}
|
|
|
|
})
|
2019-09-16 18:55:58 +02:00
|
|
|
.catch(error => dispatch(setError(`${target}|${error}`)))
|
2018-04-30 21:38:09 +02:00
|
|
|
}
|
2019-09-16 14:19:21 +02:00
|
|
|
|
|
|
|
export const updateLanguage = language => dispatch => {
|
|
|
|
i18next.changeLanguage(language).then(dispatch(setLanguage(language)))
|
|
|
|
}
|