Browse Source

add category/group filters

Daniel Sheffield 9 months ago
parent
commit
144d4cf37c
5 changed files with 130 additions and 0 deletions
  1. 34 0
      sqlpage/categories.sql
  2. 2 0
      sqlpage/clear.sql
  3. 26 0
      sqlpage/cookie.sql
  4. 34 0
      sqlpage/groups.sql
  5. 34 0
      sqlpage/products.sql

+ 34 - 0
sqlpage/categories.sql

@@ -1,5 +1,7 @@
 SET title = 'Categories';
 SET products = COALESCE($products, sqlpage.cookie('products'));
+SET categories = COALESCE($categories, sqlpage.cookie('categories'));
+SET groups = COALESCE($groups, sqlpage.cookie('groups'));
 
 DROP TABLE IF EXISTS sqlpage_categories;
 CREATE TEMPORARY TABLE IF NOT EXISTS sqlpage_categories AS (
@@ -38,6 +40,38 @@ FROM (
 ) o(value)
 WHERE value IS NOT NULL
 );
+SET category_options = (
+SELECT
+  json_agg(json_build_object(
+    'label', value,
+    'value', value,
+    'selected', value IN (SELECT v#>>'{}' FROM json_array_elements($categories::json) j(v))
+  ) ORDER BY value)
+FROM (
+  SELECT v#>>'{}'
+  FROM json_array_elements($categories::json) j(v)
+  UNION
+  SELECT category
+  FROM sqlpage_categories
+) o(value)
+WHERE value IS NOT NULL
+);
+SET group_options = (
+SELECT
+  json_agg(json_build_object(
+    'label', value,
+    'value', value,
+    'selected', value IN (SELECT v#>>'{}' FROM json_array_elements($groups::json) j(v))
+  ) ORDER BY value)
+FROM (
+  SELECT v#>>'{}'
+  FROM json_array_elements($groups::json) j(v)
+  UNION
+  SELECT "group"
+  FROM sqlpage_categories
+) o(value)
+WHERE value IS NOT NULL
+);
 
 SELECT 'dynamic' AS component, sqlpage.run_sql('cookie.sql') AS properties;
 SELECT 'dynamic' AS component, sqlpage.run_sql('theme.sql') AS properties;

+ 2 - 0
sqlpage/clear.sql

@@ -1,4 +1,6 @@
 SET products = NULL;
+SET categories = NULL;
+SET groups = NULL;
 SELECT 'dynamic' AS component, sqlpage.run_sql('cookie.sql') AS properties;
 SELECT
   'redirect' AS component,

+ 26 - 0
sqlpage/cookie.sql

@@ -18,3 +18,29 @@ SELECT
   TRUE AS remove
 WHERE $products IS NULL
 ;
+SELECT
+  'cookie' AS component,
+  'categories' AS name,
+  $categories AS value
+WHERE $categories IS NOT NULL
+;
+SELECT
+  'cookie' AS component,
+  'categories' AS name,
+  $categories AS value,
+  TRUE AS remove
+WHERE $categories IS NULL
+;
+SELECT
+  'cookie' AS component,
+  'groups' AS name,
+  $groups AS value
+WHERE $groups IS NOT NULL
+;
+SELECT
+  'cookie' AS component,
+  'groups' AS name,
+  $groups AS value,
+  TRUE AS remove
+WHERE $groups IS NULL
+;

+ 34 - 0
sqlpage/groups.sql

@@ -1,5 +1,7 @@
 SET title = 'Groups';
 SET products = COALESCE($products, sqlpage.cookie('products'));
+SET categories = COALESCE($categories, sqlpage.cookie('categories'));
+SET groups = COALESCE($groups, sqlpage.cookie('groups'));
 
 DROP TABLE IF EXISTS sqlpage_groups;
 CREATE TEMPORARY TABLE IF NOT EXISTS sqlpage_groups AS (
@@ -38,6 +40,38 @@ FROM (
 ) o(value)
 WHERE value IS NOT NULL
 );
+SET category_options = (
+SELECT
+  json_agg(json_build_object(
+    'label', value,
+    'value', value,
+    'selected', value IN (SELECT v#>>'{}' FROM json_array_elements($categories::json) j(v))
+  ) ORDER BY value)
+FROM (
+  SELECT v#>>'{}'
+  FROM json_array_elements($categories::json) j(v)
+  UNION
+  SELECT category
+  FROM sqlpage_groups
+) o(value)
+WHERE value IS NOT NULL
+);
+SET group_options = (
+SELECT
+  json_agg(json_build_object(
+    'label', value,
+    'value', value,
+    'selected', value IN (SELECT v#>>'{}' FROM json_array_elements($groups::json) j(v))
+  ) ORDER BY value)
+FROM (
+  SELECT v#>>'{}'
+  FROM json_array_elements($groups::json) j(v)
+  UNION
+  SELECT "group"
+  FROM sqlpage_groups
+) o(value)
+WHERE value IS NOT NULL
+);
 
 SELECT 'dynamic' AS component, sqlpage.run_sql('cookie.sql') AS properties;
 SELECT 'dynamic' AS component, sqlpage.run_sql('theme.sql') AS properties;

+ 34 - 0
sqlpage/products.sql

@@ -1,5 +1,7 @@
 SET title = 'Products';
 SET products = COALESCE($products, sqlpage.cookie('products'));
+SET categories = COALESCE($categories, sqlpage.cookie('categories'));
+SET groups = COALESCE($groups, sqlpage.cookie('groups'));
 
 DROP TABLE IF EXISTS sqlpage_products;
 CREATE TEMPORARY TABLE IF NOT EXISTS sqlpage_products AS (
@@ -38,6 +40,38 @@ FROM (
 ) o(value)
 WHERE value IS NOT NULL
 );
+SET category_options = (
+SELECT
+  json_agg(json_build_object(
+    'label', value,
+    'value', value,
+    'selected', value IN (SELECT v#>>'{}' FROM json_array_elements($categories::json) j(v))
+  ) ORDER BY value)
+FROM (
+  SELECT v#>>'{}'
+  FROM json_array_elements($categories::json) j(v)
+  UNION
+  SELECT category
+  FROM sqlpage_products
+) o(value)
+WHERE value IS NOT NULL
+);
+SET group_options = (
+SELECT
+  json_agg(json_build_object(
+    'label', value,
+    'value', value,
+    'selected', value IN (SELECT v#>>'{}' FROM json_array_elements($groups::json) j(v))
+  ) ORDER BY value)
+FROM (
+  SELECT v#>>'{}'
+  FROM json_array_elements($groups::json) j(v)
+  UNION
+  SELECT "group"
+  FROM sqlpage_products
+) o(value)
+WHERE value IS NOT NULL
+);
 
 SELECT 'dynamic' AS component, sqlpage.run_sql('cookie.sql') AS properties;
 SELECT 'dynamic' AS component, sqlpage.run_sql('theme.sql') AS properties;