diff --git a/data/repository.yaml b/data/repository.yaml index b5448ac..9091502 100644 --- a/data/repository.yaml +++ b/data/repository.yaml @@ -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: diff --git a/src/main.rs b/src/main.rs index cb86254..8f47340 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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>) -> 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() }; diff --git a/src/store/form.rs b/src/store/form.rs index 283601f..d2ec6e6 100644 --- a/src/store/form.rs +++ b/src/store/form.rs @@ -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::>(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::>(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::>(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 diff --git a/templates/index.html.tera b/templates/index.html.tera index 978089a..de86908 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -7,6 +7,9 @@ {% block content -%}
+
+

New Record

+
{%- for field in fields %}
@@ -40,6 +43,8 @@ {%- endif -%}
{%- endfor %} - +
+ +
{%- endblock content %} diff --git a/templates/static/style.css b/templates/static/style.css index e02b28d..9a1d7df 100644 --- a/templates/static/style.css +++ b/templates/static/style.css @@ -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,