Browse Source

convert filters to json

Daniel Sheffield 8 months ago
parent
commit
576994d087
3 changed files with 103 additions and 88 deletions
  1. 4 13
      sqlpage/sqlpage/internal/entry.sql
  2. 23 75
      sqlpage/sqlpage/internal/nav.sql
  3. 76 0
      sqlpage/sqlpage/json/filters.json

+ 4 - 13
sqlpage/sqlpage/internal/entry.sql

@@ -52,7 +52,6 @@ SELECT 'redirect' AS component
 WHERE ($page IS NULL OR $page = '')
 ;
 
---SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/'||$page);
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/tags.sql') AS properties
 WHERE $apply::bool OR $clear::bool;
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/transactions.sql') AS properties
@@ -61,16 +60,8 @@ SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/products.sql') AS p
 WHERE $apply::bool OR $clear::bool;
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/units.sql') AS properties
 WHERE $apply::bool OR $clear::bool;
--- SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/'||sql||'.sql')
--- FROM (SELECT UNNEST(ARRAY[
---   'tags',
---   'transactions',
---   'products'
--- ])) AS data(sql)
--- ;
 
-
-SET product_options = (
+SET products_options = (
 SELECT
   json_agg(json_build_object(
     'label', value,
@@ -86,7 +77,7 @@ FROM (
 ) o(value)
 WHERE value IS NOT NULL
 );
-SET category_options = (
+SET categories_options = (
 SELECT
   json_agg(json_build_object(
     'label', value,
@@ -102,7 +93,7 @@ FROM (
 ) o(value)
 WHERE value IS NOT NULL
 );
-SET group_options = (
+SET groups_options = (
 SELECT
   json_agg(json_build_object(
     'label', value,
@@ -119,7 +110,7 @@ FROM (
 WHERE value IS NOT NULL
 );
 
-SET tag_options = (
+SET tags_options = (
 SELECT
   json_agg(json_build_object(
     'label', value,

+ 23 - 75
sqlpage/sqlpage/internal/nav.sql

@@ -1,5 +1,4 @@
 SET input_log_scale = COALESCE($input_log_scale, 'checkbox');
---SET input_log_scale = 'hidden'; -- log scale now working yet
 SET input_start = COALESCE($input_start, 'date');
 SET input_end = COALESCE($input_end, 'date');
 SET input_unit_mass = COALESCE($input_unit_mass, 'select');
@@ -26,71 +25,29 @@ SELECT 'hidden' AS type
 , 'title' AS name
 , $title AS value
 ;
-SELECT 'Product' AS label
-, 'products[]' AS name
-, 4 AS width
-, 'select' AS type
-, TRUE AS dropdown
-, TRUE AS multiple
-, $product_options AS options
-;
-SELECT 'Categories' AS label
-, 'categories[]' AS name
-, 4 AS width
-, 'select' AS type
-, TRUE AS dropdown
-, TRUE AS multiple
-, $category_options AS options
-;
-SELECT 'Groups' AS label
-, 'groups[]' AS name
-, 4 AS width
-, 'select' AS type
-, TRUE AS dropdown
-, TRUE AS multiple
-, $group_options AS options
-;
-SELECT 'Unit (vol.)' AS label
-, 'unit_volume' AS name
-, 2 AS width
-, $input_unit_volume AS type
-, $unit_volume_options AS options
-;
-SELECT 'Unit (mass)' AS label
-, 'unit_mass' AS name
-, 2 AS width
-, $input_unit_mass AS type
-, $unit_mass AS value
-, $unit_mass_options AS options
-;
-SELECT 'Unit (count)' AS label
-, 'unit_count[]' AS name
-, 4 AS width
-, $input_unit_count AS type
-, TRUE AS multiple
-, TRUE AS dropdown
-, '' AS value
-, $unit_count_options AS options
-;
-SELECT 'Tags' AS label
-, 'tags[]' AS name
-, 4 AS width
-, 'select' AS "type"
-, TRUE AS dropdown
-, TRUE AS multiple
-, $tag_options AS options
-;
-SELECT 'From' AS label
-, 'start' AS name
-, 2 AS width
-, $start AS value
-, $input_start AS type
-;
-SELECT 'To' AS label
-, 'end' AS name
-, 2 AS width
-, $end AS value
-, $input_end AS type
+
+SELECT v#>>'{name}' AS name
+, v#>>'{label}' AS label
+, COALESCE(c.value#>>'{type}', v#>>'{type}') AS type
+, v#>>'{dropdown}' AS dropdown
+, v#>>'{multiple}' AS multiple
+, v#>>'{width}' AS width
+, o.value AS options
+, CASE v#>>'{type}'
+  WHEN 'checkbox' THEN 'true'
+  ELSE v.value#>>'{}'
+  END AS value
+, CASE v#>>'{type}'
+  WHEN 'checkbox' THEN (v.value#>>'{}')::bool
+  ELSE NULL
+END AS checked
+FROM json_array_elements(sqlpage.read_file_as_text('sqlpage/json/filters.json')::json) j(v)
+LEFT JOIN json_each($filter_config::json) c(k,value)
+ON c.k = (v#>>'{name}')
+LEFT JOIN json_each(sqlpage.variables()::json) o(key, value)
+ON o.key = replace(v#>>'{name}', '[]', '')||'_options'
+LEFT JOIN json_each(sqlpage.variables()::json) v(key, value)
+ON v.key = (v#>>'{name}')
 ;
 
 SELECT 'Apply' AS value
@@ -108,12 +65,3 @@ SELECT 'Clear' AS value
 , 'submit' AS type
 , '/grocery/internal/clear.sql' AS formaction
 ;
-
-SELECT 'Log Scale' AS label
-, 'log_scale' AS name
-, $input_log_scale AS type
-, TRUE AS disabled
-, 2 AS width
-, lower($log_scale) = 'true' AS checked
-, TRUE AS value
-;

+ 76 - 0
sqlpage/sqlpage/json/filters.json

@@ -0,0 +1,76 @@
+[
+    {
+        "name": "products[]",
+        "label": "Products",
+        "type": "select",
+        "dropdown": true,
+        "multiple": true,
+        "width": 4
+    },
+    {
+        "name": "categories[]",
+        "label": "Categories",
+        "type": "select",
+        "dropdown": true,
+        "multiple": true,
+        "width": 4
+    },
+    {
+        "name": "groups[]",
+        "label": "Groups",
+        "type": "select",
+        "dropdown": true,
+        "multiple": true,
+        "width": 4
+    },
+    {
+        "name": "tags[]",
+        "label": "Tags",
+        "type": "select",
+        "dropdown": true,
+        "multiple": true,
+        "width": 4
+    },
+    {
+        "name": "unit_volume",
+        "label": "Unit (vol.)",
+        "type": "select",
+        "multiple": false,
+        "dropdown": true,
+        "width": 2
+    },
+    {
+        "name": "unit_mass",
+        "label": "Unit (mass)",
+        "type": "select",
+        "multiple": false,
+        "dropdown": true,
+        "width": 2
+    },
+    {
+        "name": "unit_count[]",
+        "label": "Unit (count)",
+        "type": "select",
+        "multiple": true,
+        "dropdown": true,
+        "width": 4
+    },
+    {
+        "name": "start",
+        "label": "From",
+        "type": "date",
+        "width": 2
+    },
+    {
+        "name": "end",
+        "label": "To",
+        "type": "date",
+        "width": 2
+    },
+    {
+        "name": "log_scale",
+        "label": "Log Scale",
+        "type": "checkbox",
+        "width": 2
+    }
+]