Repository navigation
Expand file tree
/
Copy pathFormResponses.tsx
More file actions
196 lines (184 loc) · 5.12 KB
/
Copy pathFormResponses.tsx
File metadata and controls
196 lines (184 loc) · 5.12 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { NavLink, Outlet, useParams } from 'react-router-dom'
import chartXkcd from 'chart.xkcd'
import { stringify } from 'csv-stringify/browser/esm/sync'
import { FormId } from './form'
import { useAppSelector } from './hooks'
import { selectFormResponses } from './responsesSlice'
import { BarChart, PieChart } from './chart'
import { QuestionStatistics } from './statistics'
import Code from './Code'
type Params = {
formId: FormId
}
export function FormResponsesLayout() {
const params = useParams() as Params
const { responses } = useAppSelector((state) =>
selectFormResponses(state, params.formId)
)
if (responses.length === 0) {
return (
<>
<p>There are no responses yet.</p>
</>
)
}
return (
<>
<FormResponsesNav responseCount={responses.length} />
<Outlet />
</>
)
}
function FormResponsesNav(props: { responseCount: number }) {
return (
<nav className="subnav row flex-edges flex-middle">
<div>
Responses: <strong>{props.responseCount}</strong>
</div>
<ul className="inline">
<li>
<NavLink to="table">Table</NavLink>
</li>
<li>
<NavLink to="statistics">Statistics</NavLink>
</li>
<li>
<NavLink to="csv">CSV</NavLink>
</li>
</ul>
</nav>
)
}
export function FormResponsesTable() {
const params = useParams() as Params
const { header, responses } = useAppSelector((state) =>
selectFormResponses(state, params.formId)
)
return (
<div className="table-container-scroll">
<table className="table-scroll">
<thead>
<tr>
{header.map((name, index) => (
<th key={index} title={name}>
{index === 0 ? '#' : name}
</th>
))}
</tr>
</thead>
<tbody>
{responses.map((row, index) => (
<tr key={index}>
{row.map((response, index) => (
<td title={response} key={index}>
{response}
</td>
))}
</tr>
))}
</tbody>
</table>
</div>
)
}
export function FormResponsesStatistics() {
const params = useParams() as Params
const { statistics } = useAppSelector((state) =>
selectFormResponses(state, params.formId)
)
const renderQuestionStatistics = (item: QuestionStatistics): JSX.Element => {
switch (item.tag) {
case 'bar':
return (
<BarChart
config={{
title: item.statistics.title,
data: {
labels: item.statistics.data.map((x) => x.label),
datasets: [
{
data: item.statistics.data.map((x) => x.value),
},
],
},
options: {
yTickCount: 4,
},
}}
/>
)
case 'pie':
return (
<PieChart
config={{
title: item.statistics.title,
data: {
labels: item.statistics.data.map((x) => x.label),
datasets: [
{
data: item.statistics.data.map((x) => x.value),
},
],
},
options: {
legendPosition: chartXkcd.config.positionType.upRight,
},
}}
/>
)
case 'scale':
return (
<BarChart
config={{
title: item.statistics.title,
data: {
labels: item.statistics.data.values.map((x, index) => {
if (
index === 0 &&
item.statistics.data.startLabel.length > 0
) {
return `${x.label} (${item.statistics.data.startLabel})`
}
if (
index === item.statistics.data.values.length - 1 &&
item.statistics.data.endLabel.length > 0
) {
return `${x.label} (${item.statistics.data.endLabel})`
}
return x.label
}),
datasets: [
{
data: item.statistics.data.values.map((x) => x.value),
},
],
},
options: {
yTickCount: 4,
dataColors: item.statistics.data.values.map(() => '#28a3dd'),
},
}}
/>
)
default:
const _exhaustiveCheck: never = item
return _exhaustiveCheck
}
}
return (
<>
{statistics.map((item) => (
<div key={item.statistics.questionId} className="chart margin-bottom">
{renderQuestionStatistics(item)}
</div>
))}
</>
)
}
export function FormResponsesCsv() {
const params = useParams() as Params
const { header, responses } = useAppSelector((state) =>
selectFormResponses(state, params.formId)
)
return <Code language="csv" code={stringify([header].concat(responses))} />
}