use std::{collections::HashMap, str::FromStr}; use web_sys::{Event, HtmlInputElement}; use yew::{html, AttrValue, Callback, Html, TargetCast}; use crate::{ generate_sql::{ SQLBoolValueGuess, SQLIntValueGuess, SQLStringValueGuess, SQLTimeValueGuess, SQLValueGuess, }, magicdraw_parser::{SQLCheckConstraint, SQLColumn}, }; fn show_dropdown_picker(selected: &str, options: &[AttrValue], onchange: Callback) -> Html { html! { } } fn show_enum_dropdown( selected: &T, options: HashMap, onchange: Callback, ) -> Html { let keys = options.keys().map(AttrValue::clone).collect::>(); let guess_str = options .iter() .find(|(_, v)| v.eq(&selected)) .unwrap() .0 .clone(); show_dropdown_picker( &guess_str, &keys, onchange.reform(move |value_str: String| options.get(value_str.as_str()).unwrap().clone()), ) } fn show_range_picker( min: T, max: T, default_min: T, default_max: T, onchange: Callback<(T, T)>, ) -> Html { let onchange_min = { let onchange = onchange.clone(); let default_min = default_min.clone(); let max = max.clone(); Callback::from(move |e: Event| { let value = e.target_unchecked_into::().value(); let min_value = value.parse().unwrap_or(default_min.clone()); onchange.emit((min_value, max.clone())) }) }; let onchange_max = { let onchange = onchange.clone(); let default_max = default_max.clone(); let min = min.clone(); Callback::from(move |e: Event| { let value = e.target_unchecked_into::().value(); let max_value = value.parse().unwrap_or(default_max.clone()); onchange.emit((min.clone(), max_value)) }) }; html! {
{ ".." }
} } pub fn generator_picker( column: &SQLColumn, value: &SQLValueGuess, onchange: Callback, ) -> Html { // TODO: Refacotr 'time', 'datetime', and 'date'. They are very similar match value { SQLValueGuess::Int(guess) => { if column.primary_key { return html!("Auto increment"); } let mut min = 0; let mut max = 0; if let SQLIntValueGuess::Range(range_min, range_max) = guess { min = *range_min; max = *range_max; } // TODO: Disallow entering floating point numbers show_range_picker( min, max, 0, 100, onchange.reform(|(min, max)| SQLValueGuess::Int(SQLIntValueGuess::Range(min, max))), ) } SQLValueGuess::Float(min, max) => show_range_picker( *min, *max, 0.0, 100.0, onchange.reform(|(min, max)| SQLValueGuess::Float(min, max)), ), SQLValueGuess::Date(guess) => { let options = HashMap::from([ ("Now".into(), SQLTimeValueGuess::Now), ("Future".into(), SQLTimeValueGuess::Future), ("Past".into(), SQLTimeValueGuess::Past), ]); show_enum_dropdown( guess, options, onchange.reform(|enum_value| SQLValueGuess::Date(enum_value)), ) } SQLValueGuess::Time(guess) => { let options = HashMap::from([ ("Now".into(), SQLTimeValueGuess::Now), ("Future".into(), SQLTimeValueGuess::Future), ("Past".into(), SQLTimeValueGuess::Past), ]); show_enum_dropdown( guess, options, onchange.reform(|enum_value| SQLValueGuess::Time(enum_value)), ) } SQLValueGuess::Datetime(guess) => { let options = HashMap::from([ ("Now".into(), SQLTimeValueGuess::Now), ("Future".into(), SQLTimeValueGuess::Future), ("Past".into(), SQLTimeValueGuess::Past), ]); show_enum_dropdown( guess, options, onchange.reform(|enum_value| SQLValueGuess::Datetime(enum_value)), ) } SQLValueGuess::Bool(guess) => { let options = HashMap::from([ ("Random".into(), SQLBoolValueGuess::Random), ("True".into(), SQLBoolValueGuess::True), ("False".into(), SQLBoolValueGuess::False), ]); show_enum_dropdown( guess, options, onchange.reform(|enum_value| SQLValueGuess::Bool(enum_value)), ) } SQLValueGuess::String(max_size, guess) => { if let Some(constraint) = &column.check_constraint { if let SQLCheckConstraint::OneOf(_) = constraint { return html!("Random Enum"); } } let options = HashMap::from([ ("Lorem Ipsum".into(), SQLStringValueGuess::LoremIpsum), ("First Name".into(), SQLStringValueGuess::FirstName), ("Last Name".into(), SQLStringValueGuess::LastName), ("Full Name".into(), SQLStringValueGuess::FullName), ("Empty".into(), SQLStringValueGuess::Empty), ("Phone number".into(), SQLStringValueGuess::PhoneNumber), ("City name".into(), SQLStringValueGuess::CityName), ("Address".into(), SQLStringValueGuess::Address), ("Email".into(), SQLStringValueGuess::Email), ("URL".into(), SQLStringValueGuess::URL), ]); let max_size = *max_size; show_enum_dropdown( guess, options, onchange.reform(move |enum_value| SQLValueGuess::String(max_size, enum_value)), ) } } }