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'
|
2020-09-11 23:27:00 +01:00
|
|
|
export const GROUP_EDITOR_PASSWORD_CHANGE = 'GROUP_EDITOR_PASSWORD_CHANGE'
|
2020-04-28 06:33:58 +01:00
|
|
|
export const GROUP_EDITOR_DESCRIPTION_CHANGE = 'GROUP_EDITOR_DESCRIPTION_CHANGE'
|
|
|
|
export const GROUP_EDITOR_COVER_IMAGE_CHANGE = 'GROUP_EDITOR_COVER_IMAGE_CHANGE'
|
2020-08-06 05:51:43 +01:00
|
|
|
export const GROUP_EDITOR_ID_CHANGE = 'GROUP_EDITOR_ID_CHANGE'
|
|
|
|
export const GROUP_EDITOR_TAGS_CHANGE = 'GROUP_EDITOR_TAGS_CHANGE'
|
|
|
|
export const GROUP_EDITOR_CATEGORY_CHANGE = 'GROUP_EDITOR_CATEGORY_CHANGE'
|
|
|
|
export const GROUP_EDITOR_IS_PRIVATE_CHANGE = 'GROUP_EDITOR_IS_PRIVATE_CHANGE'
|
|
|
|
export const GROUP_EDITOR_IS_VISIBLE_CHANGE = 'GROUP_EDITOR_IS_VISIBLE_CHANGE'
|
2020-04-28 06:33:58 +01:00
|
|
|
|
|
|
|
export const GROUP_EDITOR_RESET = 'GROUP_EDITOR_RESET'
|
|
|
|
export const GROUP_EDITOR_SETUP = 'GROUP_EDITOR_SETUP'
|
2019-07-17 19:22:19 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
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'])
|
2020-08-06 05:51:43 +01:00
|
|
|
let tags = getState().getIn(['group_editor', 'tags'], '')
|
|
|
|
tags = `${tags}`.split(',').map((t) => t.trim())
|
|
|
|
const category = getState().getIn(['group_editor', 'category'])
|
|
|
|
const isPrivate = getState().getIn(['group_editor', 'isPrivate'])
|
|
|
|
const isVisible = getState().getIn(['group_editor', 'isVisible'])
|
2020-09-11 23:27:00 +01:00
|
|
|
const slug = getState().getIn(['group_editor', 'id'], null)
|
|
|
|
const password = getState().getIn(['group_editor', 'password'], null)
|
2020-08-06 05:51:43 +01:00
|
|
|
|
|
|
|
const options = {
|
|
|
|
title,
|
2020-09-11 23:27:00 +01:00
|
|
|
password,
|
2020-08-06 05:51:43 +01:00
|
|
|
description,
|
|
|
|
coverImage,
|
|
|
|
tags,
|
|
|
|
category,
|
|
|
|
isPrivate,
|
|
|
|
isVisible,
|
|
|
|
slug,
|
|
|
|
}
|
2019-07-17 19:22:19 +01:00
|
|
|
|
|
|
|
if (groupId === null) {
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(createGroup(options, routerHistory))
|
2019-07-17 19:22:19 +01:00
|
|
|
} else {
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(updateGroup(groupId, options, routerHistory))
|
2019-07-17 19:22:19 +01:00
|
|
|
}
|
2020-08-06 05:51:43 +01:00
|
|
|
}
|
2019-07-17 19:22:19 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
const createGroup = (options, routerHistory) => (dispatch, getState) => {
|
2020-04-28 06:33:58 +01:00
|
|
|
if (!me) return
|
2019-07-17 19:22:19 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(createGroupRequest())
|
2019-07-17 19:22:19 +01:00
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
const formData = new FormData()
|
2020-08-06 05:51:43 +01:00
|
|
|
formData.append('title', options.title)
|
|
|
|
formData.append('description', options.description)
|
|
|
|
formData.append('tags', options.tags)
|
|
|
|
formData.append('is_private', options.isPrivate)
|
|
|
|
formData.append('is_visible', options.isVisible)
|
2020-09-11 23:27:00 +01:00
|
|
|
formData.append('password', options.password)
|
2020-08-06 05:51:43 +01:00
|
|
|
|
|
|
|
if (options.coverImage !== null) {
|
|
|
|
formData.append('cover_image', options.coverImage)
|
2019-07-17 19:22:19 +01:00
|
|
|
}
|
2020-09-15 02:15:45 +01:00
|
|
|
if (options.category !== null) {
|
|
|
|
formData.append('group_category_id', options.category)
|
|
|
|
}
|
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 }) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(createGroupSuccess(data))
|
2020-12-22 06:36:38 +00:00
|
|
|
console.log("pushing routerHistory:", routerHistory)
|
2020-04-28 06:33:58 +01:00
|
|
|
routerHistory.push(`/groups/${data.id}`)
|
2020-11-15 18:48:32 +00:00
|
|
|
}).catch((err) => dispatch(createGroupFail(err)))
|
2020-08-06 05:51:43 +01:00
|
|
|
}
|
2020-04-28 06:33:58 +01:00
|
|
|
|
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const createGroupRequest = (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-12-22 06:36:38 +00:00
|
|
|
const createGroupSuccess = (group) => ({
|
2019-07-17 19:22:19 +01:00
|
|
|
type: GROUP_CREATE_SUCCESS,
|
2020-12-08 04:39:13 +00:00
|
|
|
showToast: true,
|
2019-07-17 19:22:19 +01:00
|
|
|
group,
|
2020-04-28 06:33:58 +01:00
|
|
|
})
|
2019-07-17 19:22:19 +01:00
|
|
|
|
2020-12-22 06:36:38 +00:00
|
|
|
const createGroupFail = (error) => ({
|
2020-04-28 06:33:58 +01:00
|
|
|
type: GROUP_CREATE_FAIL,
|
2020-12-08 04:39:13 +00:00
|
|
|
showToast: true,
|
2020-04-28 06:33:58 +01:00
|
|
|
error,
|
|
|
|
})
|
2019-07-17 19:22:19 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
const updateGroup = (groupId, options, routerHistory) => (dispatch, getState) => {
|
2020-04-28 06:33:58 +01:00
|
|
|
if (!me) return
|
2019-07-17 19:56:06 +01:00
|
|
|
|
2020-12-23 04:42:27 +00:00
|
|
|
dispatch(updateGroupRequest())
|
2019-07-17 19:56:06 +01:00
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
const formData = new FormData()
|
2020-08-06 05:51:43 +01:00
|
|
|
formData.append('title', options.title)
|
|
|
|
formData.append('description', options.description)
|
|
|
|
formData.append('tags', options.tags)
|
|
|
|
formData.append('is_private', options.isPrivate)
|
|
|
|
formData.append('is_visible', options.isVisible)
|
2020-09-11 23:27:00 +01:00
|
|
|
formData.append('password', options.password)
|
2020-08-06 05:51:43 +01:00
|
|
|
|
2020-09-11 23:27:00 +01:00
|
|
|
if (!!options.slug) {
|
|
|
|
formData.append('slug', options.slug)
|
|
|
|
}
|
2020-08-06 05:51:43 +01:00
|
|
|
if (options.coverImage !== null) {
|
|
|
|
formData.append('cover_image', options.coverImage)
|
2019-07-17 19:56:06 +01:00
|
|
|
}
|
2020-09-15 02:15:45 +01:00
|
|
|
if (options.category !== null) {
|
|
|
|
formData.append('group_category_id', options.category)
|
|
|
|
}
|
2020-04-28 06:33:58 +01:00
|
|
|
|
2020-08-06 05:51:43 +01:00
|
|
|
api(getState).put(`/api/v1/groups/${groupId}`, formData, {
|
|
|
|
headers: {
|
2020-12-22 06:36:38 +00:00
|
|
|
'Content-Type': 'multipart/form-data',
|
2020-08-06 05:51:43 +01:00
|
|
|
}
|
|
|
|
}).then(({ data }) => {
|
2020-11-15 18:48:32 +00:00
|
|
|
dispatch(updateGroupSuccess(data))
|
2020-08-06 05:51:43 +01:00
|
|
|
routerHistory.push(`/groups/${data.id}`)
|
2020-11-15 18:48:32 +00:00
|
|
|
}).catch((err) => dispatch(updateGroupFail(err)))
|
2020-08-06 05:51:43 +01:00
|
|
|
}
|
2019-07-17 19:56:06 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const updateGroupRequest = (id) => ({
|
2019-07-17 19:56:06 +01:00
|
|
|
type: GROUP_UPDATE_REQUEST,
|
|
|
|
id,
|
2020-08-06 05:51:43 +01:00
|
|
|
})
|
2019-07-17 19:56:06 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const updateGroupSuccess = (group) => ({
|
2019-07-17 19:56:06 +01:00
|
|
|
type: GROUP_UPDATE_SUCCESS,
|
2020-12-08 04:39:13 +00:00
|
|
|
showToast: true,
|
2019-07-17 19:56:06 +01:00
|
|
|
group,
|
2020-08-06 05:51:43 +01:00
|
|
|
})
|
2019-07-17 19:56:06 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
const updateGroupFail = (error) => ({
|
2020-04-28 06:33:58 +01:00
|
|
|
type: GROUP_UPDATE_FAIL,
|
2020-12-08 04:39:13 +00:00
|
|
|
showToast: true,
|
2020-04-28 06:33:58 +01:00
|
|
|
error,
|
|
|
|
})
|
2019-07-17 19:56:06 +01:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2020-04-28 06:33:58 +01:00
|
|
|
export const resetEditor = () => ({
|
|
|
|
type: GROUP_EDITOR_RESET
|
2020-08-06 05:51:43 +01:00
|
|
|
})
|
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,
|
2020-08-06 05:51:43 +01:00
|
|
|
})
|
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,
|
|
|
|
})
|
|
|
|
|
2020-09-11 23:27:00 +01:00
|
|
|
export const changeGroupPassword = (password) => ({
|
|
|
|
type: GROUP_EDITOR_PASSWORD_CHANGE,
|
|
|
|
password,
|
|
|
|
})
|
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
export const changeGroupDescription = (description) => ({
|
|
|
|
type: GROUP_EDITOR_DESCRIPTION_CHANGE,
|
|
|
|
description,
|
|
|
|
})
|
|
|
|
|
2020-08-06 05:51:43 +01:00
|
|
|
export const changeGroupId = (idValue) => ({
|
|
|
|
type: GROUP_EDITOR_ID_CHANGE,
|
|
|
|
idValue,
|
|
|
|
})
|
|
|
|
|
|
|
|
export const changeGroupTags = (tags) => ({
|
|
|
|
type: GROUP_EDITOR_TAGS_CHANGE,
|
|
|
|
tags,
|
|
|
|
})
|
|
|
|
|
|
|
|
export const changeGroupCategory = (category) => ({
|
|
|
|
type: GROUP_EDITOR_CATEGORY_CHANGE,
|
|
|
|
category,
|
|
|
|
})
|
|
|
|
|
|
|
|
export const changeGroupIsPrivate = (isPrivate) => ({
|
|
|
|
type: GROUP_EDITOR_IS_PRIVATE_CHANGE,
|
|
|
|
isPrivate,
|
|
|
|
})
|
|
|
|
|
|
|
|
export const changeGroupIsVisible = (isVisible) => ({
|
|
|
|
type: GROUP_EDITOR_IS_VISIBLE_CHANGE,
|
|
|
|
isVisible,
|
|
|
|
})
|
|
|
|
|
2020-04-28 06:33:58 +01:00
|
|
|
export const changeGroupCoverImage = (imageData) => ({
|
|
|
|
type: GROUP_EDITOR_COVER_IMAGE_CHANGE,
|
|
|
|
value: imageData,
|
|
|
|
})
|