1
0

finish generate button

This commit is contained in:
Rokas Puzonas 2023-03-05 13:25:23 +02:00
parent dc68f88f9d
commit bb52e3e84f
5 changed files with 50 additions and 25 deletions

View File

@ -12,8 +12,6 @@
<link data-trunk rel="css" href="./static/uno.css" />
</head>
<body
class="bg-dark500 text-light100 fw-400 m-auto max-w-2xl"
>
<body class="bg-dark500 text-light100 fw-400 m-auto max-w-4xl">
</body>
</html>

View File

@ -2,7 +2,7 @@ use std::cell::RefCell;
use std::io::Cursor;
use std::collections::{HashMap, self};
use std::rc::Rc;
use gloo::console::console_dbg;
use gloo::console::{console_dbg, console};
use gloo::file::callbacks::FileReader;
use gloo::file::File;
use gloo::storage::{LocalStorage, Storage};
@ -10,7 +10,7 @@ use web_sys::{DragEvent, Event, FileList, HtmlInputElement, MouseEvent};
use yew::html::TargetCast;
use yew::{html, Callback, Component, Context, Html};
use crate::generate_sql::{SQLValueGuess, generate_table_guessess};
use crate::generate_sql::{SQLValueGuess, generate_table_guessess, generate_fake_entries};
use crate::magicdraw_parser::{parse_project, SQLTableCollection, SQLTable};
use crate::components::sql_column_info::SQLTableColumnInfo;
@ -27,6 +27,7 @@ pub enum Msg {
ShowPrevTable,
AllGoodConfirmation,
GenerateSQL,
UpdateRowsPerTable(u32),
}
pub struct App {
@ -35,7 +36,8 @@ pub struct App {
current_guessess: Vec<Rc<RefCell<HashMap<String, SQLValueGuess>>>>,
currently_shown_table: usize,
all_good_confirmed: bool,
generated_sql: Option<String>
generated_sql: Option<String>,
rows_per_table: u32
}
impl Component for App {
@ -60,7 +62,8 @@ impl Component for App {
currently_shown_table: 0,
all_good_confirmed: true, // TODO: make this false, by default
generated_sql: None,
current_guessess
current_guessess,
rows_per_table: DEFAULT_ROWS_PER_TABLE
}
}
@ -75,6 +78,7 @@ impl Component for App {
let msg = Self::update_current_collection(Some(collections.remove(0)));
ctx.link().send_message(msg);
}
// TODO: show error message
}
self.active_readers.remove(&file_name);
@ -88,6 +92,7 @@ impl Component for App {
let file_name = file_name.clone();
gloo::file::callbacks::read_as_bytes(&file, move |res| {
// TODO: show error message
link.send_message(Msg::Loaded(
file_name,
res.expect("failed to read file"),
@ -137,15 +142,26 @@ impl Component for App {
true
},
Msg::UpdateGenarator(column, generator) => {
console_dbg!(column, generator);
let mut guessess = self.current_guessess[self.currently_shown_table].borrow_mut();
let entry = guessess.get_mut(&column).unwrap();
*entry = generator;
true
},
Msg::GenerateSQL => {
false
let tables = self.current_collection.as_ref().unwrap();
let guessess = self.current_guessess.iter().map(|v| v.borrow()).collect();
// TODO: show error message
if let Ok(result) = generate_fake_entries(tables, &guessess, self.rows_per_table) {
self.generated_sql = Some(result)
} else {
self.generated_sql = None
}
true
},
Msg::UpdateRowsPerTable(rows_per_table) => {
self.rows_per_table = rows_per_table;
false
}
}
}
@ -249,6 +265,12 @@ impl App {
}
fn show_step3(&self, ctx: &Context<Self>) -> Html {
let on_rows_changed = ctx.link().callback(|e: Event| {
let value_str = e.target_unchecked_into::<HtmlInputElement>().value();
let value = value_str.parse().unwrap_or(DEFAULT_ROWS_PER_TABLE);
Msg::UpdateRowsPerTable(value)
});
html! {
<div>
<p class="text-2xl mt-2rem">{ "3. Final settings" }</p>
@ -258,12 +280,14 @@ impl App {
<input
id="gen-amount-input"
class="rounded items-center p-0.3rem bg-dark800 text-light100 w-5rem b-0"
value={DEFAULT_ROWS_PER_TABLE.to_string()}
value={self.rows_per_table.to_string()}
type="number"
onchange={on_rows_changed}
/>
<button
class="block mt-1rem p-1rem btn-emerald"
onclick={ctx.link().callback(|_: MouseEvent| { Msg::GenerateSQL })}
>
{ "Generate" }
</button>
@ -276,9 +300,9 @@ impl App {
html! {
<div>
<p class="text-2xl mt-2rem">{ "4. Copy & Paste" }</p>
<code>
<pre class="bg-dark900 p-0.5rem rounded">
{ sql }
</code>
</pre>
</div>
}
}

View File

@ -1,6 +1,5 @@
use std::{collections::HashMap, str::FromStr};
use gloo::console::console_dbg;
use yew::{Html, html, Callback, TargetCast, AttrValue};
use web_sys::{Event, HtmlInputElement};

View File

@ -1,6 +1,7 @@
use std::{rc::Rc, collections::{HashSet, HashMap}};
use std::{rc::Rc, collections::{HashSet, HashMap}, cell::Ref};
use anyhow::{Result, bail};
use gloo::console::console_dbg;
use rand::{seq::SliceRandom, Rng, rngs::ThreadRng};
use chrono::{Local, NaiveDateTime, Days};
use fake::{faker::{lorem::en::*, name::en::{FirstName, LastName, Name}, phone_number::en::PhoneNumber, internet::en::{DomainSuffix, FreeEmail}, company::en::BsNoun, address::{en::{CityName, StreetName}}}, Fake};
@ -58,7 +59,7 @@ pub enum SQLValueGuess {
// TODO: Check primary key constraint
pub fn generate_fake_entries(
tables: &[Rc<SQLTable>],
value_guessess: &Vec<HashMap<&str, SQLValueGuess>>,
value_guessess: &Vec<Ref<HashMap<String, SQLValueGuess>>>,
rows_per_table: u32
) -> Result<String> {
let mut lines = vec![];
@ -97,13 +98,17 @@ pub fn generate_fake_entries(
let entries = &mut all_entries[table_idx];
for column in &table.columns {
let mut auto_increment_counter = 0;
let value_guess = value_guessess[table_idx].get(column.name.as_str()).expect("Failed to get column guess");
for entry_idx in 0..(rows_per_table as usize) {
if let Some(_) = &column.foreign_key {
if column.foreign_key.is_some() {
for entry_idx in 0..(rows_per_table as usize) {
entries_with_foreign_keys.insert((table_idx, entry_idx));
entries[entry_idx].push("".into());
} else {
}
} else {
let mut auto_increment_counter = 0;
let value_guess = value_guessess[table_idx]
.get(column.name.as_str())
.expect("Failed to get column guess");
for entry_idx in 0..(rows_per_table as usize) {
entries[entry_idx].push(generate_value(&mut rng, &value_guess, &mut auto_increment_counter));
}
}

View File

@ -1,3 +1,5 @@
use anyhow::Result;
use app::App;
mod magicdraw_parser;
@ -10,10 +12,7 @@ mod generate_sql;
// TODO: Fix double rebuilding when on "trunk server". uno css triggers second build.
// TODO: Add simple versioning in frontend for data
fn main() {
// let f = File::open("example.mdzip").unwrap();
// let collections = parse_project(f)?;
// dbg!(collections);
fn main() -> Result<()> {
yew::Renderer::<App>::new().render();
Ok(())
}