-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
149 lines (131 loc) · 4.01 KB
/
index.tsx
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import React, { FunctionComponent, useEffect, useState } from 'react'
import styled from 'styled-components'
import shallow from 'zustand/shallow'
import { useStore } from '../../store'
import { DragItem } from '../QuestionContent/components/DragItem'
import { ExplanationInput } from './components/ExplanationInput'
import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd";
import { Component } from '../../utils/dynamicComponents'
import { Explanation } from '../../store/slices/explanation'
import { publish } from '../../utils/customEvent'
interface Props {
initialData?: Explanation[]
}
export const Explanations: FunctionComponent<Props> = ({ initialData }) => {
const {
storeExplanations,
changeSelected,
selectedExplanation,
deleteExplanation,
updateExplanation,
updateExplanations,
setInitialExplanations
} = useStore((state) => ({
storeExplanations: state.explanations,
changeSelected: state.changeSelected,
selectedExplanation: state.selectedExplanation,
updateExplanation: state.updateExplanation,
updateExplanations: state.updateExplanations,
deleteExplanation: state.deleteExplanation,
setInitialExplanations: state.setInitialExplanations
}), shallow)
useEffect(() => {
if(initialData?.length > 0) {
const initialExplanations = initialData?.map(init => init)
setInitialExplanations(initialExplanations)
}
}, [initialData])
const reorder = (list, startIndex, endIndex) => {
const result: Explanation[] = Array.from(list);
const [removed] = result.splice(startIndex, 1);
result.splice(endIndex, 0, removed);
return result.map((r, i) => {
return {
...r,
position: i + 1
}
})
};
const onDragEnd = (result) => {
// dropped outside the list
if (!result.destination) {
return;
}
const items = reorder(
storeExplanations,
result.source.index,
result.destination.index
);
updateExplanations(items)
}
return (
<Wrapper>
<p>Explanations</p>
<DragDropContext onDragEnd={onDragEnd}>
<Droppable droppableId='droppable'>
{(provided, snapshot) => (
<div
{...provided.droppableProps}
ref={provided.innerRef}
>
{ storeExplanations.map(((e, i) => (
<DragItem
key={e.position + ''}
id={e.position + ''}
index={i}
component={(
<ExplanationBox
key={e.index}
selected={+e.index === selectedExplanation}
onClick={() => {
changeSelected(e.index)
}}
>
<ExplanationInput
text={e.text}
unselect={() => { changeSelected(null) }}
onUpdate={(text) => {
updateExplanation(e.index, text, e.position, e.id)
}}
/>
</ExplanationBox>
)}
onDelete={() => {
publish('delete-explanation', { deleteIndex: e.index })
deleteExplanation(e.index)
}}
/>
))) }
{ provided.placeholder }
</div>
)}
</Droppable>
</DragDropContext>
</Wrapper>
)
}
const Wrapper = styled.div`
background: #eee;
border-radius: 4px;
margin-left: 8px;
height: 100%;
padding: 4px;
> p {
margin: 0;
padding: 10px;
font-weight: 600;
}
`
interface StyledExplanation {
selected: boolean;
}
const ExplanationBox = styled.div<StyledExplanation>`
border: 2px solid white;
padding: 8px;
border-radius: 4px;
background: white;
margin-bottom: 4px;
${props => props.selected && `
border: 2px solid #424242;
`}
`