From 2b73ec25dd4ed16ce790c2bc5917b8e6356e5037 Mon Sep 17 00:00:00 2001 From: Egor Gorbachev <7gorbachevm@gmail.com> Date: Sat, 24 Feb 2024 13:21:39 +0700 Subject: [PATCH] Handle pie chart empty state (#18) --- src/api/api.ts | 2 +- src/screens/component-catalog/components.tsx | 5 + .../pie-chart-canvas-story.tsx | 43 +++++++ .../empty-study-frequency-chart-text.tsx | 27 +++++ src/screens/user-statistics/legend-item.tsx | 8 +- .../store/user-statistics-store.ts | 4 + .../user-statistics-screen.tsx | 107 ++++++++++++------ src/translations/t.ts | 4 + 8 files changed, 162 insertions(+), 38 deletions(-) create mode 100644 src/screens/component-catalog/pie-chart-canvas-story.tsx create mode 100644 src/screens/user-statistics/empty-study-frequency-chart-text.tsx diff --git a/src/api/api.ts b/src/api/api.ts index 8e955d99..b3745ef9 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -167,4 +167,4 @@ export const myPlansRequest = () => { export const myStatisticsRequest = () => { return request("/my-statistics"); -} +}; diff --git a/src/screens/component-catalog/components.tsx b/src/screens/component-catalog/components.tsx index 8fd2b847..dc7ac263 100644 --- a/src/screens/component-catalog/components.tsx +++ b/src/screens/component-catalog/components.tsx @@ -2,6 +2,7 @@ import { Button } from "../../ui/button.tsx"; import { ReactNode } from "react"; import { CardPreviewStory } from "./card-preview-story.tsx"; import { SelectStory } from "./select-story.tsx"; +import { PieChartCanvasStory } from "./pie-chart-canvas-story.tsx"; export type Component = { name: string; @@ -46,4 +47,8 @@ export const components: Array = [ name: "Select", component: , }, + { + name: "PieChart", + component: , + }, ]; diff --git a/src/screens/component-catalog/pie-chart-canvas-story.tsx b/src/screens/component-catalog/pie-chart-canvas-story.tsx new file mode 100644 index 00000000..0f766e7a --- /dev/null +++ b/src/screens/component-catalog/pie-chart-canvas-story.tsx @@ -0,0 +1,43 @@ +import { PieChartCanvas } from "../user-statistics/pie-chart-canvas.tsx"; +import { css } from "@emotion/css"; + +export const PieChartCanvasStory = () => { + return ( +
+ + + + + +
+ ); +}; diff --git a/src/screens/user-statistics/empty-study-frequency-chart-text.tsx b/src/screens/user-statistics/empty-study-frequency-chart-text.tsx new file mode 100644 index 00000000..f829029e --- /dev/null +++ b/src/screens/user-statistics/empty-study-frequency-chart-text.tsx @@ -0,0 +1,27 @@ +import { css } from "@emotion/css"; +import { theme } from "../../ui/theme.tsx"; +import React from "react"; +import { t } from "../../translations/t.ts"; + +export const EmptyStudyFrequencyChartText = () => { + return ( +
+ {t("user_stats_empty_text")} +
+ ); +}; diff --git a/src/screens/user-statistics/legend-item.tsx b/src/screens/user-statistics/legend-item.tsx index 1d66d02e..9ba9a21a 100644 --- a/src/screens/user-statistics/legend-item.tsx +++ b/src/screens/user-statistics/legend-item.tsx @@ -1,7 +1,11 @@ import { css } from "@emotion/css"; import React from "react"; -export const LegendItem = (props: { color: string }) => { +type Props = { + color: string; +}; + +export const LegendItem = (props: Props) => { const { color } = props; return (
{ height: 14, width: 14, backgroundColor: color, - borderRadius: 4 + borderRadius: 4, })} /> ); diff --git a/src/screens/user-statistics/store/user-statistics-store.ts b/src/screens/user-statistics/store/user-statistics-store.ts index 2e762891..b1e8eb76 100644 --- a/src/screens/user-statistics/store/user-statistics-store.ts +++ b/src/screens/user-statistics/store/user-statistics-store.ts @@ -44,4 +44,8 @@ export class UserStatisticsStore { return this.userStatistics.value.intervalFrequency; } + + get isFrequencyChartEmpty() { + return this.frequencyChart.every((item) => item.frequency === 0); + } } diff --git a/src/screens/user-statistics/user-statistics-screen.tsx b/src/screens/user-statistics/user-statistics-screen.tsx index ece786bb..71f02383 100644 --- a/src/screens/user-statistics/user-statistics-screen.tsx +++ b/src/screens/user-statistics/user-statistics-screen.tsx @@ -16,6 +16,10 @@ import { } from "./pie-chart-canvas.tsx"; import { LegendItem } from "./legend-item.tsx"; import { DeckLoading } from "../shared/deck-loading.tsx"; +import { EmptyStudyFrequencyChartText } from "./empty-study-frequency-chart-text.tsx"; + +const pieChartWidth = 250; +const pieChartHeight = 200; export const UserStatisticsScreen = observer(() => { const userStatisticsStore = useUserStatisticsStore(); @@ -79,45 +83,78 @@ export const UserStatisticsScreen = observer(() => { marginTop: 10, marginLeft: "auto", marginRight: "auto", + position: "relative", })} > - -
+ {userStatisticsStore.isFrequencyChartEmpty ? ( +
+ +
+ ) : ( + + )} -
-
- - - {t("user_stats_chart_min_expl")} - -
-
- - - {t("user_stats_chart_max_expl")} - -
+ {userStatisticsStore.isFrequencyChartEmpty && ( + + )}
-

- - {t("user_stats_learning_time_hint")} - -

+ + {!userStatisticsStore.isFrequencyChartEmpty && ( + <> +
+
+ + + {t("user_stats_chart_min_expl")} + +
+
+ + + {t("user_stats_chart_max_expl")} + +
+
+

+ + {t("user_stats_learning_time_hint")} + +

+ + )} ) : null} diff --git a/src/translations/t.ts b/src/translations/t.ts index 8ccf3a2f..0a3776e3 100644 --- a/src/translations/t.ts +++ b/src/translations/t.ts @@ -2,6 +2,7 @@ import { Translator } from "../lib/translator/translator.ts"; import { getUserLanguage } from "./get-user-language.ts"; const en = { + user_stats_empty_text: "Study more cards to see the data", hide_card_forever: "Hide forever", mute_cards: "Mute sound", unmute_cards: "Unmute sound", @@ -186,6 +187,7 @@ const en = { type Translation = typeof en; const ru: Translation = { + user_stats_empty_text: "Изучите больше карточек, чтобы увидеть данные", hide_card_forever_confirm_title: "Вы уверены, что хотите скрыть эту карточку навсегда? Вы больше не увидите её", hide_card_forever: "Скрыть навсегда", @@ -363,6 +365,7 @@ const ru: Translation = { }; const es: Translation = { + user_stats_empty_text: "Estudia más tarjetas para ver los datos", hide_card_forever: "Ocultar para siempre", mute_cards: "Silenciar sonido", unmute_cards: "Activar sonido", @@ -546,6 +549,7 @@ const es: Translation = { }; const ptBr: Translation = { + user_stats_empty_text: "Estude mais cartões para ver os dados", hide_card_forever: "Ocultar para sempre", mute_cards: "Silenciar som", unmute_cards: "Ativar som",