From 84ed698c444d3301aa9ddc1e83f8199eb9d4c433 Mon Sep 17 00:00:00 2001 From: Augustin <48162609+AugustinSorel@users.noreply.github.com> Date: Sat, 20 Jul 2024 18:15:36 +0100 Subject: [PATCH] REFACTOR/useFilterExerciseData hook added in exercise page ctx (#233) * REFACTOR/useFilterExerciseData hook added in exercise page ctx * FIX/remove object reference --- .../[id]/_components/exercisePageContext.tsx | 106 +++++++++--------- 1 file changed, 56 insertions(+), 50 deletions(-) diff --git a/src/app/exercises/[id]/_components/exercisePageContext.tsx b/src/app/exercises/[id]/_components/exercisePageContext.tsx index beb37188..cca3b1fe 100644 --- a/src/app/exercises/[id]/_components/exercisePageContext.tsx +++ b/src/app/exercises/[id]/_components/exercisePageContext.tsx @@ -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; + filteredData: RouterOutputs["exercise"]["get"]["data"]; }; filter: Dispatch< SetStateAction<{ @@ -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 ( { - 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, + ) => { + 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);