about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-07-27T19·58+0300
committertazjin <tazjin@tvl.su>2022-07-27T21·54+0000
commit134efdab2541eb505e6f6fedc2fb48f94e1ab3be (patch)
treec2b7f86eac16ea0de529b47d6b9b6febac138f5e
parent67b7668e85dfcab4fadcab2fc42baba969f7df9e (diff)
feat(predlozhnik): implement fever-dream like case selection r/4328
don't look too closely at this, it's kind of insane

Change-Id: I789ddd9dd5a4cf28b3007e38ef1c345e639a5fc1
Reviewed-on: https://cl.tvl.fyi/c/depot/+/5983
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
-rw-r--r--users/tazjin/predlozhnik/src/main.rs101
1 files changed, 97 insertions, 4 deletions
diff --git a/users/tazjin/predlozhnik/src/main.rs b/users/tazjin/predlozhnik/src/main.rs
index f2e46da48f..65c11f07db 100644
--- a/users/tazjin/predlozhnik/src/main.rs
+++ b/users/tazjin/predlozhnik/src/main.rs
@@ -1,3 +1,4 @@
+use yew::html::Scope;
 use yew::prelude::*;
 
 use lazy_static::lazy_static;
@@ -97,6 +98,16 @@ lazy_static! {
 
         m
     };
+    static ref ПАДЕЖИ: BTreeSet<Падеж> = BTreeSet::from(Падеж::ВСЕ);
+    static ref ПРЕДЛОГИ: BTreeSet<&'static str> = {
+        let mut s: BTreeSet<&'static str> = BTreeSet::new();
+
+        for п in ПО_ПРЕДЛОГУ.keys() {
+            s.insert(п);
+        }
+
+        s
+    };
 }
 
 fn example_output() -> String {
@@ -156,13 +167,68 @@ fn ограничить(м: &Модель) -> Вывод {
         },
 
         (None, None) => Вывод {
-            доступные_падежи: BTreeSet::new(),
-            доступные_предлоги: BTreeSet::new(),
+            доступные_падежи: ПАДЕЖИ.clone(),
+            доступные_предлоги: ПРЕДЛОГИ.clone(),
             объяснение: None,
         },
     }
 }
 
+fn покажи_предлог(
+    link: &Scope<Модель>,
+    м: &Модель,
+    вв: &Вывод,
+    п: &'static str,
+) -> Html {
+    let выбран = м.предлог == Some(п);
+    let доступен = вв.доступные_предлоги.contains(п);
+
+    let mut класс = "btn btn-ghost ".to_string();
+    класс += match (выбран, доступен) {
+        (true, _) => "btn-error",
+        (false, true) => "btn-primary",
+        (false, false) => "btn-default",
+    };
+
+    html! {
+        <button class={класс}
+         onclick={link.callback(move |_| if выбран {
+             Сообщение::ВыбралПредлог(None)
+         } else {
+             Сообщение::ВыбралПредлог(Some(п))
+         })}
+         disabled={!доступен}>
+        {п}
+        </button>
+    }
+}
+
+fn покажи_падеж(
+    link: &Scope<Модель>, м: &Модель, вв: &Вывод, п: Падеж
+) -> Html {
+    let выбран = м.падеж == Some(п);
+    let доступен = вв.доступные_падежи.contains(&п);
+
+    let mut класс = "btn btn-ghost ".to_string();
+    класс += match (выбран, доступен) {
+        (true, _) => "btn-error",
+        (false, true) => "btn-primary",
+        (false, false) => "btn-default",
+    };
+
+    html! {
+        <button class={класс}
+         onclick={link.callback(move |_| if выбран {
+             Сообщение::ВыбралПадеж(None)
+         } else {
+             Сообщение::ВыбралПадеж(Some(п))
+         })}
+         disabled={!доступен}>
+        {format!("{:?}", п)}
+        </button>
+    }
+}
+
 impl Component for Модель {
     type Message = Сообщение;
     type Properties = ();
@@ -180,9 +246,36 @@ impl Component for Модель {
         true
     }
 
-    fn view(&self, _ctx: &Context<Self>) -> Html {
+    fn view(&self, ctx: &Context<Self>) -> Html {
+        let вв = ограничить(self);
+        let link = ctx.link();
+
+        let кнапки_предлогов = ПРЕДЛОГИ
+            .iter()
+            .map(|п| покажи_предлог(link, self, &вв, п))
+            .collect::<Html>();
+
+        let кнапки_падежов = ПАДЕЖИ
+            .iter()
+            .map(|п| покажи_падеж(link, self, &вв, *п))
+            .collect::<Html>();
+
         html! {
-            <pre>{example_output()}</pre>
+            <>
+                <link rel="stylesheet"
+                      href="https://unpkg.com/terminal.css@0.7.2/dist/terminal.min.css" />
+                <div id="predlogi">
+                  <h2>{"Предлоги:"}</h2>
+                  {кнапки_предлогов}
+                </div>
+                <hr/>
+
+                <div id="padezhi">
+                  <h2>{"Падежи:"}</h2>
+                  {кнапки_падежов}
+                </div>
+                <hr/>
+            </>
         }
     }
 }