From 74e859ef7182c30afed7e193393ab1eb863e6f70 Mon Sep 17 00:00:00 2001 From: Timothy Cardoza Date: Wed, 18 Mar 2026 18:04:39 -0500 Subject: [PATCH] standard pricing with suppliers fixes --- src/store/appStore.ts | 3 +- src/views/StandardPricing.tsx | 55 +++++++++++++++++------------------ 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/src/store/appStore.ts b/src/store/appStore.ts index d38e9f8..09cdbdd 100644 --- a/src/store/appStore.ts +++ b/src/store/appStore.ts @@ -172,7 +172,8 @@ export const appStore = { async loadSuppliers() { try { const records = await pb.collection(COLLECTIONS.STANDARD_PRICES).getFullList({ - fields: 'supplier' + fields: 'supplier', + requestKey: null }); const unique = [...new Set(records.map(r => r.supplier).filter(Boolean))].sort() as string[]; setSuppliers(unique); diff --git a/src/views/StandardPricing.tsx b/src/views/StandardPricing.tsx index 5ba2125..0c620f0 100644 --- a/src/views/StandardPricing.tsx +++ b/src/views/StandardPricing.tsx @@ -1,4 +1,4 @@ -import { createSignal, onMount, For, createMemo } from 'solid-js'; +import { createSignal, onMount, For, createMemo, createResource } from 'solid-js'; import type { Component } from 'solid-js'; import { Trash2, Plus, RefreshCw, Edit2, Save, X, ChevronDown } from 'lucide-solid'; import { pb, COLLECTIONS } from '../utils/db'; @@ -6,8 +6,15 @@ import type { StandardPrice } from '../types'; import { appStore } from '../store/appStore'; const StandardPricing: Component = () => { - const [prices, setPrices] = createSignal([]); - const [isLoading, setIsLoading] = createSignal(true); + const fetchPrices = async () => { + const records = await pb.collection(COLLECTIONS.STANDARD_PRICES).getFullList({ + sort: 'category,name', + requestKey: null + }); + return records as unknown as StandardPrice[]; + }; + + const [prices, { refetch }] = createResource(fetchPrices); const [newCategory, setNewCategory] = createSignal(''); const [newName, setNewName] = createSignal(''); @@ -21,29 +28,14 @@ const StandardPricing: Component = () => { const [editSupplier, setEditSupplier] = createSignal(''); const categories = createMemo(() => { - return [...new Set(prices().map(p => p.category))].sort(); + return [...new Set((prices() || []).map(p => p.category))].sort(); }); const itemNames = createMemo(() => { - return [...new Set(prices().map(p => p.name))].sort(); + return [...new Set((prices() || []).map(p => p.name))].sort(); }); - const loadPrices = async () => { - setIsLoading(true); - try { - const records = await pb.collection(COLLECTIONS.STANDARD_PRICES).getFullList({ - sort: 'category,name' - }); - setPrices(records as unknown as StandardPrice[]); - } catch (error) { - console.error('Error loading standard prices:', error); - } finally { - setIsLoading(false); - } - }; - onMount(() => { - loadPrices(); appStore.loadSuppliers(); }); @@ -57,8 +49,8 @@ const StandardPricing: Component = () => { price: newPrice(), supplier: newSupplier() }; - const added = await pb.collection(COLLECTIONS.STANDARD_PRICES).create(record); - setPrices([...prices(), added as unknown as StandardPrice]); + await pb.collection(COLLECTIONS.STANDARD_PRICES).create(record); + refetch(); appStore.loadSuppliers(); // Refresh global list // Reset fields @@ -96,7 +88,7 @@ const StandardPricing: Component = () => { supplier: editSupplier() }; await pb.collection(COLLECTIONS.STANDARD_PRICES).update(id, updates); - setPrices(prices().map(p => p.id === id ? { ...p, ...updates } : p)); + refetch(); setEditingId(null); appStore.loadSuppliers(); // Refresh global list } catch (error) { @@ -109,7 +101,7 @@ const StandardPricing: Component = () => { if (!confirm('Are you sure you want to delete this standard price?')) return; try { await pb.collection(COLLECTIONS.STANDARD_PRICES).delete(id); - setPrices(prices().filter(p => p.id !== id)); + refetch(); appStore.loadSuppliers(); // Refresh global list } catch (error) { console.error('Error deleting price:', error); @@ -127,10 +119,10 @@ const StandardPricing: Component = () => { @@ -205,14 +197,19 @@ const StandardPricing: Component = () => {

Existing Prices

- {isLoading() ? ( + {prices.loading && !prices() ? (
Loading...
- ) : prices().length === 0 ? ( + ) : (prices()?.length || 0) === 0 ? (
No standard prices added yet.
) : ( -
+
+ {prices.loading && ( +
+ +
+ )}