Skip to content

Commit dca2913

Browse files
committed
Refactor medication and purchase state management functions for improved clarity and consistency
1 parent f6fe2e1 commit dca2913

11 files changed

Lines changed: 200 additions & 197 deletions

File tree

src/routes/apps/(app)/medication-tracker/MedicationList.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@
381381
{medicationFrequency || 'Select frequency'}
382382
</Select.Trigger>
383383
<Select.Content>
384-
{#each medState.frequencyPresets as preset (preset)}
384+
{#each medState.getFrequencyPresets() as preset (preset)}
385385
<Select.Item value={preset} label={preset}>
386386
{preset}
387387
</Select.Item>
@@ -468,7 +468,7 @@
468468
{medicationFrequency || 'Select frequency'}
469469
</Select.Trigger>
470470
<Select.Content>
471-
{#each medState.frequencyPresets as preset (preset)}
471+
{#each medState.getFrequencyPresets() as preset (preset)}
472472
<Select.Item value={preset} label={preset}>
473473
{preset}
474474
</Select.Item>

src/routes/apps/(app)/medication-tracker/states.svelte.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,21 @@ export const medicationColors = [
6262
];
6363

6464
// Frequency presets
65-
export const frequencyPresets = [
66-
'Once daily',
67-
'Twice daily',
68-
'Three times daily',
69-
'Four times daily',
70-
'Every 4 hours',
71-
'Every 6 hours',
72-
'Every 8 hours',
73-
'Every 12 hours',
74-
'As needed',
75-
'Weekly',
76-
'Custom'
77-
];
65+
export function getFrequencyPresets() {
66+
return [
67+
'Once daily',
68+
'Twice daily',
69+
'Three times daily',
70+
'Four times daily',
71+
'Every 4 hours',
72+
'Every 6 hours',
73+
'Every 8 hours',
74+
'Every 12 hours',
75+
'As needed',
76+
'Weekly',
77+
'Custom'
78+
];
79+
}
7880

7981
// Persisted state for treatment sessions
8082
export const treatmentSessions = new PersistedState<TreatmentSession[]>(

src/routes/apps/(app)/purchase-tracker/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@
191191
let availableCategories = $derived(purchaseState.getAllCategories());
192192
193193
// Get currency options
194-
let currencyOptions = $derived(purchaseState.supportedCurrencies);
194+
let currencyOptions = $derived(purchaseState.getSupportedCurrencies());
195195
196196
// Handle adding new item
197197
function handleAddItem() {

src/routes/apps/(app)/purchase-tracker/AddEditItemDialog.svelte

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
</script>
4040

4141
<Dialog.Root bind:open>
42-
<Dialog.Content class="sm:max-w-[500px] max-h-[90vh] overflow-y-auto">
42+
<Dialog.Content class="max-h-[90vh] overflow-y-auto sm:max-w-[500px]">
4343
<Dialog.Header class="space-y-3 pb-4">
4444
<Dialog.Title>{editingItem ? 'Edit Item' : 'Add New Item'}</Dialog.Title>
4545
<Dialog.Description>
@@ -64,7 +64,7 @@
6464
</DropdownMenu.Trigger>
6565
<DropdownMenu.Content class="w-full">
6666
{#each availableCategories as category}
67-
<DropdownMenu.Item onclick={() => itemCategory = category.id}>
67+
<DropdownMenu.Item onclick={() => (itemCategory = category.id)}>
6868
{category.icon}
6969
{category.name}
7070
</DropdownMenu.Item>
@@ -96,7 +96,7 @@
9696
</DropdownMenu.Trigger>
9797
<DropdownMenu.Content>
9898
{#each currencyOptions as currency}
99-
<DropdownMenu.Item onclick={() => itemDefaultCurrency = currency.code}>
99+
<DropdownMenu.Item onclick={() => (itemDefaultCurrency = currency.code)}>
100100
{#if currency.icon}
101101
<img src={currency.icon} alt={currency.symbol} class="mr-2 inline h-4 w-4" />
102102
{:else}
@@ -111,12 +111,10 @@
111111
</div>
112112
</div>
113113
<Dialog.Footer>
114-
<Button variant="outline" onclick={onCancel}>
115-
Cancel
116-
</Button>
114+
<Button variant="outline" onclick={onCancel}>Cancel</Button>
117115
<Button onclick={onSave}>
118116
{editingItem ? 'Update Item' : 'Add Item'}
119117
</Button>
120118
</Dialog.Footer>
121119
</Dialog.Content>
122-
</Dialog.Root>
120+
</Dialog.Root>

src/routes/apps/(app)/purchase-tracker/AddEditPurchaseDialog.svelte

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
</script>
5252

5353
<Dialog.Root bind:open>
54-
<Dialog.Content class="sm:max-w-[500px] max-h-[90vh] overflow-y-auto">
54+
<Dialog.Content class="max-h-[90vh] overflow-y-auto sm:max-w-[500px]">
5555
<Dialog.Header class="space-y-3 pb-4">
5656
<Dialog.Title>
5757
{editingPurchase ? 'Edit Purchase Record' : 'Add Purchase Record'}
@@ -118,7 +118,7 @@
118118
</DropdownMenu.Trigger>
119119
<DropdownMenu.Content>
120120
{#each currencyOptions as currency}
121-
<DropdownMenu.Item onclick={() => purchaseCurrency = currency.code}>
121+
<DropdownMenu.Item onclick={() => (purchaseCurrency = currency.code)}>
122122
{#if currency.icon}
123123
<img src={currency.icon} alt={currency.symbol} class="mr-2 inline h-4 w-4" />
124124
{:else}
@@ -160,12 +160,10 @@
160160
</div>
161161
</div>
162162
<Dialog.Footer>
163-
<Button variant="outline" onclick={onCancel}>
164-
Cancel
165-
</Button>
163+
<Button variant="outline" onclick={onCancel}>Cancel</Button>
166164
<Button onclick={onSave}>
167165
{editingPurchase ? 'Update Purchase' : 'Add Purchase'}
168166
</Button>
169167
</Dialog.Footer>
170168
</Dialog.Content>
171-
</Dialog.Root>
169+
</Dialog.Root>

src/routes/apps/(app)/purchase-tracker/PurchaseRecord.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,4 @@
7474
</Button>
7575
</div>
7676
</CardContent>
77-
</Card>
77+
</Card>

src/routes/apps/(app)/purchase-tracker/states.svelte.ts

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @wc-ignore-file
12
import { PersistedState } from 'runed';
23
import icons from 'currency-icons';
34

@@ -34,37 +35,31 @@ export interface PurchaseCategory {
3435
}
3536

3637
// Default categories
37-
export const defaultCategories: PurchaseCategory[] = [
38-
{ id: 'fuel', name: 'Fuel', color: '#ef4444', icon: '⛽' },
39-
{ id: 'groceries', name: 'Groceries', color: '#22c55e', icon: '🛒' },
40-
{ id: 'dining', name: 'Dining Out', color: '#f59e0b', icon: '🍽️' },
41-
{ id: 'utilities', name: 'Utilities', color: '#3b82f6', icon: '⚡' },
42-
{ id: 'entertainment', name: 'Entertainment', color: '#8b5cf6', icon: '🎬' },
43-
{ id: 'shopping', name: 'Shopping', color: '#ec4899', icon: '🛍️' },
44-
{ id: 'healthcare', name: 'Healthcare', color: '#06b6d4', icon: '🏥' },
45-
{ id: 'transportation', name: 'Transportation', color: '#84cc16', icon: '🚗' },
46-
{ id: 'other', name: 'Other', color: '#6b7280', icon: '📦' }
47-
];
38+
export function getDefaultCategories(): PurchaseCategory[] {
39+
return [
40+
{ id: 'fuel', name: 'Fuel', color: '#ef4444', icon: '⛽' },
41+
{ id: 'groceries', name: 'Groceries', color: '#22c55e', icon: '🛒' },
42+
{ id: 'dining', name: 'Dining Out', color: '#f59e0b', icon: '🍽️' },
43+
{ id: 'utilities', name: 'Utilities', color: '#3b82f6', icon: '⚡' },
44+
{ id: 'entertainment', name: 'Entertainment', color: '#8b5cf6', icon: '🎬' },
45+
{ id: 'shopping', name: 'Shopping', color: '#ec4899', icon: '🛍️' },
46+
{ id: 'healthcare', name: 'Healthcare', color: '#06b6d4', icon: '🏥' },
47+
{ id: 'transportation', name: 'Transportation', color: '#84cc16', icon: '🚗' },
48+
{ id: 'other', name: 'Other', color: '#6b7280', icon: '📦' }
49+
];
50+
}
4851

4952
// Supported currencies - using currency-icons package
50-
export const supportedCurrencies = [
51-
'USD',
52-
'EUR',
53-
'GBP',
54-
'JPY',
55-
'CAD',
56-
'AUD',
57-
'CHF',
58-
'CNY',
59-
'INR',
60-
'BRL',
61-
'NGN'
62-
].map((code) => ({
63-
code,
64-
symbol: icons[code]?.symbol || code,
65-
name: icons[code]?.name || code,
66-
icon: icons[code]?.icon || ''
67-
}));
53+
export function getSupportedCurrencies() {
54+
return ['USD', 'EUR', 'GBP', 'JPY', 'CAD', 'AUD', 'CHF', 'CNY', 'INR', 'BRL', 'NGN'].map(
55+
(code) => ({
56+
code,
57+
symbol: icons[code]?.symbol || code,
58+
name: icons[code]?.name || code,
59+
icon: icons[code]?.icon || ''
60+
})
61+
);
62+
}
6863

6964
// Create persisted states
7065
const itemsState = new PersistedState<Item[]>('purchase-items', [], {
@@ -230,12 +225,12 @@ export function deleteCustomCategory(id: string) {
230225

231226
// Get all categories (default + custom)
232227
export function getAllCategories(): PurchaseCategory[] {
233-
return [...defaultCategories, ...customCategoriesState.current];
228+
return [...getDefaultCategories(), ...customCategoriesState.current];
234229
}
235230

236231
// Get currency info
237232
export function getCurrencyInfo(code: string) {
238-
return supportedCurrencies.find((c) => c.code === code);
233+
return getSupportedCurrencies().find((c) => c.code === code);
239234
}
240235

241236
// Calculate statistics

src/routes/apps/(app)/purchase-tracker/states.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ describe('Purchase Tracker State Management', () => {
175175
});
176176

177177
it('should have supported currencies with proper structure', () => {
178-
expect(purchaseState.supportedCurrencies).toBeDefined();
179-
expect(Array.isArray(purchaseState.supportedCurrencies)).toBe(true);
180-
expect(purchaseState.supportedCurrencies.length).toBeGreaterThan(0);
178+
expect(purchaseState.getSupportedCurrencies()).toBeDefined();
179+
expect(Array.isArray(purchaseState.getSupportedCurrencies())).toBe(true);
180+
expect(purchaseState.getSupportedCurrencies().length).toBeGreaterThan(0);
181181

182-
const usdCurrency = purchaseState.supportedCurrencies.find((c) => c.code === 'USD');
182+
const usdCurrency = purchaseState.getSupportedCurrencies().find((c) => c.code === 'USD');
183183
expect(usdCurrency).toBeDefined();
184184
expect(usdCurrency?.code).toBe('USD');
185185
expect(usdCurrency?.symbol).toBe('$');
@@ -221,7 +221,7 @@ describe('Purchase Tracker State Management', () => {
221221
const customCategoryId = purchaseState.addCustomCategory('Custom Category', '#ff0000', '🔥');
222222

223223
const allCategories = purchaseState.getAllCategories();
224-
expect(allCategories.length).toBeGreaterThan(purchaseState.defaultCategories.length);
224+
expect(allCategories.length).toBeGreaterThan(purchaseState.getDefaultCategories().length);
225225

226226
const customCategory = allCategories.find((c) => c.id === customCategoryId);
227227
expect(customCategory).toBeDefined();

src/routes/apps/(app)/smoke-free-tracker/+page.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@
7777
);
7878
7979
let randomQuote = $derived(
80-
smokeState.motivationalQuotes[Math.floor(Math.random() * smokeState.motivationalQuotes.length)]
80+
smokeState.getMotivationalQuotes()[
81+
Math.floor(Math.random() * smokeState.getMotivationalQuotes().length)
82+
]
8183
);
8284
8385
// Initialize

src/routes/apps/(app)/smoke-free-tracker/MilestonesView.svelte

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { Badge } from '@/ui/badge';
55
import { Progress } from '@/ui/progress';
66
import type { SmokingAttempt } from './states.svelte';
7-
import { defaultMilestones, getStreakMinutes } from './states.svelte';
7+
import { getDefaultMilestones, getStreakMinutes } from './states.svelte';
88
99
interface Props {
1010
statistics: {
@@ -18,7 +18,7 @@
1818
let { statistics, attempt }: Props = $props();
1919
2020
let allMilestones = $derived(
21-
defaultMilestones.map((m) => {
21+
getDefaultMilestones().map((m) => {
2222
const achieved = statistics.achievedMilestones.some((am) => am.id === m.id);
2323
const isNext = statistics.nextMilestone?.id === m.id;
2424
const progress =
@@ -78,12 +78,12 @@
7878
{/if}
7979
</div>
8080

81-
<p class="text-sm text-muted-foreground">{milestone.description}</p>
81+
<p class="text-muted-foreground text-sm">{milestone.description}</p>
8282

8383
{#if !milestone.achieved && milestone.isNext}
8484
<div class="mt-3">
8585
<Progress value={milestone.progress} class="h-2" />
86-
<p class="mt-1 text-xs text-muted-foreground">
86+
<p class="text-muted-foreground mt-1 text-xs">
8787
{milestone.progress.toFixed(0)}% complete
8888
</p>
8989
</div>

0 commit comments

Comments
 (0)