Heftig rekatorering

This commit is contained in:
Håkon Størdal 2025-09-03 18:20:08 +02:00
parent 201280dc54
commit 35ecc5f7a7
20 changed files with 220 additions and 451 deletions

12
package-lock.json generated
View file

@ -9,8 +9,7 @@
"version": "0.0.1", "version": "0.0.1",
"dependencies": { "dependencies": {
"@types/pg": "^8.15.5", "@types/pg": "^8.15.5",
"pg": "^8.16.3", "pg": "^8.16.3"
"zod": "^4.1.5"
}, },
"devDependencies": { "devDependencies": {
"@sveltejs/adapter-node": "^5.3.1", "@sveltejs/adapter-node": "^5.3.1",
@ -2733,15 +2732,6 @@
"integrity": "sha512-rAbqEGa8ovJy4pyBxZM70hg4pE6gDgaQ0Sl9M3enG3I0d6H4XSAM3GeNGLKnsBpuijUow064sf7ww1nutC5/3w==", "integrity": "sha512-rAbqEGa8ovJy4pyBxZM70hg4pE6gDgaQ0Sl9M3enG3I0d6H4XSAM3GeNGLKnsBpuijUow064sf7ww1nutC5/3w==",
"dev": true, "dev": true,
"license": "MIT" "license": "MIT"
},
"node_modules/zod": {
"version": "4.1.5",
"resolved": "https://registry.npmjs.org/zod/-/zod-4.1.5.tgz",
"integrity": "sha512-rcUUZqlLJgBC33IT3PNMgsCq6TzLQEG/Ei/KTCU0PedSWRMAXoOUN+4t/0H+Q8bdnLPdqUYnvboJT0bn/229qg==",
"license": "MIT",
"funding": {
"url": "https://github.com/sponsors/colinhacks"
}
} }
} }
} }

View file

@ -29,7 +29,6 @@
}, },
"dependencies": { "dependencies": {
"@types/pg": "^8.15.5", "@types/pg": "^8.15.5",
"pg": "^8.16.3", "pg": "^8.16.3"
"zod": "^4.1.5"
} }
} }

View file

@ -1,21 +0,0 @@
import type { HandleClientError } from '@sveltejs/kit';
import { dev } from '$app/environment';
// Handle client-side errors
export const handleError: HandleClientError = ({ error, event }) => {
console.error('Client error:', error);
// Log additional context in development
if (dev) {
console.error('Event details:', {
url: event.url,
route: event.route?.id
});
}
// Return user-friendly error message
return {
message: dev ? String(error) : 'Something went wrong',
code: 'CLIENT_ERROR'
};
};

View file

@ -3,7 +3,7 @@ import { dev } from '$app/environment';
import { db } from '$lib/db'; import { db } from '$lib/db';
export const init: ServerInit = async () => { export const init: ServerInit = async () => {
db.createTables(); await db.createTables();
console.log('Tables created'); console.log('Tables created');
}; };

View file

