feat(corp/rih): wire up captcha solving callback

This turned out a lot nicer than I expected it to be.

Change-Id: I427670644eba789ea2037423fa9af8e632b19b34
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8695
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
This commit is contained in:
Vincent Ambo 2023-06-02 15:56:47 +03:00 committed by tazjin
parent 6f0ddbac06
commit 138f1ca1b9
2 changed files with 35 additions and 11 deletions

View file

@ -1,12 +1,13 @@
html! {
<main>
<script>
{r#"function captchaOnload(sitekey) {
{r#"function captchaOnload(sitekey, callback) {
if (window.smartCaptcha) {
const container = document.getElementById('captcha-container');
const widgetId = window.smartCaptcha.render(container, {
sitekey: sitekey,
hl: 'en',
callback: callback,
});
}
}"#}

View file

@ -98,7 +98,6 @@ struct Record {
work_background: String,
}
#[derive(Default)]
struct App {
// The record being populated.
record: Record,
@ -111,6 +110,13 @@ struct App {
// History handler.
history: BrowserHistory,
// Captcha token, if the captcha has been solved.
captcha_token: Option<String>,
// Captcha callback closure which needs to be kept alive for the
// lifecycle of the app.
captcha_callback: Closure<dyn FnMut(String)>,
}
#[derive(Clone, Debug)]
@ -130,6 +136,7 @@ enum Msg {
SetPosition(String),
SetJobDetails(String),
SetWorkBackground(String),
CaptchaSolved(String),
}
/// Callback handler for adding a technology.
@ -293,14 +300,20 @@ impl Component for App {
type Message = Msg;
type Properties = ();
fn create(_ctx: &Context<Self>) -> Self {
let mut new = Self::default();
if let Ok(record) = LocalStorage::get("record") {
new.record = record;
fn create(ctx: &Context<Self>) -> Self {
App {
record: LocalStorage::get("record").unwrap_or_default(),
citizenship_focus: false,
citizenship_query: String::default(),
history: BrowserHistory::default(),
captcha_token: None,
captcha_callback: {
let link = ctx.link().clone();
Closure::wrap(Box::new(move |val| {
link.send_message(Msg::CaptchaSolved(val));
}))
},
}
new
}
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
@ -369,6 +382,11 @@ impl Component for App {
self.record.work_background = background;
(true, false)
}
Msg::CaptchaSolved(token) => {
self.captcha_token = Some(token);
(false, true)
}
};
if state_change {
@ -399,8 +417,13 @@ impl Component for App {
fn rendered(&mut self, _: &Context<Self>, first_render: bool) {
if first_render {
let func = js_sys::Function::new_with_args("key", "captchaOnload(key)");
let _ = func.call1(&JsValue::NULL, &JsValue::from_str(CAPTCHA_KEY));
let func =
js_sys::Function::new_with_args("key, callback", "captchaOnload(key, callback)");
let _ = func.call2(
&JsValue::NULL,
&JsValue::from_str(CAPTCHA_KEY),
&self.captcha_callback.as_ref().unchecked_ref(),
);
}
}
}