Admin: add and delete a sport (WIP)

This commit is contained in:
Sam
2018-04-30 21:38:09 +02:00
parent b69a55362d
commit e423c355b5
10 changed files with 256 additions and 47 deletions

View File

@ -1,4 +1,5 @@
import mpwoApi from '../mwpoApi/index'
import { history } from '../index'
export const setData = (target, data) => ({
@ -30,6 +31,24 @@ export function getData(target, id = null) {
}
}
export function addData(target, data) {
return function(dispatch) {
if (isNaN(data.id)) {
return dispatch(setError(target, `${target}: Incorrect id`))
}
return mpwoApi
.addData(target, data)
.then(ret => {
if (ret.status === 'created') {
dispatch(setData(target, ret.data))
} else {
dispatch(setError(`${target}: ${ret.status}`))
}
})
.catch(error => dispatch(setError(`${target}: ${error}`)))
}
}
export function updateData(target, data) {
return function(dispatch) {
if (isNaN(data.id)) {
@ -47,3 +66,21 @@ export function updateData(target, data) {
.catch(error => dispatch(setError(`${target}: ${error}`)))
}
}
export function deleteData(target, id) {
return function(dispatch) {
if (isNaN(id)) {
return dispatch(setError(target, `${target}: Incorrect id`))
}
return mpwoApi
.deleteData(target, id)
.then(ret => {
if (ret.status === 204) {
history.push(`/admin/${target}`)
} else {
dispatch(setError(`${target}: ${ret.status}`))
}
})
.catch(error => dispatch(setError(`${target}: ${error}`)))
}
}