@ -1 +1,26 @@
<svg xmlns="http://www.w3.org/2000/svg" width="107" height="128" viewBox="0 0 107 128"><title>svelte-logo</title><path d="M94.157 22.819c-10.4-14.885-30.94-19.297-45.792-9.835L22.282 29.608A29.92 29.92 0 0 0 8.764 49.65a31.5 31.5 0 0 0 3.108 20.231 30 30 0 0 0-4.477 11.183 31.9 31.9 0 0 0 5.448 24.116c10.402 14.887 30.942 19.297 45.791 9.835l26.083-16.624A29.92 29.92 0 0 0 98.235 78.35a31.53 31.53 0 0 0-3.105-20.232 30 30 0 0 0 4.474-11.182 31.88 31.88 0 0 0-5.447-24.116" style="fill:#ff3e00"/><path d="M45.817 106.582a20.72 20.72 0 0 1-22.237-8.243 19.17 19.17 0 0 1-3.277-14.503 18 18 0 0 1 .624-2.435l.49-1.498 1.337.981a33.6 33.6 0 0 0 10.203 5.098l.97.294-.09.968a5.85 5.85 0 0 0 1.052 3.878 6.24 6.24 0 0 0 6.695 2.485 5.8 5.8 0 0 0 1.603-.704L69.27 76.28a5.43 5.43 0 0 0 2.45-3.631 5.8 5.8 0 0 0-.987-4.371 6.24 6.24 0 0 0-6.698-2.487 5.7 5.7 0 0 0-1.6.704l-9.953 6.345a19 19 0 0 1-5.296 2.326 20.72 20.72 0 0 1-22.237-8.243 19.17 19.17 0 0 1-3.277-14.502 17.99 17.99 0 0 1 8.13-12.052l26.081-16.623a19 19 0 0 1 5.3-2.329 20.72 20.72 0 0 1 22.237 8.243 19.17 19.17 0 0 1 3.277 14.503 18 18 0 0 1-.624 2.435l-.49 1.498-1.337-.98a33.6 33.6 0 0 0-10.203-5.1l-.97-.294.09-.968a5.86 5.86 0 0 0-1.052-3.878 6.24 6.24 0 0 0-6.696-2.485 5.8 5.8 0 0 0-1.602.704L37.73 51.72a5.42 5.42 0 0 0-2.449 3.63 5.79 5.79 0 0 0 .986 4.372 6.24 6.24 0 0 0 6.698 2.486 5.8 5.8 0 0 0 1.602-.704l9.952-6.342a19 19 0 0 1 5.295-2.328 20.72 20.72 0 0 1 22.237 8.242 19.17 19.17 0 0 1 3.277 14.503 18 18 0 0 1-8.13 12.053l-26.081 16.622a19 19 0 0 1-5.3 2.328" style="fill:#fff"/></svg> <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">
<defs>
<linearGradient id="gradient" x1="0%" y1="0%" x2="100%" y2="100%">
<stop offset="0%" style="stop-color:#4ade80;stop-opacity:1" />
<stop offset="100%" style="stop-color:#059669;stop-opacity:1" />
</linearGradient>
</defs>
<!-- Dumbbell weights -->
<rect x="2" y="10" width="6" height="12" rx="2" fill="url(#gradient)" stroke="#047857" stroke-width="0.5"/>
<rect x="24" y="10" width="6" height="12" rx="2" fill="url(#gradient)" stroke="#047857" stroke-width="0.5"/>
<!-- Dumbbell bar -->
<rect x="8" y="14.5" width="16" height="3" rx="1.5" fill="#374151" stroke="#1f2937" stroke-width="0.5"/>
<!-- Grip texture lines -->
<line x1="12" y1="14.8" x2="12" y2="17.2" stroke="#6b7280" stroke-width="0.3"/>
<line x1="14" y1="14.8" x2="14" y2="17.2" stroke="#6b7280" stroke-width="0.3"/>
<line x1="16" y1="14.8" x2="16" y2="17.2" stroke="#6b7280" stroke-width="0.3"/>
<line x1="18" y1="14.8" x2="18" y2="17.2" stroke="#6b7280" stroke-width="0.3"/>
<line x1="20" y1="14.8" x2="20" y2="17.2" stroke="#6b7280" stroke-width="0.3"/>
<!-- Small highlight on weights for 3D effect -->
<rect x="3" y="11" width="1.5" height="3" rx="0.5" fill="#6ee7b7" opacity="0.7"/>
<rect x="26.5" y="11" width="1.5" height="3" rx="0.5" fill="#6ee7b7" opacity="0.7"/>
</svg>

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

Before After
Before After

View file

