Skip to content

Commit

Permalink
REFACTOR/useFilterExerciseData hook added in exercise page ctx (#233)
Browse files Browse the repository at this point in the history
* REFACTOR/useFilterExerciseData hook added in exercise page ctx

* FIX/remove object reference
  • Loading branch information
AugustinSorel authored Jul 20, 2024
1 parent 9f664ff commit 84ed698
Showing 1 changed file with 56 additions and 50 deletions.
106 changes: 56 additions & 50 deletions src/app/exercises/[id]/_components/exercisePageContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import {
type SetStateAction,
} from "react";
import { useExercisePageSearchParams } from "./useExercisePageSearchParams";
import type { ExerciseData, ExerciseWithData } from "@/server/db/types";
import { convertWeight } from "@/lib/math";
import { useWeightUnit } from "@/context/weightUnit";
import type { ExercisePageSearchParams } from "./exercisePageSearchParams";

const ExercisePageContext = createContext<
| {
exercise: RouterOutputs["exercise"]["get"] & {
filteredData: Array<ExerciseData>;
filteredData: RouterOutputs["exercise"]["get"]["data"];
};
filter: Dispatch<
SetStateAction<{
Expand All @@ -30,68 +30,74 @@ const ExercisePageContext = createContext<
>(undefined);

export const ExercisePageContextProvider = (
props: PropsWithChildren & { exercise: ExerciseWithData },
props: PropsWithChildren & { exercise: RouterOutputs["exercise"]["get"] },
) => {
const searchParams = useExercisePageSearchParams();
const weightUnit = useWeightUnit();
const [datesFilter, setDatesFilter] = useState({
from: searchParams.values.from,
to: searchParams.values.to,
});

//TODO:clean this crap
const convertExerciseWeight = useConvertExerciseWeight();
const injectFilteredData = useInjectFilteredData();

const exercise = injectFilteredData(
convertExerciseWeight(props.exercise),
datesFilter,
);

return (
<ExercisePageContext.Provider
value={{
filter: setDatesFilter,
exercise: {
...props.exercise,
data: props.exercise.data.map((d) => {
return {
...d,
weightLifted: convertWeight(d.weightLifted, weightUnit.get),
};
}),
filteredData: props.exercise.data
.filter((d) => {
if (datesFilter.from && !datesFilter.to) {
return (
new Date(datesFilter.from).getTime() <=
new Date(d.doneAt).getTime()
);
}

if (!datesFilter.from && datesFilter.to) {
return (
new Date(datesFilter.to).getTime() >=
new Date(d.doneAt).getTime()
);
}

if (datesFilter.from && datesFilter.to) {
return (
new Date(datesFilter.from).getTime() <=
new Date(d.doneAt).getTime() &&
new Date(datesFilter.to).getTime() >=
new Date(d.doneAt).getTime()
);
}

return true;
})
.map((d) => {
return {
...d,
weightLifted: convertWeight(d.weightLifted, weightUnit.get),
};
}),
},
}}
value={{ filter: setDatesFilter, exercise }}
{...props}
/>
);
};

const useInjectFilteredData = () => {
return (
exercise: RouterOutputs["exercise"]["get"],
dates: Pick<ExercisePageSearchParams, "to" | "from">,
) => {
return {
...exercise,
filteredData: exercise.data.filter((exerciseData) => {
const fromTime = dates.from ? new Date(dates.from).getTime() : null;
const toTime = dates.to ? new Date(dates.to).getTime() : null;
const doneAtTime = new Date(exerciseData.doneAt).getTime();

if (fromTime && !toTime) {
return fromTime <= doneAtTime;
}

if (!fromTime && toTime) {
return toTime >= doneAtTime;
}

if (fromTime && toTime) {
return fromTime <= doneAtTime && toTime >= doneAtTime;
}

return true;
}),
};
};
};

const useConvertExerciseWeight = () => {
const weightUnit = useWeightUnit();

return (exercise: RouterOutputs["exercise"]["get"]) => {
return {
...exercise,
data: exercise.data.map((exerciseData) => ({
...exerciseData,
weightLifted: convertWeight(exerciseData.weightLifted, weightUnit.get),
})),
};
};
};

export const useExercisePageContext = () => {
const ctx = useContext(ExercisePageContext);

Expand Down

0 comments on commit 84ed698

Please sign in to comment.