Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement filter helper and use it to suppress reactive variables for non-widget fields #128

Merged
merged 14 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/templates/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use serde_json::Value;

pub mod merge;
pub mod uniq_lines;
pub mod filter;

use merge::register_merge;
use uniq_lines::register_uniq_lines;
use filter::register_filter;

pub fn register_helpers<'a>(h: Handlebars<'a>) -> Handlebars<'a> {
let h = register_concat_helper(h);
Expand All @@ -19,6 +21,7 @@ pub fn register_helpers<'a>(h: Handlebars<'a>) -> Handlebars<'a> {
let h = register_pluralize_helpers(h);
let h = register_merge(h);
let h = register_uniq_lines(h);
let h = register_filter(h);

h
}
Expand Down
114 changes: 114 additions & 0 deletions src/templates/helpers/filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use handlebars::{
Context, Handlebars, Helper, HelperDef, RenderContext, RenderError,
ScopedJson,
};
use serde_json::{json, Value, Map};

#[derive(Clone, Copy)]
pub struct FilterHelper;

pub enum FilterableValues {
Array(Vec<Value>),
Object(Map<String, Value>), // imported from serde_json
pdaoust marked this conversation as resolved.
Show resolved Hide resolved
}

/// A Handlebars helper to filter an iterable JSON value.
/// It receives the value to be filtered and a string containing the condition predicate,
/// then uses Handlebars' truthy logic to filter the items in the value.
/// It also supports the `#if` helper's `includeZero` optional parameter.
impl HelperDef for FilterHelper {
fn call_inner<'reg: 'rc, 'rc>(
&self,
h: &Helper<'reg, 'rc>,
r: &'reg Handlebars<'reg>,
_ctx: &'rc Context,
_rc: &mut RenderContext<'reg, 'rc>,
) -> Result<ScopedJson<'reg, 'rc>, RenderError> {
let mut params = h.params().iter();
let value = params
.next()
.ok_or(RenderError::new(
"Filter helper: Param not found for index 0; must be value to be filtered",
))?
.value();

let condition = params
.next()
.ok_or(RenderError::new("Filter helper: Param not found for index 1; must be string containing filter condition predicate"))?
.value()
.as_str()
.ok_or(RenderError::new("Filter helper: filter condition predicate must be a string"))?;

let include_zero = h
.hash_get("includeZero")
.and_then(|v| v.value().as_bool())
.unwrap_or(false);

let items: FilterableValues = match value {
Value::Array(items) => Ok(
FilterableValues::Array(items
.iter()
.cloned()
.collect()
)
),
Value::Object(items) => Ok(
FilterableValues::Object(items.clone())
),
_ => Err(RenderError::new("Filter helper: value to be filtered must be an array or object"))
}?;

// This template allows us to evaluate the condition according to Handlebars'
// available helper functions and existing truthiness logic.
let template = format!(
"{}{}{}{}",
"{{#if ",
include_zero.then_some("includeZero=true").unwrap_or(""),
condition,
"}}true{{else}}false{{/if}}"
);

match items {
FilterableValues::Array(items) => {
let mut filtered_array = vec![];
for item in items.iter() {
match r.render_template(&template, item) {
Ok(s) => {
if s.as_str() == "true" {
filtered_array.push(item);
}
},
Err(e) => {
return Err(e);
}
}
}
Ok(ScopedJson::Derived(json!(filtered_array)))
},
FilterableValues::Object(object) => {
let mut filtered_object = Map::new();
for key in object.keys() {
if let Some(v) = object.get(key) {
match r.render_template(&template, v) {
Ok(s) => {
if s.as_str() == "true" {
filtered_object.insert(key.into(), v.clone());
}
},
Err(e) => {
return Err(e);
}
}
}
}
Ok(ScopedJson::Derived(json!(filtered_object)))
}
}
}
}

pub fn register_filter<'a>(mut h: Handlebars<'a>) -> Handlebars<'a> {
h.register_helper("filter", Box::new(FilterHelper));

h
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ let {{camel_case field_name}}: Array<{{> (concat field_type.type "/type")}} | un

let errorSnackbar: Snackbar;

$: {{#each entry_type.fields}}{{#if widget}}{{camel_case field_name}}{{#unless @last}}, {{/unless}}{{/if}}{{/each}};
$: {{#each (filter entry_type.fields "widget")}}{{camel_case field_name}}{{#unless @last}}, {{/unless}}{{/each}};
$: is{{pascal_case entry_type.name}}Valid = true{{#each entry_type.fields}}{{#if widget}}{{#if (eq cardinality "single")}} && {{> (concat field_type.type "/" widget "/is-valid") variable_to_validate=(camel_case field_name) }}{{/if}}{{#if (eq cardinality "vector")}} && {{camel_case field_name}}.every(e => {{> (concat field_type.type "/" widget "/is-valid") variable_to_validate="e" }}){{/if}}{{/if}}{{/each}};

onMount(() => {
Expand All @@ -55,7 +55,7 @@ onMount(() => {

async function update{{pascal_case entry_type.name}}() {

const {{camel_case entry_type.name}}: {{pascal_case entry_type.name}} = {
const {{camel_case entry_type.name}}: {{pascal_case entry_type.name}} = {
{{#each entry_type.fields}}
{{#if widget}}
{{#if (eq cardinality "single") }}
Expand Down Expand Up @@ -86,7 +86,7 @@ async function update{{pascal_case entry_type.name}}() {
updated_{{snake_case entry_type.name}}: {{camel_case entry_type.name}}
}
});

dispatch('{{kebab_case entry_type.name}}-updated', { actionHash: updateRecord.signed_action.hashed.hash });
} catch (e) {
errorSnackbar.labelText = `Error updating the {{lower_case entry_type.name}}: ${e.data.data}`;
Expand All @@ -99,7 +99,7 @@ async function update{{pascal_case entry_type.name}}() {
</mwc-snackbar>
<div style="display: flex; flex-direction: column">
<span style="font-size: 18px">Edit {{pascal_case entry_type.name}}</span>

{{#each entry_type.fields}}
{{#if widget}}
<div style="margin-bottom: 16px">
Expand All @@ -108,7 +108,7 @@ async function update{{pascal_case entry_type.name}}() {
{{else}}
{{> Vec/edit/render field_name=field_name field_type=field_type widget=widget }}
{{/if}}

</div>

{{/if}}
Expand All @@ -121,7 +121,7 @@ async function update{{pascal_case entry_type.name}}() {
on:click={() => dispatch('edit-canceled')}
style="flex: 1; margin-right: 16px"
></mwc-button>
<mwc-button
<mwc-button
raised
label="Save"
disabled={!is{{pascal_case entry_type.name}}Valid}
Expand Down