@ -1,11 +0,0 @@
<script lang="ts">
import { testConnection } from './db.remote';
</script>
<svelte:boundary>
<p>Here be connection: {testConnection().current}</p>
{#snippet pending()}
<p>loading...</p>
{/snippet}
</svelte:boundary>

View file

@ -1,107 +0,0 @@
<script lang="ts">
import ExerciseField from "../../routes/ExerciseField.svelte";
import WorkoutStatCard from "../../routes/WorkoutStatCard.svelte";
// Example of how to easily add new exercises to the system
// Just define the configuration and use the reusable components
let squats = $state(0);
let burpees = $state(0);
// New exercise configurations
const newExerciseConfigs = {
squats: {
name: 'Squats',
icon: '🦵',
color: 'purple' as const,
quickAddOptions: [
{ label: '+15', value: 15 },
{ label: '+30', value: 30 }
],
max: 9999,
step: 1
},
burpees: {
name: 'Burpees',
icon: '🤸',
color: 'yellow' as const,
quickAddOptions: [
{ label: '+5', value: 5 },
{ label: '+10', value: 10 }
],
max: 999,
step: 1
}
};
function handleSquatsChange(value: number) {
squats = value;
}
function handleBurpeesChange(value: number) {
burpees = value;
}
</script>
<div class="mx-auto max-w-md rounded-lg bg-white p-6 shadow-lg">
<h2 class="mb-6 text-center text-2xl font-bold text-gray-800">Example: Adding New Exercises</h2>
<!-- Form Fields using reusable ExerciseField component -->
<div class="mb-6">
<ExerciseField
label={newExerciseConfigs.squats.name}
icon={newExerciseConfigs.squats.icon}
name="squats"
bind:value={squats}
onchange={handleSquatsChange}
quickAddOptions={newExerciseConfigs.squats.quickAddOptions}
color="blue"
max={newExerciseConfigs.squats.max}
step={newExerciseConfigs.squats.step}
/>
<ExerciseField
label={newExerciseConfigs.burpees.name}
icon={newExerciseConfigs.burpees.icon}
name="burpees"
bind:value={burpees}
onchange={handleBurpeesChange}
quickAddOptions={newExerciseConfigs.burpees.quickAddOptions}
color="green"
max={newExerciseConfigs.burpees.max}
step={newExerciseConfigs.burpees.step}
/>
</div>
<!-- Display Cards using reusable WorkoutStatCard component -->
<div class="grid grid-cols-2 gap-4">
<WorkoutStatCard
icon={newExerciseConfigs.squats.icon}
value={squats}
label={newExerciseConfigs.squats.name}
color="blue"
/>
<WorkoutStatCard
icon={newExerciseConfigs.burpees.icon}
value={burpees}
label={newExerciseConfigs.burpees.name}
color="green"
/>
</div>
<!-- Instructions -->
<div class="mt-6 rounded-md bg-gray-50 p-4">
<h3 class="mb-2 text-sm font-medium text-gray-700">How to add new exercises:</h3>
<ol class="list-inside list-decimal space-y-1 text-sm text-gray-600">
<li>Define exercise config with name, icon, color, and quick-add options</li>
<li>
Use <code class="rounded bg-gray-200 px-1">ExerciseField</code> component for input forms
</li>
<li>
Use <code class="rounded bg-gray-200 px-1">WorkoutStatCard</code> component for display
</li>
<li>Add to <code class="rounded bg-gray-200 px-1">workout-utils.ts</code> for consistency</li>
</ol>
</div>
</div>

View file

@ -1,29 +0,0 @@
<script lang="ts">
import type { ColorExercise } from '../../routes/workoutData';
interface Props {
label: string;
onclick: () => void;
color: ColorExercise;
}
let { label, onclick, color = 'blue' }: Props = $props();
const colorClasses = {
blue: 'bg-blue-100 text-blue-700 hover:bg-blue-200 focus:ring-blue-500',
green: 'bg-green-100 text-green-700 hover:bg-green-200 focus:ring-green-500',
orange: 'bg-orange-100 text-orange-700 hover:bg-orange-200 focus:ring-orange-500',
red: 'bg-red-100 text-red-700 hover:bg-red-200 focus:ring-red-500',
purple: 'bg-purple-100 text-purple-700 hover:bg-purple-200 focus:ring-purple-500'
};
</script>
<button
type="button"
{onclick}
class="rounded px-3 py-1 text-xs focus:ring-2 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 {colorClasses[
color
]}"
>
{label}
</button>

View file

@ -1,6 +0,0 @@
import { query } from '$app/server';
import { db } from '$lib/db';
export const testConnection = query(async () => {
return db.testConnection();
});

View file

