Skip to content

Commit

Permalink
feat: auto start pause after time exercise
Browse files Browse the repository at this point in the history
## Explanation

Automatically end the current set and start a pause timer when closing
the count up in a time-based exercise.
  • Loading branch information
clentfort committed Feb 2, 2024
1 parent 3ba5d20 commit 29def4d
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/app/(app)/current-plan/exercise/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import React, { useCallback, useEffect, useRef, useState } from "react";
import {
PlanItemSet,
PlanItemSetHistory,
PlanItemTimeSet,
PlanItemWeightSet,
} from "@/api/types";
import CountUp from "@/components/count-up";
Expand Down Expand Up @@ -213,11 +214,40 @@ export default function ExercisePlanScreen() {
updatePlanItem({ objectId: planItemId, finishedSets, history });
};

const handleEndCountUp = () => {
const handleEndCountUp = async () => {
const now = Date.now();
const duration = Math.round((now - timerStartedAt!) / 1000);
setTimerStartedAt(-1);
handleEditCurrentSet({ type: "TIME", time: duration });

const set: PlanItemTimeSet = { type: "TIME", time: duration };

const sets = [...planItem.sets];
sets[planItem.currentSetIndex ?? 0] = set;

const currentSetIndex = planItem.currentSetIndex ?? 0 + 1;
const [, ...openSets] = planItem.openSets;
const finishedSets = [...planItem.finishedSets, set];
const history = upsertSetsInHistory(
planItem.history ?? [],
finishedSets,
planStartedAt,
);

await updatePlanItem({
objectId: planItemId,
currentSetIndex,
finishedSets,
history,
openSets,
sets,
});

if (planItem.openSets.length > 1) {
setPause(nextPauseDuration);
}
if (planItem.openSets.length <= 1) {
router.push(`/current-plan/${planItem.plan.objectId}`);
}
};

const lastSession = (planItem.history ?? []).findLast(
Expand Down

0 comments on commit 29def4d

Please sign in to comment.