From 1b90b78cfbac86a8bb2a53f5753af92ee0d2a3ee Mon Sep 17 00:00:00 2001 From: Kimi Kuru Date: Thu, 8 Aug 2024 14:28:08 +0300 Subject: [PATCH 01/16] Create context for selected entry state --- .../entry-context/EntryContext.ts | 9 +++++++++ .../entry-context/EntryContextProvider.tsx | 18 ++++++++++++++++++ .../entry-context/useEntryContext.ts | 10 ++++++++++ 3 files changed, 37 insertions(+) create mode 100644 web/src/components/workday-browser/entry-context/EntryContext.ts create mode 100644 web/src/components/workday-browser/entry-context/EntryContextProvider.tsx create mode 100644 web/src/components/workday-browser/entry-context/useEntryContext.ts diff --git a/web/src/components/workday-browser/entry-context/EntryContext.ts b/web/src/components/workday-browser/entry-context/EntryContext.ts new file mode 100644 index 00000000..f695a276 --- /dev/null +++ b/web/src/components/workday-browser/entry-context/EntryContext.ts @@ -0,0 +1,9 @@ +import { createContext } from "react"; +import { Entry } from "../../../graphql/generated/graphql"; + +type EntryContextValue = { + selectedEntry: Entry | null; + setSelectedEntry: (entry: Entry | null) => void; +}; + +export const EntryContext = createContext(undefined); diff --git a/web/src/components/workday-browser/entry-context/EntryContextProvider.tsx b/web/src/components/workday-browser/entry-context/EntryContextProvider.tsx new file mode 100644 index 00000000..252803d7 --- /dev/null +++ b/web/src/components/workday-browser/entry-context/EntryContextProvider.tsx @@ -0,0 +1,18 @@ +import { ReactNode, useState } from "react"; +import { EntryContext } from "./EntryContext"; +import { Entry } from "../../../graphql/generated/graphql"; + +type EntryContextProviderProps = { + children: ReactNode; +}; + +export const EntryContextProvider = ({ children }: EntryContextProviderProps) => { + const [selectedEntry, setSelected] = useState(null); + const setSelectedEntry = (entry: Entry | null) => setSelected(entry); + + return ( + + {children} + + ); +}; diff --git a/web/src/components/workday-browser/entry-context/useEntryContext.ts b/web/src/components/workday-browser/entry-context/useEntryContext.ts new file mode 100644 index 00000000..fb72c419 --- /dev/null +++ b/web/src/components/workday-browser/entry-context/useEntryContext.ts @@ -0,0 +1,10 @@ +import { useContext } from "react"; +import { EntryContext } from "./EntryContext"; + +export const useEntryContext = () => { + const entryContext = useContext(EntryContext); + if (entryContext === undefined) { + throw new Error("context must be inside a entryProvider"); + } + return entryContext; +}; From 94f33d097f3519aa2743f56e8c29eef9a7c85ab2 Mon Sep 17 00:00:00 2001 From: Kimi Kuru Date: Thu, 8 Aug 2024 14:37:55 +0300 Subject: [PATCH 02/16] Use entry context provider in workday list --- .../workday-browser/WorkdayList.tsx | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/web/src/components/workday-browser/WorkdayList.tsx b/web/src/components/workday-browser/WorkdayList.tsx index 81550a56..ba8e7774 100644 --- a/web/src/components/workday-browser/WorkdayList.tsx +++ b/web/src/components/workday-browser/WorkdayList.tsx @@ -11,6 +11,7 @@ import { useState } from "react"; import { isWeekend } from "../../common/workdayUtils"; import { Dayjs } from "dayjs"; import { t } from "i18next"; +import { EntryContextProvider } from "./entry-context/EntryContextProvider"; const WorkdayList = () => { const dayjs = useDayjs(); @@ -61,18 +62,20 @@ const WorkdayList = () => { - {dividedWorkdays.map((wdArr) => { - if (isWeekend(wdArr[0].date)) { - return ( - - {wdArr.map((wd) => ( - - ))} - - ); - } - return wdArr.map((wd) => ); - })} + + {dividedWorkdays.map((wdArr) => { + if (isWeekend(wdArr[0].date)) { + return ( + + {wdArr.map((wd) => ( + + ))} + + ); + } + return wdArr.map((wd) => ); + })} +