@ -1,15 +1,17 @@
import { Pool } from 'pg'; import { Pool } from 'pg';
import { env } from '$env/dynamic/private';
// Get DATABASE_URL with fallback // Get DATABASE_URL with fallback
const getDatabaseUrl = const getDatabaseUrl = () => {
'postgres://postgres:voHMlrqwwzlZcqWW3oCYvkmWnmOAFntc4nYJmTRVsr7bm8CiOoxqAKv1zVn5Opsq@192.168.0.133:5432/postgres'; return env.DATABASE_URL || '';
};
// Lazy pool creation - only create when actually needed // Lazy pool creation - only create when actually needed
let pool: Pool | null = null; let pool: Pool | null = null;
const getPool = () => { const getPool = () => {
if (!pool) { if (!pool) {
pool = new Pool({ pool = new Pool({
connectionString: getDatabaseUrl, connectionString: getDatabaseUrl(),
ssl: false, ssl: false,
options: '-c timezone=Europe/Oslo' options: '-c timezone=Europe/Oslo'
}); });

View file

@ -1,17 +1,6 @@
export type ColorExercise = 'blue' | 'green' | 'orange' | 'red' | 'purple'; import type { WorkoutData, ExerciseConfig } from './types';
export interface ExerciseConfig { export const exerciseConfigs: Record<keyof WorkoutData, ExerciseConfig> = {
name: string;
icon: string;
color: ColorExercise;
quickAddOptions: Array<{ label: string; value: number }>;
min?: number;
max?: number;
step?: number;
unit?: string;
}
export const exerciseConfigs: Record<string, ExerciseConfig> = {
pushups: { pushups: {
name: 'Push-ups', name: 'Push-ups',
icon: '💪', icon: '💪',
@ -35,7 +24,7 @@ export const exerciseConfigs: Record<string, ExerciseConfig> = {
step: 1 step: 1
}, },
plankSeconds: { plankSeconds: {
name: 'Plank', name: 'Planke',
icon: '🧘', icon: '🧘',
color: 'orange', color: 'orange',
quickAddOptions: [ quickAddOptions: [
@ -44,19 +33,8 @@ export const exerciseConfigs: Record<string, ExerciseConfig> = {
], ],
max: 9999, max: 9999,
step: 1, step: 1,
unit: 'seconds' unit: 'sekunder',
}, formatter: (number) => formatTime(number)
runKm: {
name: 'Running',
icon: '🏃',
color: 'red',
quickAddOptions: [
{ label: '+1km', value: 1 },
{ label: '+2.5km', value: 2.5 }
],
max: 999.99,
step: 0.01,
unit: 'km'
}, },
hangups: { hangups: {
name: 'Hang-ups', name: 'Hang-ups',
@ -68,5 +46,35 @@ export const exerciseConfigs: Record<string, ExerciseConfig> = {
], ],
max: 9999, max: 9999,
step: 1 step: 1
},
runKm: {
name: 'Running',
icon: '🏃',
color: 'red',
quickAddOptions: [
{ label: '+1km', value: 1 },
{ label: '+2.5km', value: 2.5 }
],
max: 999.99,
step: 0.01,
unit: 'km',
formatter: (value) => `${value.toFixed(2)} km`
} }
}; };
function formatTime(seconds: number): string {
if (seconds === 0) return '0 seconds';
if (seconds < 60) return `${seconds} seconds`;
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
if (remainingSeconds === 0) return `${minutes} minutes`;
return `${minutes}m ${remainingSeconds}s`;
}
// Get strongly typed entries directly from exerciseConfigs
export const exercises = Object.entries(exerciseConfigs).map(([key, config]) => ({
key: key as keyof WorkoutData,
config
}));

4
src/lib/workout/index.ts Normal file
View file

@ -0,0 +1,4 @@
// Barrel exports for workout module
export * from './types';
export * from './exercises';
export * from './utils';

32
src/lib/workout/types.ts Normal file
View file

@ -0,0 +1,32 @@
export type ColorExercise = 'blue' | 'green' | 'orange' | 'red' | 'purple';
export type WorkoutData = {
pushups: number;
situps: number;
plankSeconds: number;
hangups: number;
runKm: number;
};
export interface ExerciseConfig {
name: string;
icon: string;
color: ColorExercise;
quickAddOptions: Array<{ label: string; value: number }>;
min?: number;
max?: number;
step?: number;
unit?: string;
formatter?: (value: number) => string;
}
export interface QuickAddOption {
label: string;
value: number;
}
export interface WorkoutApiResponse<T> {
success: boolean;
data?: T;
message?: string;
}

13
src/lib/workout/utils.ts Normal file
View file

@ -0,0 +1,13 @@
import type { WorkoutData } from './types';
export function getTodayDateString(locale: string = 'nb-NO'): string {
return new Date().toLocaleDateString(locale);
}
export const exampleWorkout: WorkoutData = {
pushups: 100,
situps: 50,
plankSeconds: 0,
hangups: 0,
runKm: 4.0
};

View file

@ -1,38 +1,41 @@
<script lang="ts"> <script lang="ts">
import QuickAddButton from '$lib/components/QuickAddButton.svelte'; import type { ExerciseConfig, ColorExercise } from '$lib/workout';
import type { ExerciseConfig } from './workoutData';
interface Props extends ExerciseConfig { interface Props extends ExerciseConfig {
label: string;
value: number; value: number;
onchange: (value: number) => void;
defaultValue: number; defaultValue: number;
} }
let { let {
label,
icon,
name,
value = $bindable(), value = $bindable(),
onchange,
min = 0, min = 0,
max = 9999, max = 9999,
step = 1, step = 1,
quickAddOptions, color = 'blue',
defaultValue, ...restProps
color = 'blue'
}: Props = $props(); }: Props = $props();
// Destructure the remaining props for easier access
const { icon, name, quickAddOptions, defaultValue, unit } = restProps;
const label = unit ? `${icon} ${name} (${unit})` : `${icon} ${name}`;
function quickAdd(amount: number) { function quickAdd(amount: number) {
const newValue = Math.max(min, value + amount); const newValue = Math.max(min, value + amount);
value = step < 1 ? Number(newValue.toFixed(2)) : newValue; value = step < 1 ? Number(newValue.toFixed(2)) : newValue;
onchange(value);
} }
const colorClasses = {
blue: 'bg-blue-100 text-blue-700 hover:bg-blue-200 focus:ring-blue-500',
green: 'bg-green-100 text-green-700 hover:bg-green-200 focus:ring-green-500',
orange: 'bg-orange-100 text-orange-700 hover:bg-orange-200 focus:ring-orange-500',
red: 'bg-red-100 text-red-700 hover:bg-red-200 focus:ring-red-500',
purple: 'bg-purple-100 text-purple-700 hover:bg-purple-200 focus:ring-purple-500'
};
</script> </script>
<div class="mb-6"> <div class="mb-6">
<label for={name} class="mb-2 block text-sm font-medium text-gray-700"> <label for={name} class="mb-2 block text-sm font-medium text-gray-700">
{icon}
{label} {label}
</label> </label>
<div class="flex items-center space-x-2"> <div class="flex items-center space-x-2">
@ -48,8 +51,21 @@
required={true} required={true}
class="flex-1 rounded-md border border-gray-300 px-3 py-2 focus:ring-2 focus:ring-blue-500 focus:outline-none" class="flex-1 rounded-md border border-gray-300 px-3 py-2 focus:ring-2 focus:ring-blue-500 focus:outline-none"
/> />
{#each quickAddOptions as option} {#each quickAddOptions as option}
<QuickAddButton label={option.label} onclick={() => quickAdd(option.value)} {color} /> {@render quickAddButton(option.label, () => quickAdd(option.value), color)}
{/each} {/each}
</div> </div>
</div> </div>
{#snippet quickAddButton(label: string, onclick: () => void, color: ColorExercise)}
<button
type="button"
{onclick}
class="rounded px-3 py-1 text-xs focus:ring-2 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 {colorClasses[
color
]}"
>
{label}
</button>
{/snippet}

View file

@ -1,11 +1,31 @@
<script lang="ts"> <script lang="ts">
import {
getTodayDateString,
type WorkoutData,
type ColorExercise,
exercises
} from '$lib/workout';
import { getTodaysWorkout, getWorkoutHistory } from './workout.remote'; import { getTodaysWorkout, getWorkoutHistory } from './workout.remote';
import { getTodayDateString, formatTime, formatDistance } from './workoutUtils';
import WorkoutStatCard from './WorkoutStatCard.svelte';
import { exerciseConfigs } from './workoutData';
// Today's date for display // Today's date for display
let todayDate = getTodayDateString(); let todayDate = getTodayDateString();
// Color classes for the stat cards
const colorClasses = {
blue: 'bg-blue-50 text-blue-700',
green: 'bg-green-50 text-green-700',
orange: 'bg-orange-50 text-orange-700',
red: 'bg-red-50 text-red-700',
purple: 'bg-purple-50 text-purple-700'
};
const textColorClasses = {
blue: 'text-blue-600',
green: 'text-green-600',
orange: 'text-orange-600',
red: 'text-red-600',
purple: 'text-purple-600'
};
</script> </script>
<div class="mx-auto max-w-md rounded-lg bg-white p-6 shadow-lg"> <div class="mx-auto max-w-md rounded-lg bg-white p-6 shadow-lg">
@ -26,74 +46,26 @@
{:else} {:else}
<!-- Workout Stats Grid --> <!-- Workout Stats Grid -->
<div class="grid grid-cols-2 gap-4"> <div class="grid grid-cols-2 gap-4">
<!-- Push-ups --> {#each exercises as { key, config }}
<WorkoutStatCard {@render workoutStatCard(
icon={exerciseConfigs.pushups.icon} config.icon,
value={workoutData.pushups} workoutData[key],
label={exerciseConfigs.pushups.name} config.name,
color={exerciseConfigs.pushups.color} config.color,
/> config.formatter
)}
<!-- Sit-ups --> {/each}
<WorkoutStatCard
icon={exerciseConfigs.situps.icon}
value={workoutData.situps}
label={exerciseConfigs.situps.name}
color={exerciseConfigs.situps.color}
/>
<!-- Plank -->
<WorkoutStatCard
icon={exerciseConfigs.plankSeconds.icon}
value={workoutData.plankSeconds}
label={exerciseConfigs.plankSeconds.name}
color={exerciseConfigs.plankSeconds.color}
formatter={formatTime}
/>
<!-- Hangups -->
<WorkoutStatCard
icon={exerciseConfigs.hangups.icon}
value={workoutData.hangups}
label={exerciseConfigs.hangups.name}
color={exerciseConfigs.hangups.color}
/>
<!-- Running -->
<WorkoutStatCard
icon={exerciseConfigs.runKm.icon}
value={workoutData.runKm}
label={exerciseConfigs.runKm.name}
color={exerciseConfigs.runKm.color}
formatter={formatDistance}
/>
</div> </div>
{@const allWorkouData = (await getWorkoutHistory()).data} {@const allWorkouData = (await getWorkoutHistory()).data}
<!-- Summary Stats --> <!-- Summary Stats -->
<div class="mt-6 rounded-md bg-gray-50 p-4"> <div class="mt-6 rounded-md bg-gray-50 p-4">
<h3 class="mb-2 font-medium text-gray-700">📊 Summary for last 7 days</h3> <h3 class="mb-2 font-medium text-gray-700">📊 Summary for last 7 days</h3>
<div class="space-y-1 text-sm text-gray-600"> <div class="space-y-1 text-sm text-gray-600">
<div class="flex justify-between"> {#each exercises as { key, config }}
<span>Total pushups:</span> {@render summaryStatRow(config.name, allWorkouData[key], config.formatter)}
<span class="font-medium">{allWorkouData.pushups}</span> {/each}
</div>
<div class="flex justify-between">
<span>Total situps:</span>
<span class="font-medium">{workoutData.situps}</span>
</div>
<div class="flex justify-between">
<span>Plank time:</span>
<span class="font-medium">{formatTime(workoutData.plankSeconds)}</span>
</div>
<div class="flex justify-between">
<span>Hang ups:</span>
<span class="font-medium">{workoutData.hangups}</span>
</div>
<div class="flex justify-between">
<span>Distance:</span>
<span class="font-medium">{formatDistance(workoutData.runKm)}</span>
</div>
</div> </div>
</div> </div>
{/if} {/if}
@ -118,3 +90,24 @@
{/snippet} {/snippet}
</svelte:boundary> </svelte:boundary>
</div> </div>
{#snippet workoutStatCard(
icon: string,
value: number,
label: string,
color: ColorExercise = 'blue',
formatter?: (value: number) => string
)}
<div class="rounded-lg p-4 text-center {colorClasses[color]}">
<div class="text-2xl">{icon}</div>
<div class="text-lg font-bold">{formatter ? formatter(value) : value}</div>
<div class="text-sm {textColorClasses[color]}">{label}</div>
</div>
{/snippet}
{#snippet summaryStatRow(label: string, value: number, formatter?: (value: number) => string)}
<div class="flex justify-between">
<span>{label}</span>
<span class="font-medium">{formatter ? formatter(value) : value}</span>
</div>
{/snippet}

View file

@ -1,14 +1,13 @@
<script lang="ts"> <script lang="ts">
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { saveWorkout, getTodaysWorkout, type WorkOutData } from './workout.remote';
import ExerciseField from './ExerciseField.svelte'; import ExerciseField from './ExerciseField.svelte';
import { exerciseConfigs } from './workoutData'; import { getTodayDateString, exampleWorkout, type WorkoutData, exercises } from '$lib/workout';
import { getTodayDateString, exampleWorkout } from './workoutUtils'; import { getTodaysWorkout, saveWorkout } from './workout.remote';
let todayDate = getTodayDateString(); let todayDate = getTodayDateString();
// Form state // Form state
let form: WorkOutData = { let form: WorkoutData = {
pushups: 0, pushups: 0,
situps: 0, situps: 0,
plankSeconds: 0, plankSeconds: 0,
@ -24,10 +23,6 @@
function loadExample() { function loadExample() {
form = { ...exampleWorkout }; form = { ...exampleWorkout };
} }
function handleFieldChange(field: keyof WorkOutData, value: number) {
form[field] = value;
}
</script> </script>
<div class="mx-auto max-w-md rounded-lg bg-white p-6 shadow-lg"> <div class="mx-auto max-w-md rounded-lg bg-white p-6 shadow-lg">
@ -39,74 +34,10 @@
<!-- Main Form --> <!-- Main Form -->
<form {...saveWorkout}> <form {...saveWorkout}>
<!-- Push-ups Field --> <!-- Exercise Fields -->
<ExerciseField {#each exercises as { config, key }}
label={exerciseConfigs.pushups.name} <ExerciseField {...config} bind:value={form[key]} defaultValue={form[key]} />
icon={exerciseConfigs.pushups.icon} {/each}
name="pushups"
bind:value={form.pushups}
defaultValue={form.pushups}
onchange={(value) => handleFieldChange('pushups', value)}
quickAddOptions={exerciseConfigs.pushups.quickAddOptions}
color={exerciseConfigs.pushups.color}
max={exerciseConfigs.pushups.max}
step={exerciseConfigs.pushups.step}
/>
<!-- Sit-ups Field -->
<ExerciseField
label={exerciseConfigs.situps.name}
icon={exerciseConfigs.situps.icon}
name="situps"
bind:value={form.situps}
onchange={(value) => handleFieldChange('situps', value)}
defaultValue={form.situps}
quickAddOptions={exerciseConfigs.situps.quickAddOptions}
color={exerciseConfigs.situps.color}
max={exerciseConfigs.situps.max}
step={exerciseConfigs.situps.step}
/>
<!-- Plank Field -->
<ExerciseField
label="{exerciseConfigs.plankSeconds.name} (seconds)"
icon={exerciseConfigs.plankSeconds.icon}
name="plankSeconds"
bind:value={form.plankSeconds}
onchange={(value) => handleFieldChange('plankSeconds', value)}
defaultValue={form.plankSeconds}
quickAddOptions={exerciseConfigs.plankSeconds.quickAddOptions}
color={exerciseConfigs.plankSeconds.color}
max={exerciseConfigs.plankSeconds.max}
step={exerciseConfigs.plankSeconds.step}
/>
<!-- Running Field -->
<ExerciseField
label={exerciseConfigs.hangups.name}
icon={exerciseConfigs.hangups.icon}
name="hangups"
bind:value={form.hangups}
onchange={(value) => handleFieldChange('hangups', value)}
defaultValue={form.hangups}
quickAddOptions={exerciseConfigs.hangups.quickAddOptions}
color={exerciseConfigs.hangups.color}
max={exerciseConfigs.hangups.max}
step={exerciseConfigs.hangups.step}
/>
<ExerciseField
label="{exerciseConfigs.runKm.name} (km)"
icon={exerciseConfigs.runKm.icon}
name="runKm"
bind:value={form.runKm}
onchange={(value) => handleFieldChange('runKm', value)}
defaultValue={form.runKm}
quickAddOptions={exerciseConfigs.runKm.quickAddOptions}
color={exerciseConfigs.runKm.color}
max={exerciseConfigs.runKm.max}
step={exerciseConfigs.runKm.step}
/>
<!-- Action Buttons --> <!-- Action Buttons -->
<div class="mb-4 flex space-x-3"> <div class="mb-4 flex space-x-3">
@ -114,7 +45,11 @@
type="submit" type="submit"
class="flex-1 rounded-md bg-blue-600 px-4 py-2 font-medium text-white hover:bg-blue-700 focus:ring-2 focus:ring-blue-500 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50" class="flex-1 rounded-md bg-blue-600 px-4 py-2 font-medium text-white hover:bg-blue-700 focus:ring-2 focus:ring-blue-500 focus:outline-none disabled:cursor-not-allowed disabled:opacity-50"
> >
💾 Save Workout {#if saveWorkout.pending !== 0}
Loading...
{:else}
💾 Save Workout
{/if}
</button> </button>
</div> </div>
</form> </form>
@ -129,8 +64,6 @@
</div> </div>
{/if} {/if}
{#snippet pending()}{/snippet}
<!-- Example Section --> <!-- Example Section -->
<section class="rounded-md bg-gray-50 p-4"> <section class="rounded-md bg-gray-50 p-4">
<h3 class="mb-2 text-sm font-medium text-gray-700">Quick Example:</h3> <h3 class="mb-2 text-sm font-medium text-gray-700">Quick Example:</h3>

View file

@ -1,37 +0,0 @@
<script lang="ts">
import type { ColorExercise } from './workoutData';
interface Props {
icon: string;
value: number;
label: string;
color?: ColorExercise;
formatter?: (value: number) => string;
}
let { icon, value, label, color = 'blue', formatter }: Props = $props();
const colorClasses = {
blue: 'bg-blue-50 text-blue-700',
green: 'bg-green-50 text-green-700',
orange: 'bg-orange-50 text-orange-700',
red: 'bg-red-50 text-red-700',
purple: 'bg-purple-50 text-purple-700'
};
const textColorClasses = {
blue: 'text-blue-600',
green: 'text-green-600',
orange: 'text-orange-600',
red: 'text-red-600',
purple: 'text-purple-600'
};
let displayValue = $derived(formatter ? formatter(value) : value);
</script>
<div class="rounded-lg p-4 text-center {colorClasses[color]}">
<div class="text-2xl">{icon}</div>
<div class="text-lg font-bold">{displayValue}</div>
<div class="text-sm {textColorClasses[color]}">{label}</div>
</div>

View file

@ -1,14 +1,7 @@
import { command, form, query } from '$app/server'; import { command, form, query } from '$app/server';
import { db } from '$lib/db'; import { db } from '$lib/db';
import { error } from '@sveltejs/kit'; import { error } from '@sveltejs/kit';
import type { WorkoutData } from '../lib/workout/types';
export interface WorkOutData {
pushups: number;
situps: number;
plankSeconds: number;
hangups: number;
runKm: number;
}
export const saveWorkout = form(async (data) => { export const saveWorkout = form(async (data) => {
let pushups = Number(data.get('pushups')); let pushups = Number(data.get('pushups'));
@ -81,7 +74,7 @@ export const getTodaysWorkout = query(async () => {
// Take all rows and add them up into each category // Take all rows and add them up into each category
const row = result.rows[0]; const row = result.rows[0];
const workoutData: WorkOutData = { const workoutData: WorkoutData = {
pushups: Number(row.pushups), pushups: Number(row.pushups),
situps: Number(row.situps), situps: Number(row.situps),
plankSeconds: Number(row.plank_time_seconds), plankSeconds: Number(row.plank_time_seconds),
@ -113,7 +106,7 @@ export const getWorkoutHistory = query(async (days: number = 7) => {
// Take all rows and add them up into each category // Take all rows and add them up into each category
const rows = result.rows; const rows = result.rows;
const workoutData: WorkOutData = { const workoutData: WorkoutData = {
pushups: rows.reduce((sum, row) => sum + Number(row.pushups), 0), pushups: rows.reduce((sum, row) => sum + Number(row.pushups), 0),
situps: rows.reduce((sum, row) => sum + Number(row.situps), 0), situps: rows.reduce((sum, row) => sum + Number(row.situps), 0),
plankSeconds: rows.reduce((sum, row) => sum + Number(row.plank_time_seconds), 0), plankSeconds: rows.reduce((sum, row) => sum + Number(row.plank_time_seconds), 0),

View file

@ -1,28 +0,0 @@
import type { WorkOutData } from './workout.remote';
export function formatTime(seconds: number): string {
if (seconds === 0) return '0 seconds';
if (seconds < 60) return `${seconds} seconds`;
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
if (remainingSeconds === 0) return `${minutes} minutes`;
return `${minutes}m ${remainingSeconds}s`;
}
export function formatDistance(km: number): string {
return `${km} km`;
}
export function getTodayDateString(locale: string = 'nb-NO'): string {
return new Date().toLocaleDateString(locale);
}
export const exampleWorkout: WorkOutData = {
pushups: 100,
situps: 50,
plankSeconds: 0,
hangups: 0,
runKm: 4.0
};