diff options
author | Justin Wernick <j.wernick@eyeo.com> | 2021-06-23 15:45:56 +0200 |
---|---|---|
committer | Justin Wernick <j.wernick@eyeo.com> | 2021-06-23 15:45:56 +0200 |
commit | 3bc808f306be580c8463652056ea0c493bbf6f7b (patch) | |
tree | ad269eb2c17927ccb32664aacc6060b1f79c249f | |
parent | 2bd9413b8dd8d5420251a7f327bd5b233c2aca61 (diff) |
Checkbox for facebook or not
-rw-r--r-- | src/form.rs | 15 | ||||
-rw-r--r-- | src/lib.rs | 58 |
2 files changed, 56 insertions, 17 deletions
diff --git a/src/form.rs b/src/form.rs index 00b5a58..c5ffe3e 100644 --- a/src/form.rs +++ b/src/form.rs @@ -1,5 +1,18 @@ -use structform::{derive_form_input, impl_text_input_with_stringops, ParseAndFormat}; +use seed::log; +use structform::{derive_form_input, impl_text_input_with_stringops, ParseAndFormat, ParseError}; derive_form_input! {FormTextInput} impl_text_input_with_stringops!(FormTextInput, String); + +derive_form_input! {FormCheckboxInput} + +impl ParseAndFormat<bool> for FormCheckboxInput<bool> { + fn parse(value: &str) -> Result<bool, ParseError> { + log!(value); + Ok(value == "true") + } + fn format(value: &bool) -> String { + value.to_string() + } +} @@ -14,18 +14,20 @@ fn init(_: Url, _: &mut impl Orders<Msg>) -> Model { #[derive(Default)] struct Model { form: ShareLinksOptionsForm, - links: Option<Vec<Node<Msg>>>, + links: Vec<Node<Msg>>, } #[derive(Default, Debug)] struct ShareLinksOptions { url: String, + facebook: bool, } #[derive(Default, StructForm)] #[structform(model = "ShareLinksOptions")] struct ShareLinksOptionsForm { url: FormTextInput<String>, + facebook: FormCheckboxInput<bool>, } pub enum Msg { @@ -39,9 +41,12 @@ fn update(msg: Msg, model: &mut Model, _: &mut impl Orders<Msg>) { let parsed = model.form.submit(); match parsed { Err(_e) => { - model.links = None; + model.links = Vec::new(); + } + Ok(options) => { + let option_links = vec![options.facebook.then(|| facebook::link(&options.url))]; + model.links = option_links.into_iter().flatten().collect() } - Ok(options) => model.links = Some(vec![facebook::link(&options.url)]), } } } @@ -89,31 +94,52 @@ fn view(model: &Model) -> Node<Msg> { p!["There is a better option. This site generates links that you can put into your site that do not allow social media companies to track your audience around the web, but still makes it easy for them to share. The links are pure HTML."], h2!["Tell Me About Your Site"], form![ - label![ - attrs! { - At::For => "url" - }, - "URL to share" + div![ + label![ + attrs! { + At::For => "url" + }, + "URL to share" + ], + input![ + attrs! { + At::Name => "url", + At::Value => model.form.url.input + }, + input_ev(Ev::Input, |val| Msg::Input(ShareLinksOptionsFormField::Url, val) ) + ], ], - input![ - id!["url"], - attrs! { - At::Value => model.form.url.input + div![ + label![ + attrs! { + At::For => "facebook", + }, + "Facebook" + ], + { + let is_checked = model.form.facebook.input == "true"; + input![ + attrs!{ + At::Name => "facebook", + At::Type => "checkbox", + At::Checked => is_checked.as_at_value(), + }, + input_ev(Ev::Input, move |_| Msg::Input(ShareLinksOptionsFormField::Facebook, (!is_checked).to_string())) + ] }, - input_ev(Ev::Input, |val| Msg::Input(ShareLinksOptionsFormField::Url, val) ) ] ], - model.links.as_ref().map(|links| nodes![ + (!model.links.is_empty()).then(|| nodes![ h2!["Here's Your Social Sharing Links"], div![ C!["results-code"], p!["Copy this code into your site to add your sharing buttons"], - codify(&links) + codify(&model.links) ], div![ C!["results-demo"], p!["This is what it will look like"], - links.iter().map(|link| nodes![link.clone(), Node::new_text("\n")]) + model.links.iter().map(|link| nodes![link.clone(), Node::new_text("\n")]) ] ]) ] |