Workout Management tests done

This commit is contained in:
AG
2025-12-09 18:11:55 +02:00
parent f32661d892
commit 2352ac04d6
14 changed files with 557 additions and 67 deletions

View File

@@ -2,6 +2,7 @@ import React, { useState } from 'react';
import { Calendar, Clock, TrendingUp, Gauge, Pencil, Trash2, X, Save, ArrowRight, ArrowUp, Timer, Activity, Dumbbell, Percent } from 'lucide-react';
import { WorkoutSession, ExerciseType, WorkoutSet, Language } from '../types';
import { t } from '../services/i18n';
import { formatSetMetrics } from '../utils/setFormatting';
import { useSession } from '../context/SessionContext';
import { Button } from './ui/Button';
import { Card } from './ui/Card';
@@ -243,13 +244,7 @@ const History: React.FC<HistoryProps> = ({ lang }) => {
{set.side && <span className="ml-2 text-xs font-medium text-on-surface-variant">{t(set.side.toLowerCase() as any, lang)}</span>}
</div>
<div className="text-sm text-on-surface-variant mt-1">
{set.type === ExerciseType.STRENGTH && `${set.weight || 0}kg x ${set.reps || 0}`}
{set.type === ExerciseType.BODYWEIGHT && `${set.weight ? `+${set.weight}kg` : 'BW'} x ${set.reps || 0}`}
{set.type === ExerciseType.CARDIO && `${set.durationSeconds || 0}s ${set.distanceMeters ? `/ ${set.distanceMeters}m` : ''}`}
{set.type === ExerciseType.STATIC && `${set.durationSeconds || 0}s`}
{set.type === ExerciseType.HIGH_JUMP && `${set.height || 0}cm`}
{set.type === ExerciseType.LONG_JUMP && `${set.distanceMeters || 0}m`}
{set.type === ExerciseType.PLYOMETRIC && `x ${set.reps || 0}`}
{formatSetMetrics(set, lang)}
</div>
<div className="text-xs text-on-surface-variant mt-1">
{new Date(set.timestamp).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}

View File

@@ -20,7 +20,7 @@ interface PlansProps {
const Plans: React.FC<PlansProps> = ({ lang }) => {
const { currentUser } = useAuth();
const userId = currentUser?.id || '';
const { plans, savePlan, deletePlan } = useSession();
const { plans, savePlan, deletePlan, refreshData } = useSession();
const { startSession } = useActiveWorkout();
const [isEditing, setIsEditing] = useState(false);
@@ -45,6 +45,7 @@ const Plans: React.FC<PlansProps> = ({ lang }) => {
useEffect(() => {
const loadData = async () => {
refreshData();
const fetchedExercises = await getExercises(userId);
// Filter out archived exercises
if (Array.isArray(fetchedExercises)) {
@@ -54,7 +55,7 @@ const Plans: React.FC<PlansProps> = ({ lang }) => {
}
};
if (userId) loadData();
}, [userId]);
}, [userId, refreshData]);
const handleCreateNew = () => {
setEditId(generateId());

View File

@@ -6,6 +6,7 @@ import FilledInput from '../FilledInput';
import ExerciseModal from '../ExerciseModal';
import { useTracker } from './useTracker';
import SetLogger from './SetLogger';
import { formatSetMetrics } from '../../utils/setFormatting';
interface ActiveSessionViewProps {
tracker: ReturnType<typeof useTracker>;
@@ -95,6 +96,7 @@ const ActiveSessionView: React.FC<ActiveSessionViewProps> = ({ tracker, activeSe
<button
onClick={() => setShowMenu(!showMenu)}
className="p-2 rounded-full bg-surface-container-high text-on-surface hover:bg-surface-container-highest transition-colors"
aria-label="Options"
>
<MoreVertical size={20} />
</button>
@@ -247,27 +249,7 @@ const ActiveSessionView: React.FC<ActiveSessionViewProps> = ({ tracker, activeSe
<div>
<div className="text-base font-medium text-on-surface">{set.exerciseName}{set.side && <span className="ml-2 text-xs font-medium text-on-surface-variant">{t(set.side.toLowerCase() as any, lang)}</span>}</div>
<div className="text-sm text-on-surface-variant">
{set.type === ExerciseType.STRENGTH &&
`${set.weight ? `${set.weight}kg` : ''} ${set.reps ? `x ${set.reps}` : ''}`.trim()
}
{set.type === ExerciseType.BODYWEIGHT &&
`${set.weight ? `${set.weight}kg` : ''} ${set.reps ? `x ${set.reps}` : ''}`.trim()
}
{set.type === ExerciseType.CARDIO &&
`${set.durationSeconds ? `${set.durationSeconds}s` : ''} ${set.distanceMeters ? `/ ${set.distanceMeters}m` : ''}`.trim()
}
{set.type === ExerciseType.STATIC &&
`${set.durationSeconds ? `${set.durationSeconds}s` : ''}`.trim()
}
{set.type === ExerciseType.HIGH_JUMP &&
`${set.height ? `${set.height}cm` : ''}`.trim()
}
{set.type === ExerciseType.LONG_JUMP &&
`${set.distanceMeters ? `${set.distanceMeters}m` : ''}`.trim()
}
{set.type === ExerciseType.PLYOMETRIC &&
`${set.reps ? `x ${set.reps}` : ''}`.trim()
}
{formatSetMetrics(set, lang)}
</div>
</div>
)}
@@ -278,12 +260,14 @@ const ActiveSessionView: React.FC<ActiveSessionViewProps> = ({ tracker, activeSe
<button
onClick={handleCancelEdit}
className="p-2 text-on-surface-variant hover:text-on-surface hover:bg-surface-container-high rounded-full transition-colors"
aria-label={t('cancel', lang)}
>
<X size={20} />
</button>
<button
onClick={() => handleSaveEdit(set)}
className="p-2 text-primary hover:bg-primary-container/20 rounded-full transition-colors"
aria-label={t('save', lang)}
>
<CheckCircle size={20} />
</button>
@@ -293,12 +277,14 @@ const ActiveSessionView: React.FC<ActiveSessionViewProps> = ({ tracker, activeSe
<button
onClick={() => handleEditSet(set)}
className="p-2 text-on-surface-variant hover:text-primary hover:bg-primary-container/20 rounded-full transition-colors"
aria-label={t('edit', lang)}
>
<Edit size={20} />
</button>
<button
onClick={() => onRemoveSet(set.id)}
className="p-2 text-on-surface-variant hover:text-error hover:bg-error-container/10 rounded-full transition-colors"
aria-label={t('delete', lang)}
>
<Trash2 size={20} />
</button>

View File

@@ -64,6 +64,7 @@ const SetLogger: React.FC<SetLoggerProps> = ({ tracker, lang, onLogSet, isSporad
<button
onClick={() => setIsCreating(true)}
className="p-2 text-primary hover:bg-primary-container/20 rounded-full"
aria-label="Add Exercise"
>
<Plus size={24} />
</button>

View File

@@ -5,6 +5,7 @@ import { t } from '../../services/i18n';
import ExerciseModal from '../ExerciseModal';
import { useTracker } from './useTracker';
import SetLogger from './SetLogger';
import { formatSetMetrics } from '../../utils/setFormatting';
interface SporadicViewProps {
tracker: ReturnType<typeof useTracker>;
@@ -40,13 +41,7 @@ const SporadicView: React.FC<SporadicViewProps> = ({ tracker, lang }) => {
}, [quickLogSession]);
const renderSetMetrics = (set: WorkoutSet) => {
const metrics: string[] = [];
if (set.weight) metrics.push(`${set.weight} ${t('weight_kg', lang)}`);
if (set.reps) metrics.push(`${set.reps} ${t('reps', lang)}`);
if (set.durationSeconds) metrics.push(`${set.durationSeconds} ${t('time_sec', lang)}`);
if (set.distanceMeters) metrics.push(`${set.distanceMeters} ${t('dist_m', lang)}`);
if (set.height) metrics.push(`${set.height} ${t('height_cm', lang)}`);
return metrics.join(' / ');
return formatSetMetrics(set, lang);
};
return (

View File

@@ -80,7 +80,7 @@ export const useTracker = (props: any) => { // Props ignored/removed
loadQuickLogSession();
};
loadData();
}, [activeSession, userId, userWeight, activePlan]);
}, [activeSession?.id, userId, userWeight, activePlan?.id, isSporadicMode]);
// Function to reload Quick Log session
const loadQuickLogSession = async () => {
@@ -99,7 +99,7 @@ export const useTracker = (props: any) => { // Props ignored/removed
const step = planExec.getCurrentStep();
if (step) {
const exDef = exercises.find(e => e.id === step.exerciseId);
if (exDef) {
if (exDef && selectedExercise?.id !== exDef.id) {
setSelectedExercise(exDef);
}
}
@@ -113,6 +113,7 @@ export const useTracker = (props: any) => { // Props ignored/removed
await form.updateFormFromLastSet(selectedExercise.id, selectedExercise.type, selectedExercise.bodyWeightPercentage);
} else {
setSearchQuery('');
form.resetForm();
}
};
updateSelection();
@@ -155,7 +156,7 @@ export const useTracker = (props: any) => { // Props ignored/removed
setSporadicSuccess(true);
setTimeout(() => setSporadicSuccess(false), 2000);
loadQuickLogSession();
form.resetForm();
// form.resetForm(); // Persist values
refreshHistory();
}
} catch (error) {

View File

@@ -64,7 +64,7 @@ const translations = {
weight_kg: 'Weight (kg)',
reps: 'Reps',
time_sec: 'Time (sec)',
dist_m: 'Dist (m)',
dist_m: 'Distance (m)',
height_cm: 'Height (cm)',
body_weight_percent: 'Body Weight',
log_set: 'Log Set',

View File

@@ -0,0 +1,53 @@
import { WorkoutSet, ExerciseType, Language } from '../types';
import { t } from '../services/i18n';
/**
* Formats a workout set's metrics into a standardized string.
* Format: "20 kg x 10 reps" or "300s / 1000m" depending on type.
* Ensures consistent delimiter usage and unit display.
*/
export const formatSetMetrics = (set: WorkoutSet, lang: Language): string => {
switch (set.type) {
case ExerciseType.STRENGTH:
return `${set.weight ? `${set.weight} kg` : ''} ${set.reps ? `x ${set.reps} ${t('reps', lang).toLowerCase()}` : ''}`.trim();
case ExerciseType.BODYWEIGHT:
case 'BODYWEIGHT': // Fallback for potential string type issues
// For bodyweight, format weight with sign if it exists, otherwise just BW
// If weight is undefined/null, standard active session logic used "BW" only in specific contexts,
// but let's standardize: if weight is 0 or undefined, maybe imply Bodyweight.
// Following ActiveSessionView logic: if weight is present, show it.
// Using signed logic: +10 kg, -10 kg
let weightStr = '';
if (set.weight !== undefined && set.weight !== null) {
weightStr = `${set.weight > 0 ? '+' : ''}${set.weight} kg`;
}
// If no weight is added, usually we just show reps.
// But History.tsx showed 'BW' if no weight. ActiveSessionView showed nothing.
// Let's stick to the richest information: if weight is set (even 0?), show it?
// Usually 0 added weight means just bodyweight.
// Let's mimic ActiveSessionView's concise approach but keep checks robust.
return `${weightStr} ${set.reps ? `x ${set.reps} ${t('reps', lang).toLowerCase()}` : ''}`.trim();
case ExerciseType.CARDIO:
return `${set.durationSeconds ? `${set.durationSeconds}s` : ''} ${set.distanceMeters ? `/ ${set.distanceMeters}m` : ''}`.trim();
case ExerciseType.STATIC:
return `${set.durationSeconds ? `${set.durationSeconds}s` : ''}`.trim();
case ExerciseType.HIGH_JUMP:
return `${set.height ? `${set.height}cm` : ''}`.trim();
case ExerciseType.LONG_JUMP:
return `${set.distanceMeters ? `${set.distanceMeters}m` : ''}`.trim();
case ExerciseType.PLYOMETRIC:
return `${set.reps ? `x ${set.reps} ${t('reps', lang).toLowerCase()}` : ''}`.trim();
default:
return '';
}
};