fix tag fields not loading pre-set value correctly

session-crate
Ondřej Hruška 5 years ago
parent 23931b1620
commit 8e5185f643
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 4
      data/repository.yaml
  2. 11
      src/main.rs
  3. 58
      src/store/form.rs
  4. 7
      templates/index.html.tera
  5. 10
      templates/static/style.css

@ -2,10 +2,8 @@ model:
fields:
category:
type: "free_enum"
generic_code:
type: "free_enum"
code:
type: "string"
type: "free_enum"
value:
type: "string"
package:

@ -3,6 +3,8 @@
#[macro_use] extern crate rocket;
#[macro_use] extern crate serde;
use serde_json::json;
//use rocket::request::FromSegments;
//use rocket::http::uri::Segments;
use rocket_contrib::serve::StaticFiles;
@ -28,10 +30,17 @@ struct FormContext<'a> {
fn index(store : State<RwLock<Store>>) -> Template {
let rg = store.read();
let tags = json!(["foo", "bar"]);
let indexes = &rg.index;
let context = FormContext {
fields: rg.model.fields.iter().map(|(key, field)| {
RenderedField::from_template_field(key, field, None, indexes)
let value = if key == "tags" {
Some(&tags)
} else {
None
};
RenderedField::from_template_field(key, field, value, indexes)
}).collect()
};

@ -43,17 +43,9 @@ impl<'a> RenderedField<'a> {
match &field.kind {
FieldKind::String => {
rendered.kind = "string";
if let Some(Value::String(s)) = value {
rendered.value = Cow::Borrowed(&s.as_str());
}
}
FieldKind::Text => {
rendered.kind = "text";
if let Some(Value::String(s)) = value {
rendered.value = Cow::Borrowed(&s.as_str());
}
}
FieldKind::Bool { default } => {
rendered.kind = "bool";
@ -117,26 +109,11 @@ impl<'a> RenderedField<'a> {
let group = enum_group.as_ref().unwrap_or(key);
let options = index.free_enums.get(group).unwrap_or(&EMPTY_VEC);
rendered.options = Some(options);
if let Some(Value::String(s)) = value {
rendered.value = Cow::Borrowed(&s.as_str());
}
}
FieldKind::Tags { options } => {
rendered.kind = "tags";
rendered.options = Some(options);
if value.is_some() {
rendered.value = serde_json::from_value::<Vec<String>>(value.unwrap().clone())
.unwrap().join(" ").into();
}
if let Some(Value::Array(items)) = value {
rendered.tags_json = serde_json::to_string(items).unwrap();
} else {
rendered.tags_json = "[]".into();
};
rendered.all_tags_json = serde_json::to_string(options).unwrap();
}
FieldKind::FreeTags { tag_group } => {
@ -144,20 +121,33 @@ impl<'a> RenderedField<'a> {
let group = tag_group.as_ref().unwrap_or(key);
let options = index.free_tags.get(group).unwrap_or(&EMPTY_VEC);
if value.is_some() {
rendered.value = serde_json::from_value::<Vec<String>>(value.unwrap().clone())
.unwrap().join(" ").into();
}
rendered.options = Some(options);
rendered.all_tags_json = "[]".into();
}
}
if let Some(Value::Array(items)) = value {
rendered.tags_json = serde_json::to_string(items).unwrap();
// Shared code by multiple variants
match field.kind {
FieldKind::Text | FieldKind::String => {
if let Some(Value::String(s)) = value {
rendered.value = Cow::Borrowed(&s.as_str());
}
},
FieldKind::Enum { .. } | FieldKind::FreeEnum { .. } => {
if let Some(Value::String(s)) = value {
rendered.value = Cow::Borrowed(&s.as_str());
}
},
FieldKind::Tags { .. } | FieldKind::FreeTags { .. } => {
if let Some(v) = value {
rendered.value = serde_json::from_value::<Vec<String>>(v.clone())
.unwrap().join(" ").into();
rendered.tags_json = serde_json::to_string(v).unwrap();
} else {
rendered.tags_json = "[]".into();
};
rendered.options = Some(options);
rendered.tags_json = serde_json::to_string(options).unwrap();
}
}
},
_ => {}
}
rendered

@ -7,6 +7,9 @@
{% block content -%}
<form action="/add" method="POST" class="Form">
<div class="Row indented">
<h1>New Record</h1>
</div>
{%- for field in fields %}
<!-- {{ field.key }} -->
<div class="Row">
@ -40,6 +43,8 @@
{%- endif -%}
</div>
{%- endfor %}
<button type="submit">Add</button>
<div class="Row indented">
<button type="submit">Add</button>
</div>
</form>
{%- endblock content %}

@ -6,11 +6,21 @@ html, textarea, select {
font-family: "IBM Plex", "DejaVu Sans", "Helvetica", sans-serif;
}
.Form {
display: block;
width: 900px;
margin: 0 auto;
}
.Form .Row {
display: flex;
padding: .25rem;
}
.Form .Row.indented {
padding-left: 10.25rem;
}
input[type="text"],
input[type="number"],
textarea,

Loading…
Cancel
Save