Skip to content

Commit cce034b

Browse files
committed
set / unset categories && refactor
1 parent 72f454d commit cce034b

File tree

22 files changed

+520
-46
lines changed

22 files changed

+520
-46
lines changed

components/CategoriesCell/index.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,40 @@ import {
1919
AddIcon,
2020
AddText,
2121
} from './styles'
22+
2223
import { ICON_ASSETS } from '../../config'
2324

2425
// import { inject, observer } from 'mobx-react'
2526
// import Link from 'next/link'
2627

27-
const CategoriesList = ({ categories, communityId, onDelete }) => {
28-
return (
29-
<CategoryWrapper>
30-
{categories.map(c => (
31-
<CategoryTag
32-
key={shortid.generate()}
33-
onClick={onDelete.bind(this, communityId, c)}
34-
>
35-
{c.title}
36-
<DeleteCross>x</DeleteCross>
37-
</CategoryTag>
38-
))}
39-
</CategoryWrapper>
40-
)
41-
}
28+
const CategoriesList = ({ categories, communityId, onDelete }) => (
29+
<CategoryWrapper>
30+
{categories.map(c => (
31+
<CategoryTag
32+
key={shortid.generate()}
33+
onClick={onDelete.bind(this, communityId, c)}
34+
>
35+
{c.title}
36+
<DeleteCross>x</DeleteCross>
37+
</CategoryTag>
38+
))}
39+
</CategoryWrapper>
40+
)
4241

