From 6b6be775be01e59a1688466547abbff650370e2c Mon Sep 17 00:00:00 2001 From: Jaysc Date: Sun, 30 Jul 2023 20:36:35 +0100 Subject: [PATCH] created context and moved component --- src/Form.tsx | 97 +++++++++++---------------------- src/components/FormProvider.tsx | 38 +++++++++++++ src/components/TodoItem.tsx | 58 ++++++++++++++++++++ 3 files changed, 128 insertions(+), 65 deletions(-) create mode 100644 src/components/FormProvider.tsx create mode 100644 src/components/TodoItem.tsx diff --git a/src/Form.tsx b/src/Form.tsx index 8ebb904..c08c77e 100644 --- a/src/Form.tsx +++ b/src/Form.tsx @@ -9,6 +9,8 @@ import { useOptimisticReducer } from "./hooks/useOptimisticReducer"; import "./Form.css"; import type { AppState } from "./types"; +import TodoItem from "./components/TodoItem"; +import FormProvider from "./components/FormProvider"; type Props = { initialTodos: AppState; @@ -76,71 +78,36 @@ export const Form = ({ initialTodos = {} }: Props) => { ); return ( -
-
-
- - -
+ +
+
+
+ + +
+
+ +
+ {Object.keys(state).map((todoId, idx) => { + return + })} +
+ +
+ +
+ +
- -
- {Object.keys(state).map((todoId, idx) => { - const { content, isComplete, isRemoved = false } = state[todoId]; - - if (isRemoved) { - return null; - } - - return ( - - ); - })} -
- -
- -
- - -
+ ); }; diff --git a/src/components/FormProvider.tsx b/src/components/FormProvider.tsx new file mode 100644 index 0000000..a4bc9f4 --- /dev/null +++ b/src/components/FormProvider.tsx @@ -0,0 +1,38 @@ +import { ReactNode, createContext } from "react"; +import type { Action, AppState, OptimisticDispatch } from "../types"; + +// eslint-disable-next-line @typescript-eslint/no-empty-function +const noop = () => {}; + +export const FormContext = createContext>({ + dispatch: noop, + handleCheck: noop, + handleRemove: noop, + state: {} +}); + +type Props = { + children: ReactNode; + dispatch: OptimisticDispatch; + handleCheck: (id: string, isComplete: boolean) => void; + handleRemove: (id: string) => void; + state: AppState; +}; + +const FormProvider = ({ + children, + dispatch, + handleCheck, + handleRemove, + state, +}: Props) => { + const value = { + dispatch, + handleCheck, + handleRemove, + state, + }; + return {children}; +}; + +export default FormProvider; diff --git a/src/components/TodoItem.tsx b/src/components/TodoItem.tsx new file mode 100644 index 0000000..33aeec5 --- /dev/null +++ b/src/components/TodoItem.tsx @@ -0,0 +1,58 @@ +import { useContext } from "react"; +import { FormContext } from "./FormProvider"; + +export type Props = { + todoId: string; + idx: number +}; + +const TodoItem = ({ todoId, idx }: Props) => { + const { dispatch, handleCheck, handleRemove, state } = + useContext(FormContext); + const { content, isComplete, isRemoved = false } = state[todoId]; + + if (isRemoved) { + return null; + } + + return ( + + ); +}; + +export default TodoItem;