Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"iterations": [
{
"start": "2023-12-05T08:30:00.000Z",
"running": false,
"end": "2023-12-05T09:30:00.000Z",
"units": [
{
Expand Down
47 changes: 41 additions & 6 deletions src/routes/Routine.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useState } from 'react';
import { useLoaderData } from 'react-router-dom';
import type { Params } from "react-router-dom";
import Button from '../components/Button';
Expand All @@ -6,31 +7,65 @@ import { formatDate } from '../lib/dateConversion';

interface Iteration {
start: string;
}
running?: boolean;
end?: string;
};

interface RoutineData {
name: string;
iterations?: Iteration[];
}
};

export async function loader({ params }: {params: Params}) {
return jsonData.routines.find(routine => routine.name === params.routine);
}
};

const Routine = () => {
const routine = useLoaderData() as RoutineData;
const [iterations, setIterations] = useState<Iteration[]>(routine.iterations || []);

const handleOnStart = () => {
let runningRoutine = iterations.find(iteration => iteration.running) || false;

if (!runningRoutine) {
let currentDate = new Date();
let newRoutine = {"start": currentDate.toISOString(), "running": true};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here as down below - what about storing start as Date instead of String?

setIterations([...iterations, newRoutine]);
}
};

const handleOnEnd = () => {
let runningRoutine = iterations.find(iteration => iteration.running) || false;

if (runningRoutine) {
runningRoutine.running = false;
let currentDate = new Date();
runningRoutine.end = currentDate.toISOString();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you saving the end date as a string? What speaks against saving it as date time?

setIterations([...iterations]);
}
};

const isRunning = () => {
if (iterations.find(iteration => iteration.running)) {
return (
<div>🔄 iteration in progress</div>
);
}
}

return (
<div>
<h1>{ routine.name }</h1>
<Button onClick={()=> {}}>Start</Button>
<Button onClick={()=> {}}>End</Button>
<Button onClick={handleOnStart}>Start</Button>
<Button onClick={handleOnEnd}>End</Button>

<h3>Past iterations:</h3>


{routine.iterations && routine.iterations.map( (iteration, index) => {
{iterations && iterations.filter(iteration => iteration.end).map( (iteration, index) => {
return (<div key={index}>{ formatDate(iteration.start) }</div>)
})}
{iterations && isRunning()}
</div>
);
};
Expand Down