Skip to content

Commit 4f02c52

Browse files
Fix: Limit max number of reps and sets
- Refactor validation to prevent submission of empty workouts - Now the UI show a modal with a message when the user tries to finish a empty workout (without reps on the first set on JSON) - HTML validation of numbers of reps (max. 99) and weight (max. 999)
1 parent e142bb1 commit 4f02c52

File tree

1 file changed

+26
-5
lines changed
  • src/app/workout/[workoutid]/edit

1 file changed

+26
-5
lines changed

src/app/workout/[workoutid]/edit/page.tsx

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ export default function TrackWorkout({ params, template } : { params?: { workout
161161

162162
const finishWorkout = () => {
163163
if(disableSubmit) return;
164-
if (workout && workout.exercises.length > 0) {
164+
if (workout &&
165+
(workout.exercises.length > 0
166+
&& workout?.exercises[0].sets[0].reps
167+
&& workout?.exercises[0].sets[0].reps > 0)
168+
) {
165169
disableSubmit = true;
166170
if(params?.workoutid && !template) {
167171
apiWithAuth(getToken()).put('workouts/' + params.workoutid, workout).then(response => {
@@ -180,7 +184,7 @@ export default function TrackWorkout({ params, template } : { params?: { workout
180184

181185
return (
182186
<div className="h-full flex flex-col relative">
183-
<Header navigationTitle="Voltar" actionTitle="Finalizar" action={(e) => { if(workout!.exercises.length > 0 && workout?.exercises !== undefined) { setModals({type: 'finishWorkout'}) } } } />
187+
<Header navigationTitle="Voltar" actionTitle="Finalizar" action={(e) => { setModals({type: 'finishWorkout'}) } } />
184188
<Main>
185189
<MainContent>
186190
{
@@ -215,9 +219,9 @@ export default function TrackWorkout({ params, template } : { params?: { workout
215219
}
216220
</select>
217221
</td>
218-
<td className="w-4/12 text-right pb-2 text-white/50"><input type="number" min={0} name="weight" inputMode="decimal" className="text-sm text-right bg-white/10 text-white mr-2 py-1 px-2 w-12 inline align-middle" defaultValue={weight} onChange={(e) => updateSet(indexExercise, indexSet, 'weight', Number(e.target.value))} />kg</td>
222+
<td className="w-4/12 text-right pb-2 text-white/50"><input type="number" min={0} max={999} name="weight" inputMode="decimal" className="text-sm text-right bg-white/10 text-white mr-2 py-1 px-2 w-12 inline align-middle" defaultValue={weight} onChange={(e) => updateSet(indexExercise, indexSet, 'weight', Number(e.target.value))} />kg</td>
219223
<td className="w-1/12 text-center pb-2 text-white/50">X</td>
220-
<td className="w-4/12 text-center pb-2 text-white/50"><input type="number" min={0} name="reps" inputMode="numeric" className="text-sm bg-white/10 text-white mr-2 py-1 px-2 w-8 align-middle" defaultValue={reps} onChange={(e) => updateSet(indexExercise, indexSet, 'reps', Number(e.target.value))} />reps</td>
224+
<td className="w-4/12 text-center pb-2 text-white/50"><input type="number" min={0} max={99} name="reps" inputMode="numeric" className="text-sm bg-white/10 text-white mr-2 py-1 px-2 w-8 align-middle" defaultValue={reps} onChange={(e) => updateSet(indexExercise, indexSet, 'reps', Number(e.target.value))} />reps</td>
221225
<td className="w-1/12 text-center pb-2 pl-2"><RemoveIcon className="h-4 w-auto fill-destructive/80" onClick={() => deleteSet(indexExercise, indexSet)} /></td>
222226
</tr>
223227
)
@@ -240,7 +244,7 @@ export default function TrackWorkout({ params, template } : { params?: { workout
240244
<div className="modal-background absolute m-auto right-0 bottom-0 left-0 w-full h-full bg-black/80">
241245
{
242246
(modals.type == "addExercise") &&
243-
<div className="modal absolute flex flex-col m-auto right-0 bottom-0 left-0 w-full max-w-screen-md mx-auto h-3/6 bg-secondary rounded-t-3xl">
247+
<div className="modal absolute flex flex-col m-auto right-0 bottom-0 left-0 w-full max-w-screen-md mx-auto h-5/6 bg-secondary rounded-t-3xl">
244248
<div className="flex">
245249
<h2 className="text-2xl font-bold m-6 mb-2 flex-1">Selecionar exercício</h2>
246250
<RemoveIcon className="m-6 mb-2 fill-white" onClick={() => closeModal()} />
@@ -350,6 +354,11 @@ export default function TrackWorkout({ params, template } : { params?: { workout
350354
{
351355
(modals.type == "finishWorkout") &&
352356
<div className="modal absolute flex flex-col m-auto right-0 bottom-0 left-0 w-full max-w-screen-md mx-auto h-3/6 bg-secondary rounded-t-3xl">
357+
{
358+
(workout && workout.exercises.length > 0
359+
&& workout?.exercises[0].sets[0].reps
360+
&& workout?.exercises[0].sets[0].reps > 0) ?
361+
<>
353362
<div className="flex">
354363
<h2 className="text-2xl font-bold m-6 mb-2 flex-1">Dados do treino</h2>
355364
<RemoveIcon className="m-6 mb-2 fill-white" onClick={() => closeModal()} />
@@ -365,6 +374,18 @@ export default function TrackWorkout({ params, template } : { params?: { workout
365374
<textarea name="workoutComment" className="text-md px-2 py-1 rounded-lg bg-black/50 w-full h-24 resize-none" placeholder="Comentário (opcional)" defaultValue={workout?.comment} onChange={(e) => addMetaData("comment", e.target.value)}></textarea>
366375
<Button link="#" title="Finalizar treino" action={() => finishWorkout()} primary />
367376
</div>
377+
</>
378+
:
379+
<>
380+
<div className="flex">
381+
<h2 className="text-2xl font-bold m-6 mb-2 flex-1">Dados do treino</h2>
382+
<RemoveIcon className="m-6 mb-2 fill-white" onClick={() => closeModal()} />
383+
</div>
384+
<div className="flex flex-col justify-center items-center h-full">
385+
<p className="text-center text-white/25 align-middle">Adicione pelo menos um set</p>
386+
</div>
387+
</>
388+
}
368389
</div>
369390
}
370391
</div>

0 commit comments

Comments
 (0)