-
Notifications
You must be signed in to change notification settings - Fork 3
/
card-list-grouper.js
112 lines (103 loc) · 3.29 KB
/
card-list-grouper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict'
// https://github.com/EnCiv/unpoll/issues/15
import React, { useRef, useState, useEffect, useLayoutEffect, useMemo, useReducer } from 'react'
import { createUseStyles } from 'react-jss'
import cx from 'classnames'
import CardStack from './card-stack'
import TopicCard from './topic-card'
import ActionCard from './action-card'
import useMethods from 'use-methods'
import PageHeader from './page-header'
import TopicHeader from './topic-header'
export const CardListGrouper = props => {
const {
className,
style,
store,
onDone,
topicCard,
majorLine = 'Select the topics you find similar enough to be combined (If any).',
minorLine = 'Below are topics submited by you and other participans',
...otherProps
} = props
const { methods, methodState } = props[store]
const [lState, lMethods] = useMethods(
(dispatch, lState) => ({
fromGroupRemoveId(group, id) {
// before removing the id, check if we are ending the grouping
const groupIndex = methodState.cards.findIndex(crd => crd.cards && crd._id === group) // might be ejecting cards from one group, while adding them to another
if (methodState.cards[groupIndex].cards.length <= 1 && group === lState.group) dispatch({ group: '' })
methods.fromGroupRemoveId(group, id)
},
toggleSelect(id) {
if (!lState.group) {
methods.toGroupAddId('', id)
dispatch({ group: methodState.cards.find(crd => crd.cards && crd.cards[0]._id === id)?._id })
} else methods.toGroupAddId(lState.group, id)
},
resetGroup(group) {
if (lState.group === group || !group) dispatch({ group: '' })
},
setGroup(group) {
if (!lState.group) dispatch({ group })
},
}),
{ group: '' }
)
const [groupMethods, neverSetGroupMethods] = useState(() => Object.assign({}, methods, lMethods))
const classes = useStyles(props)
return (
<div className={cx(className, classes.list)} style={style}>
{topicCard && <TopicHeader card={topicCard} />}
<PageHeader majorLine={majorLine} minorLine={minorLine} />
{methodState.cards.map(card => {
if (card.cards)
if (card.cards.length)
return (
<CardStack
className={classes.topic}
key={card._id}
cardStore={{ ...props[store], methods: groupMethods }}
group={card._id}
defaultShape={'add-remove'}
cards={card.cards}
/>
)
else return null
else
return (
<TopicCard
topicObj={card}
className={classes.topic}
key={card._id}
onToggleSelect={lMethods.toggleSelect}
/>
)
})}
<ActionCard
className={classes.action}
active={!lState.group && 'true'}
name={lState.group ? 'Complete Group before continuing' : 'Continue'}
onDone={onDone}
/>
</div>
)
}
const useStyles = createUseStyles({
list: {
padding: '1rem',
},
topic: {
position: 'relative',
borderRadius: '1rem',
overflow: 'hidden',
marginTop: '1rem',
'&:first-child': {
marginTop: 0,
},
},
action: {
marginTop: '1rem',
},
})
export default CardListGrouper