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: fields:
category: category:
type: "free_enum" type: "free_enum"
generic_code:
type: "free_enum"
code: code:
type: "string" type: "free_enum"
value: value:
type: "string" type: "string"
package: package:

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

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

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

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

Loading…
Cancel
Save