/* * Copyright (C) 2022 Aravinth Manivannan * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ use tracing::{debug, error, info}; use web_sys::{HtmlInputElement, HtmlTextAreaElement}; use yew::prelude::*; const JSON: &str = "JSON"; const YAML: &str = "YAML"; const TOML: &str = "TOML"; #[function_component] pub fn Validator() -> Html { let format_value_handle = use_state(|| TOML.to_string()); let input_value_handle = use_state(String::default); let input_value = (*input_value_handle).clone(); let config = input_value_handle.clone(); let format = format_value_handle.clone(); let onsubmit = { move |e: SubmitEvent| { e.prevent_default(); let form = e.target().unwrap(); debug!("Received form submit: {:?}", form); debug!("Received form submit: {:?}", &config); debug!("Received form submit: {:?}", format); if format.as_str() == JSON { let _config: libconfig::Config = serde_json::from_str(&config).unwrap(); info!("All good"); } else if format.as_str() == YAML { let _config: libconfig::Config = serde_yaml::from_str(config.as_str()).unwrap(); info!("All good"); } else if format.as_str() == TOML { let _config: libconfig::Config = toml::from_str(config.as_str()).unwrap(); info!("All good"); } else { error!("Unknown format: {}", format.as_str()); } } }; let on_input_change = Callback::from(move |e: InputEvent| { let value = input_value_handle.clone(); // You must KNOW target is a HtmlInputElement, otherwise // the call to value would be Undefined Behaviour (UB). value.set(e.target_unchecked_into::().value()); info!("Changing configuration value: {:?}", input_value_handle) }); let on_format_change = Callback::from(move |e: InputEvent| { let value = format_value_handle.clone(); // You must KNOW target is a HtmlInputElement, otherwise // the call to value would be Undefined Behaviour (UB). let target = e.target_unchecked_into::(); info!("Received radio value: {:?}", target.value()); value.set(target.value()); target.checked(); info!("Changing radio value: {:?}", value) }); html! {

{"Validate LibrePages Configuration"}