2020-04-28 06:33:58 +01:00
|
|
|
import { Map as ImmutableMap } from 'immutable'
|
2019-07-17 19:22:19 +01:00
|
|
|
import {
|
2020-04-28 06:33:58 +01:00
|
|
|
GROUP_CREATE_REQUEST,
|
|
|
|
GROUP_CREATE_FAIL,
|
|
|
|
GROUP_CREATE_SUCCESS,
|
|
|
|
GROUP_UPDATE_REQUEST,
|
|
|
|
GROUP_UPDATE_FAIL,
|
|
|
|
GROUP_UPDATE_SUCCESS,
|
|
|
|
GROUP_EDITOR_RESET,
|
|
|
|
GROUP_EDITOR_SETUP,
|
|
|
|
GROUP_EDITOR_TITLE_CHANGE,
|
|
|
|
GROUP_EDITOR_DESCRIPTION_CHANGE,
|
|
|
|
GROUP_EDITOR_COVER_IMAGE_CHANGE,
|
2020-08-06 05:51:43 +01:00
|
|
|
GROUP_EDITOR_ID_CHANGE,
|
|
|
|
GROUP_EDITOR_TAGS_CHANGE,
|
|
|
|
GROUP_EDITOR_CATEGORY_CHANGE,
|
|
|
|
GROUP_EDITOR_IS_PRIVATE_CHANGE,
|
|
|
|
GROUP_EDITOR_IS_VISIBLE_CHANGE,
|
2020-04-28 06:33:58 +01:00
|
|
|
} from '../actions/group_editor'
|
2020-08-06 05:51:43 +01:00
|
|
|
import slugify from '../utils/slugify'
|
2019-07-17 19:22:19 +01:00
|
|
|
|
|
|
|
const initialState = ImmutableMap({
|
2020-04-28 06:33:58 +01:00
|
|
|
groupId: null,
|
|
|
|
isSubmitting: false,
|
|
|
|
isChanged: false,
|
|
|
|
title: '',
|
|
|
|
description: '',
|
2020-08-06 05:51:43 +01:00
|
|
|
id: '',
|
|
|
|
tags: '',
|
|
|
|
category: '',
|
2020-04-28 06:33:58 +01:00
|
|
|
coverImage: null,
|
2020-08-06 05:51:43 +01:00
|
|
|
isPrivate: false,
|
|
|
|
isVisible: true,
|
2020-04-28 06:33:58 +01:00
|
|
|
})
|
2019-07-17 19:22:19 +01:00
|
|
|
|
|
|
|
export default function groupEditorReducer(state = initialState, action) {
|
2020-04-28 06:33:58 +01:00
|
|
|
switch(action.type) {
|
|
|
|
case GROUP_EDITOR_RESET:
|
|
|
|
return initialState
|
|
|
|
case GROUP_EDITOR_SETUP:
|
2020-08-06 05:51:43 +01:00
|
|
|
let tags
|
|
|
|
try {
|
|
|
|
tags = action.group.get('tags').toJS().join(', ')
|
|
|
|
} catch (error) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
return state.withMutations((map) => {
|
2020-04-28 06:33:58 +01:00
|
|
|
map.set('groupId', action.group.get('id'))
|
|
|
|
map.set('title', action.group.get('title'))
|
|
|
|
map.set('description', action.group.get('description'))
|
2020-08-06 05:51:43 +01:00
|
|
|
map.set('tags', tags)
|
|
|
|
map.set('isPrivate', action.group.get('is_private'))
|
|
|
|
map.set('isVisible', action.group.get('is_visible'))
|
|
|
|
map.set('id', action.group.get('slug'))
|
|
|
|
map.set('category', action.group.get('category'))
|
2020-04-28 06:33:58 +01:00
|
|
|
map.set('isSubmitting', false)
|
|
|
|
})
|
|
|
|
case GROUP_EDITOR_TITLE_CHANGE:
|
2020-08-06 05:51:43 +01:00
|
|
|
return state.withMutations((map) => {
|
2020-04-28 06:33:58 +01:00
|
|
|
map.set('title', action.title)
|
|
|
|
map.set('isChanged', true)
|
|
|
|
})
|
|
|
|
case GROUP_EDITOR_DESCRIPTION_CHANGE:
|
2020-08-06 05:51:43 +01:00
|
|
|
return state.withMutations((map) => {
|
2020-04-28 06:33:58 +01:00
|
|
|
map.set('description', action.description)
|
|
|
|
map.set('isChanged', true)
|
|
|
|
})
|
|
|
|
case GROUP_EDITOR_COVER_IMAGE_CHANGE:
|
2020-08-06 05:51:43 +01:00
|
|
|
return state.withMutations((map) => {
|
2020-04-28 06:33:58 +01:00
|
|
|
map.set('coverImage', action.value)
|
|
|
|
map.set('isChanged', true)
|
|
|
|
})
|
2020-08-06 05:51:43 +01:00
|
|
|
case GROUP_EDITOR_ID_CHANGE:
|
|
|
|
return state.withMutations((map) => {
|
|
|
|
map.set('id', slugify(action.idValue || ''))
|
|
|
|
map.set('isChanged', true)
|
|
|
|
})
|
|
|
|
case GROUP_EDITOR_TAGS_CHANGE:
|
|
|
|
return state.withMutations((map) => {
|
|
|
|
map.set('tags', action.tags)
|
|
|
|
map.set('isChanged', true)
|
|
|
|
})
|
|
|
|
case GROUP_EDITOR_CATEGORY_CHANGE:
|
|
|
|
return state.withMutations((map) => {
|
|
|
|
map.set('category', action.category)
|
|
|
|
map.set('isChanged', true)
|
|
|
|
})
|
|
|
|
case GROUP_EDITOR_IS_PRIVATE_CHANGE:
|
|
|
|
return state.withMutations((map) => {
|
|
|
|
map.set('isPrivate', action.isPrivate)
|
|
|
|
map.set('isChanged', true)
|
|
|
|
})
|
|
|
|
case GROUP_EDITOR_IS_VISIBLE_CHANGE:
|
|
|
|
return state.withMutations((map) => {
|
|
|
|
map.set('isVisible', action.isVisible)
|
|
|
|
map.set('isChanged', true)
|
|
|
|
})
|
2020-04-28 06:33:58 +01:00
|
|
|
case GROUP_CREATE_REQUEST:
|
|
|
|
case GROUP_UPDATE_REQUEST:
|
2020-08-06 05:51:43 +01:00
|
|
|
return state.withMutations((map) => {
|
2020-04-28 06:33:58 +01:00
|
|
|
map.set('isSubmitting', true)
|
|
|
|
map.set('isChanged', false)
|
|
|
|
})
|
|
|
|
case GROUP_CREATE_FAIL:
|
|
|
|
case GROUP_UPDATE_FAIL:
|
|
|
|
return state.set('isSubmitting', false)
|
|
|
|
case GROUP_CREATE_SUCCESS:
|
|
|
|
case GROUP_UPDATE_SUCCESS:
|
2020-08-06 05:51:43 +01:00
|
|
|
return state.withMutations((map) => {
|
2020-04-28 06:33:58 +01:00
|
|
|
map.set('isSubmitting', false)
|
|
|
|
map.set('groupId', action.group.id)
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
}
|