2020-04-28 06:33:58 +01:00
|
|
|
import api from '../api'
|
|
|
|
import { me } from '../initial_state'
|
2019-07-17 19:22:19 +01:00
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
export const GROUP_CREATE_REQUEST = 'GROUP_CREATE_REQUEST'
|
|
|
|
export const GROUP_CREATE_SUCCESS = 'GROUP_CREATE_SUCCESS'
|
|
|
|
export const GROUP_CREATE_FAIL = 'GROUP_CREATE_FAIL'
|
2019-07-17 19:22:19 +01:00
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
export const GROUP_UPDATE_REQUEST = 'GROUP_UPDATE_REQUEST'
|
|
|
|
export const GROUP_UPDATE_SUCCESS = 'GROUP_UPDATE_SUCCESS'
|
|
|
|
export const GROUP_UPDATE_FAIL = 'GROUP_UPDATE_FAIL'
|
2019-07-17 19:22:19 +01:00
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
export const GROUP_EDITOR_TITLE_CHANGE = 'GROUP_EDITOR_TITLE_CHANGE'
|
|
|
|
export const GROUP_EDITOR_DESCRIPTION_CHANGE = 'GROUP_EDITOR_DESCRIPTION_CHANGE'
|
|
|
|
export const GROUP_EDITOR_COVER_IMAGE_CHANGE = 'GROUP_EDITOR_COVER_IMAGE_CHANGE'
|
|
|
|
|
|
|
|
export const GROUP_EDITOR_RESET = 'GROUP_EDITOR_RESET'
|
|
|
|
export const GROUP_EDITOR_SETUP = 'GROUP_EDITOR_SETUP'
|
2019-07-17 19:22:19 +01:00
|
|
|
|
|
|
|
export const submit = (routerHistory) => (dispatch, getState) => {
|
2020-04-28 06:33:58 +01:00
|
|
|
if (!me) return
|
|
|
|
|
|
|
|
const groupId = getState().getIn(['group_editor', 'groupId'])
|
|
|
|
const title = getState().getIn(['group_editor', 'title'])
|
|
|
|
const description = getState().getIn(['group_editor', 'description'])
|
|
|
|
const coverImage = getState().getIn(['group_editor', 'coverImage'])
|
2019-07-17 19:22:19 +01:00
|
|
|
|
|
|
|
if (groupId === null) {
|
2020-04-28 06:33:58 +01:00
|
|
|
dispatch(create(title, description, coverImage, routerHistory))
|
2019-07-17 19:22:19 +01:00
|
|
|
} else {
|
2020-04-28 06:33:58 +01:00
|
|
|
dispatch(update(groupId, title, description, coverImage, routerHistory))
|
2019-07-17 19:22:19 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
const create = (title, description, coverImage, routerHistory) => (dispatch, getState) => {
|
|
|
|
if (!me) return
|
2019-07-17 19:22:19 +01:00
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
dispatch(createRequest())
|
2019-07-17 19:22:19 +01:00
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
const formData = new FormData()
|
|
|
|
formData.append('title', title)
|
|
|
|
formData.append('description', description)
|
2019-07-17 19:35:40 +01:00
|
|
|
|
2019-07-17 19:22:19 +01:00
|
|
|
if (coverImage !== null) {
|
2020-04-28 06:33:58 +01:00
|
|
|
formData.append('cover_image', coverImage)
|
2019-07-17 19:22:19 +01:00
|
|
|
}
|
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
api(getState).post('/api/v1/groups', formData, {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'multipart/form-data'
|
|
|
|
}
|
|
|
|
}).then(({ data }) => {
|
|
|
|
dispatch(createSuccess(data))
|
|
|
|
routerHistory.push(`/groups/${data.id}`)
|
|
|
|
}).catch(err => dispatch(createFail(err)))
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export const createRequest = (id) => ({
|
2019-07-17 19:22:19 +01:00
|
|
|
type: GROUP_CREATE_REQUEST,
|
|
|
|
id,
|
2020-04-28 06:33:58 +01:00
|
|
|
})
|
2019-07-17 19:22:19 +01:00
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
export const createSuccess = (group) => ({
|
2019-07-17 19:22:19 +01:00
|
|
|
type: GROUP_CREATE_SUCCESS,
|
|
|
|
group,
|
2020-04-28 06:33:58 +01:00
|
|
|
})
|
2019-07-17 19:22:19 +01:00
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
export const createFail = (error) => ({
|
|
|
|
type: GROUP_CREATE_FAIL,
|
|
|
|
error,
|
|
|
|
})
|
2019-07-17 19:22:19 +01:00
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
const update = (groupId, title, description, coverImage, routerHistory) => (dispatch, getState) => {
|
|
|
|
if (!me) return
|
2019-07-17 19:56:06 +01:00
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
dispatch(updateRequest())
|
2019-07-17 19:56:06 +01:00
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
const formData = new FormData()
|
|
|
|
formData.append('title', title)
|
2019-07-17 19:56:06 +01:00
|
|
|
formData.append('description', description);
|
|
|
|
|
|
|
|
if (coverImage !== null) {
|
|
|
|
formData.append('cover_image', coverImage);
|
|
|
|
}
|
2020-04-28 06:33:58 +01:00
|
|
|
|
2019-07-17 19:56:06 +01:00
|
|
|
api(getState).put(`/api/v1/groups/${groupId}`, formData, { headers: { 'Content-Type': 'multipart/form-data' } }).then(({ data }) => {
|
|
|
|
dispatch(updateSuccess(data));
|
|
|
|
routerHistory.push(`/groups/${data.id}`);
|
|
|
|
}).catch(err => dispatch(updateFail(err)));
|
2020-04-28 06:33:58 +01:00
|
|
|
};
|
|
|
|
|
2019-07-17 19:56:06 +01:00
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
export const updateRequest = (id) => ({
|
2019-07-17 19:56:06 +01:00
|
|
|
type: GROUP_UPDATE_REQUEST,
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
export const updateSuccess = (group) => ({
|
2019-07-17 19:56:06 +01:00
|
|
|
type: GROUP_UPDATE_SUCCESS,
|
|
|
|
group,
|
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
export const updateFail = (error) => ({
|
|
|
|
type: GROUP_UPDATE_FAIL,
|
|
|
|
error,
|
|
|
|
})
|
2019-07-17 19:56:06 +01:00
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
export const resetEditor = () => ({
|
|
|
|
type: GROUP_EDITOR_RESET
|
2019-07-17 19:22:19 +01:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
export const setGroup = (group) => ({
|
|
|
|
type: GROUP_EDITOR_SETUP,
|
|
|
|
group,
|
2019-07-17 19:22:19 +01:00
|
|
|
});
|
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
export const changeGroupTitle = (title) => ({
|
|
|
|
type: GROUP_EDITOR_TITLE_CHANGE,
|
|
|
|
title,
|
|
|
|
})
|
|
|
|
|
|
|
|
export const changeGroupDescription = (description) => ({
|
|
|
|
type: GROUP_EDITOR_DESCRIPTION_CHANGE,
|
|
|
|
description,
|
|
|
|
})
|
|
|
|
|
|
|
|
export const changeGroupCoverImage = (imageData) => ({
|
|
|
|
type: GROUP_EDITOR_COVER_IMAGE_CHANGE,
|
|
|
|
value: imageData,
|
|
|
|
})
|