4342
class CategoriesCell extends React.Component {
4443
componentDidMount() {}
4544

4645
componentWillUnmount() {}
4746

4847
render() {
49-
const { communityId, categories, onDelete } = this.props
48+
const { communityId, categories, onDelete, onAdd } = this.props
5049

5150
return (
5251
<div>
5352
{R.isEmpty(categories) ? (
5453
<AddWrapper>
5554
<AddIcon src={`${ICON_ASSETS}/cmd/plus.svg`} />
56-
<AddText>添加</AddText>
55+
<AddText onClick={onAdd.bind(this, communityId, [])}>添加</AddText>
5756
</AddWrapper>
5857
) : (
5958
<Wrapper>
@@ -62,7 +61,9 @@ class CategoriesCell extends React.Component {
6261
onDelete={onDelete}
6362
communityId={communityId}
6463
/>
65-
<AddIcon src={`${ICON_ASSETS}/cmd/plus.svg`} />
64+
<div onClick={onAdd.bind(this, communityId, categories)}>
65+
<AddIcon src={`${ICON_ASSETS}/cmd/plus.svg`} />
66+
</div>
6667
</Wrapper>
6768
)}
6869
</div>
@@ -82,6 +83,7 @@ CategoriesCell.propTypes = {
8283
),
8384
communityId: PropTypes.number.isRequired,
8485
onDelete: PropTypes.func.isRequired,
86+
onAdd: PropTypes.func.isRequired,
8587
}
8688

8789
CategoriesCell.defaultProps = {

components/CommunityCell/index.js

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,76 @@
55
*/
66

77
import React from 'react'
8+
import R from 'ramda'
89
import PropTypes from 'prop-types'
910

11+
import shortid from 'shortid'
12+
13+
import ReactTooltip from 'react-tooltip'
1014
import { makeDebugger } from '../../utils'
1115
import { Wrapper, Logo, Title } from './styles'
1216

1317
/* eslint-disable no-unused-vars */
1418
const debug = makeDebugger('c:CommunityCell:index')
1519
/* eslint-enable no-unused-vars */
1620

17-
// TODO: array version
18-
const CommunityCell = ({ data }) => (
19-
<Wrapper>
20-
<Logo src={data.logo} />
21-
<Title>{data.title}</Title>
22-
</Wrapper>
21+
const tooltipOffset = JSON.stringify({ top: 1 })
22+
23+
const renderContent = (data, array) => {
24+
if (!R.isEmpty(data)) {
25+
return (
26+
<Wrapper>
27+
<Logo src={data.logo} />
28+
<Title>{data.title}</Title>
29+
</Wrapper>
30+
)
31+
} else if (!R.isEmpty(array)) {
32+
return (
33+
<Wrapper>
34+
{array.map(c => (
35+
<Wrapper key={shortid.generate()}>
36+
<div
37+
data-tip={c.title}
38+
data-for="community_cell"
39+
data-offset={tooltipOffset}
40+
>
41+
<Logo src={c.logo} />
42+
</div>
43+
</Wrapper>
44+
))}
45+
</Wrapper>
46+
)
47+
}
48+
return <div />
49+
}
50+
51+
// TODO: array version && tooltip
52+
const CommunityCell = ({ data, array }) => (
53+
<div>
54+
{renderContent(data, array)}
55+
<ReactTooltip effect="solid" place="bottom" id="community_cell" />
56+
</div>
2357
)
2458

2559
CommunityCell.propTypes = {
2660
// https://www.npmjs.com/package/prop-types
2761
data: PropTypes.shape({
28-
id: PropTypes.string.isRequired,
29-
logo: PropTypes.string.isRequired,
30-
title: PropTypes.string.isRequired,
31-
}).isRequired,
62+
id: PropTypes.string,
63+
logo: PropTypes.string,
64+
title: PropTypes.string,
65+
}),
66+
67+
array: PropTypes.arrayOf(
68+
PropTypes.shape({
69+
logo: PropTypes.string,
70+
title: PropTypes.string,
71+
})
72+
),
3273
}
3374

34-
CommunityCell.defaultProps = {}
75+
CommunityCell.defaultProps = {
76+
array: [],
77+
data: {},
78+
}
3579

3680
export default CommunityCell

containers/CategoryEditor/index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,26 @@ const debug = makeDebugger('C:CategoryEditor')
2929

3030
class CategoryEditorContainer extends React.Component {
3131
componentWillMount() {
32-
logic.init(this.props.categoryEditor)
32+
const { categoryEditor, editData } = this.props
33+
34+
logic.init(categoryEditor, editData)
35+
}
36+
37+
componentWillUnmount() {
38+
logic.uninit()
3339
}
3440

3541
render() {
3642
const { categoryEditor } = this.props
37-
const { mutating, categoryData } = categoryEditor
43+
const { mutating, categoryData, isEdit } = categoryEditor
3844

3945
return (
4046
<Wrapper>
4147
coderplanets
42-
<h2>创建社区分类</h2>
48+
<h2>
49+
{isEdit ? '编辑' : '创建'}
50+
社区分类
51+
</h2>
4352
<Divider />
4453
<FormInputer
4554
label="名称:"

containers/CategoryEditor/logic.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ export const mutateConfirm = () => {
3636
const fargs = castArgs(args, requiredArgs)
3737

3838
debug('fargs --> ', fargs)
39-
sr71$.mutate(S.createCategory, fargs)
39+
if (categoryEditor.isEdit) {
40+
return sr71$.mutate(
41+
S.updateCategory,
42+
castArgs(args, ['id', ...requiredArgs])
43+
)
44+
}
45+
return sr71$.mutate(S.createCategory, fargs)
4046
}
4147

4248
export function cancleMutate() {
@@ -47,24 +53,52 @@ export function cancleMutate() {
4753
closePreviewer()
4854
}
4955

56+
const initEditData = editData => {
57+
categoryEditor.markState({
58+
category: editData,
59+
isEdit: true,
60+
})
61+
}
62+
63+
export function cancleEdit() {
64+
categoryEditor.markState({
65+
category: {},
66+
isEdit: false,
67+
})
68+
/* closePreviewer() */
69+
}
5070
// ###############################
5171
// Data & Error handlers
5272
// ###############################
5373
const DataSolver = [
5474
{
5575
match: asyncRes('createCategory'),
5676
action: () => {
57-
debug('createCategory done!')
77+
closePreviewer(TYPE.GATEGORIES_REFRESH)
78+
},
79+
},
80+
{
81+
match: asyncRes('updateCategory'),
82+
action: () => {
5883
closePreviewer(TYPE.GATEGORIES_REFRESH)
5984
},
6085
},
6186
]
6287

6388
const ErrSolver = []
6489

65-
export function init(selectedStore) {
90+
export function init(selectedStore, editData) {
6691
categoryEditor = selectedStore
6792
debug(categoryEditor)
6893
if (sub$) sub$.unsubscribe()
6994
sub$ = sr71$.data().subscribe($solver(DataSolver, ErrSolver))
95+
96+
if (editData) {
97+
initEditData(editData)
98+
}
99+
}
100+
101+
export function uninit() {
102+
cancleEdit()
103+
/* cancleLoading() */
70104
}

containers/CategoryEditor/schema.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@ const createCategory = gql`
77
}
88
}
99
`
10+
const updateCategory = gql`
11+
mutation($id: ID!, $title: String!) {
12+
updateCategory(id: $id, title: $title) {
13+
id
14+
}
15+
}
16+
`
1017

1118
const schema = {
1219
createCategory,
20+
updateCategory,
1321
}
1422

1523
export default schema

containers/CategorySetter/index.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
*
3+
* CategorySetter
4+
*
5+
*/
6+
7+
import React from 'react'
8+
import { inject, observer } from 'mobx-react'
9+
import R from 'ramda'
10+
11+
import shortid from 'shortid'
12+
13+
// import Link from 'next/link'
14+
import { makeDebugger, storePlug } from '../../utils'
15+
16+
import { Wrapper, Divider, CategoryWrapper, CategoryTag } from './styles'
17+
18+
import * as logic from './logic'
19+
20+
/* eslint-disable no-unused-vars */
21+
const debug = makeDebugger('C:CategorySetter')
22+
/* eslint-enable no-unused-vars */
23+
24+
const CategoriesList = ({ communityId, categories, selectedCids }) => (
25+
<CategoryWrapper>
26+
{categories.map(c => (
27+
<CategoryTag
28+
key={shortid.generate()}
29+
active={R.contains(c.id, selectedCids)}
30+
onClick={logic.onAdd.bind(this, communityId, c.id, selectedCids)}
31+
>
32+
{c.title}
33+
</CategoryTag>
34+
))}
35+
</CategoryWrapper>
36+
)
37+
38+
class CategorySetterContainer extends React.Component {
39+
componentWillMount() {
40+
logic.init(this.props.categorySetter)
41+
}
42+
43+
render() {
44+
/* const mutating = false */
45+
const { categorySetter, editData } = this.props
46+
const { pagedCategories } = categorySetter
47+
48+
const selectedCids = R.pluck('id', editData.categories)
49+
50+
return (
51+
<Wrapper>
52+
coderplanets
53+
<h2>设置社区分类</h2>
54+
<Divider />
55+
{pagedCategories ? (
56+
<CategoriesList
57+
communityId={editData.id}
58+
categories={pagedCategories.entries}
59+
selectedCids={selectedCids}
60+
/>
61+
) : (
62+
<div />
63+
)}
64+
</Wrapper>
65+
)
66+
}
67+
}
68+
69+
export default inject(storePlug('categorySetter'))(
70+
observer(CategorySetterContainer)
71+
)

0 commit comments

Comments
 (0)