Skip to content

Commit db8e631

Browse files
committed
Render summary as bar chart after submitting answer
1 parent 3a184f0 commit db8e631

File tree

5 files changed

+67
-13
lines changed

5 files changed

+67
-13
lines changed

back-end/server.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,20 @@ def get_feedback_summary(key: str):
252252
values[value_to_str(c)] = 0
253253
values[value_to_str(None)] = 0
254254

255+
total = 0
256+
non_null = 0
255257
for count, value in rows:
256258
values[value_to_str(value)] = count
257259

258-
data = dict(values=values)
260+
total += count
261+
if value is not None:
262+
non_null += count
263+
264+
data = dict(
265+
count_non_null=non_null,
266+
count_total=total,
267+
values=values,
268+
)
259269
return jsonify(data), 200
260270

261271

back-end/test.sh

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,38 @@ base_url=${1:-localhost\:5000}
44

55
# Wait until server is online
66
for i in $(seq 1 60); do
7-
curl $base_url/feedback/how-are-you-feeling && break || true
7+
curl $base_url/feedback/thumbs && break || true
88
sleep 1
99
done
1010

1111
check_summary() {
12-
len=$(curl -s $base_url/feedback/how-are-you-feeling/answer | jq 'length')
12+
len=$(curl -s $base_url/feedback/thumbs/answer | jq 'length')
1313
test "$len" -eq $1
1414

15-
positive=$(curl -s $base_url/feedback/how-are-you-feeling/summary | jq .'values."1"')
15+
positive=$(curl -s $base_url/feedback/thumbs/summary | jq .'values."1"')
1616
test "$positive" -eq $2
17-
negative=$(curl -s $base_url/feedback/how-are-you-feeling/summary | jq .'values."-1"')
17+
negative=$(curl -s $base_url/feedback/thumbs/summary | jq .'values."-1"')
1818
test "$negative" -eq $3
19-
empty=$(curl -s $base_url/feedback/how-are-you-feeling/summary | jq .'values.""')
19+
empty=$(curl -s $base_url/feedback/thumbs/summary | jq .'values.""')
2020
test "$empty" -eq $4
2121
}
2222

23-
len=$(curl -s $base_url/feedback/how-are-you-feeling/answer | jq 'length')
23+
len=$(curl -s $base_url/feedback/thumbs/answer | jq 'length')
2424
test "$len" -eq 0
2525

2626
feedback="1
2727
-1
2828
1"
2929

3030
for i in $feedback; do
31-
curl -s -X POST -H "Content-Type: application/json" -d "{\"value\":$i, \"submit\": true}" $base_url/feedback/how-are-you-feeling/answer
31+
curl -s -X POST -H "Content-Type: application/json" -d "{\"value\":$i, \"submit\": true}" $base_url/feedback/thumbs/answer
3232
done;
3333

34-
last=$(curl -s -X POST -H "Content-Type: application/json" -d "{\"value\": -1}" $base_url/feedback/how-are-you-feeling/answer | jq .id -r)
34+
last=$(curl -s -X POST -H "Content-Type: application/json" -d "{\"value\": -1}" $base_url/feedback/thumbs/answer | jq .id -r)
3535

3636
check_summary 4 2 2 0
3737

38-
curl -s -X PATCH -H "Content-Type: application/json" -d "{\"value\": 1, \"submit\": true}" $base_url/feedback/how-are-you-feeling/answer/$last
39-
curl -s -X POST -H "Content-Type: application/json" -d "{}" $base_url/feedback/how-are-you-feeling/answer
38+
curl -s -X PATCH -H "Content-Type: application/json" -d "{\"value\": 1, \"submit\": true}" $base_url/feedback/thumbs/answer/$last
39+
curl -s -X POST -H "Content-Type: application/json" -d "{}" $base_url/feedback/thumbs/answer
4040

4141
check_summary 5 3 1 1

front-end/src/App.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import Error from "./lib/Error.svelte";
1919
import Submit from "./lib/Submit.svelte";
2020
import Comment from "./lib/Comment.svelte";
21+
import BarChart from "./lib/BarChart.svelte";
2122
2223
let loading = $state(true);
2324
let error = $state<Problem | null>(null);
@@ -139,10 +140,10 @@
139140
{/if}
140141
<Submit onSubmit={handleSubmit} />
141142
{/if}
142-
{:else if question && answer?.submitted_at}
143+
{:else if question && answer?.submitted_at && summary}
143144
<div class="summary">
144145
<p>Thank you for your feedback!</p>
145-
<pre>{JSON.stringify(summary, null, 2)}</pre>
146+
<BarChart choices={question.choices} {summary} />
146147
</div>
147148
{/if}
148149
</main>

front-end/src/lib/BarChart.svelte

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<script lang="ts">
2+
import type { Choice, Summary } from "./api";
3+
4+
interface Props {
5+
choices: Choice[];
6+
summary: Summary;
7+
}
8+
9+
let { choices, summary }: Props = $props();
10+
</script>
11+
12+
<div class="bar-chart">
13+
{#each choices as choice}
14+
<div class="row">
15+
<span>{choice.label}</span>
16+
<div class="bar" style="width: {summary.values[String(choice.value)] / summary.count_non_null * 100}%"
17+
></div>
18+
<span>{summary.values[String(choice.value)]}</span>
19+
</div>
20+
{/each}
21+
</div>
22+
23+
<style>
24+
.bar-chart {
25+
margin: 2rem 0;
26+
}
27+
28+
.row {
29+
display: flex;
30+
align-items: center;
31+
gap: 1rem;
32+
margin: 1rem 0;
33+
}
34+
35+
.bar {
36+
background-color: var(--color-primary);
37+
border-radius: var(--border-radius);
38+
height: 0.5rem;
39+
min-width: 1rem;
40+
}
41+
</style>

front-end/src/lib/api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export interface Answer {
3434
export type UpdateAnswerPayload = Partial<Pick<Answer, "value" | "comment" | "group"> & {submit: boolean}>;
3535

3636
export interface Summary {
37+
count_non_null: number;
38+
count_total: number;
3739
values: Record<string, number>;
3840
}
3941

0 commit comments

Comments
 (0)