Skip to content

Commit

Permalink
Fix some linting complaints.
Browse files Browse the repository at this point in the history
  • Loading branch information
eswan18 committed Aug 31, 2023
1 parent 4ace255 commit 6f94655
Showing 1 changed file with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,22 @@ export default function ExerciseSetWidget({ exerciseType, exercises, workoutId }
}
// This feels janky, but we need a unique key for each exercise that is constant across renders.
const newKey = Math.random();
function onSubmit(exercise: Exercise) {
setExercisesWithKeys([...exercisesWithKeys, { exercise, key: newKey }])
function onSubmit({weight, reps}: ExerciseInputModalState) {
if (!type || !type.id) {
// This should never happen.
throw new Error("can't create new exercise because no exercise type is selected")
}
const newExercise = {
workout_id: workoutId,
exercise_type_id: type.id,
weight,
reps,
start_time: new Date(),
}
setExercisesWithKeys([...exercisesWithKeys, { exercise: newExercise, key: newKey }])
setModal(null);
}
setModal(<ExerciseInputModal onSubmit={onSubmit} workoutId={workoutId} exerciseTypeId={type.id} handleClose={ () => setModal(null) } />)
setModal(<ExerciseInputModal onSubmit={onSubmit} exerciseTypeName={type.name} handleClose={ () => setModal(null) } />)
}

return (
Expand All @@ -68,32 +79,24 @@ export default function ExerciseSetWidget({ exerciseType, exercises, workoutId }
}

type ExerciseInputModalProps = {
onSubmit: (exercise: Exercise) => void;
workoutId: string;
exerciseTypeId: string;
onSubmit: (e: ExerciseInputModalState) => void;
exerciseTypeName: string;
handleClose?: () => void
}

function ExerciseInputModal({onSubmit, workoutId, exerciseTypeId, handleClose }: ExerciseInputModalProps) {
type ExerciseInputModalState = {
weight: number
reps: number
}

function ExerciseInputModal({onSubmit, exerciseTypeName, handleClose }: ExerciseInputModalProps) {
const [weight, setWeight] = useState<number | undefined>(undefined);
const [reps, setReps] = useState<number | undefined>(undefined);
const buttonEnabled = (weight != null) && (reps != null);

let exercise: Exercise = {
workout_id: workoutId,
exercise_type_id: exerciseTypeId,
weight: weight,
reps: reps,
start_time: new Date(),
}
useEffect(() => {
exercise.weight = weight;
exercise.reps = reps;
}, [weight, reps])

return (
<ClientModal handleClose={ handleClose }>
<Form title="Record exercise" onSubmit={() => {onSubmit(exercise)}}>
<Form title="Record exercise" onSubmit={() => {weight && reps && onSubmit({weight, reps})}}>
<Input label="Weight" htmlFor="weight" type="number" step="0.5" id="weight" name="Weight" placeholder="9000" onValueUpdate={setWeight} />
<Input label="Reps" htmlFor="reps" type="number" step="1" id="reps" name="Reps" placeholder="42" onValueUpdate={setReps} />
<div className="flex flex-row justify-evenly items-center mt-4" >
Expand Down

0 comments on commit 6f94655

Please sign in to comment.