Skip to content

Commit

Permalink
draft 2
Browse files Browse the repository at this point in the history
  • Loading branch information
VvynnykV committed Oct 28, 2024
1 parent a24fe9e commit a93a9c9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import cn from 'classnames';
import { useDispatch, useGlobalState } from '../../context/Store';

export const Header: FC = () => {
const titleField = useRef<HTMLInputElement>(null);
const [title, setTitle] = useState('');
const [isSubmitting, setIsSubmiting] = useState(false);

Expand Down Expand Up @@ -43,6 +42,8 @@ export const Header: FC = () => {
}
};

const titleField = useRef<HTMLInputElement>(null);

useEffect(() => {
titleField.current?.focus();
}, [todos, isSubmitting]);
Expand Down
8 changes: 4 additions & 4 deletions src/components/TodoItem/TodoItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export const TodoItem: React.FC<Props> = ({ todo }) => {
const [isEditing, setIsEditing] = useState(false);
const [editedTitle, setEditedTitle] = useState(title);

const editField = useRef<HTMLInputElement>(null);

const handleDelete = () => dispatch({ type: 'delete', payload: id });

const handleSubmit = (
Expand Down Expand Up @@ -71,9 +69,11 @@ export const TodoItem: React.FC<Props> = ({ todo }) => {
});
};

const editField = useRef<HTMLInputElement>(null);

useEffect(() => {
if (editField.current && isEditing) {
editField.current.focus();
if (isEditing) {
editField.current?.focus();
}
}, [isEditing]);

Expand Down
23 changes: 16 additions & 7 deletions src/context/Store.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { createContext, FC, ReactNode, useContext, useReducer } from 'react';
import {
createContext,
Dispatch,
FC,
ReactNode,
useContext,
useReducer,
} from 'react';
import { Todo } from '../types/Todo';
import { getLocalStorage } from '../utils/getLocalStorageData';

Expand All @@ -7,8 +14,6 @@ type Action =
| { type: 'update'; payload: Todo }
| { type: 'delete'; payload: number };

type Dispatch = { (action: Action): void };

function reducer(todos: Todo[], action: Action) {
let newTodos: Todo[] = [];

Expand Down Expand Up @@ -45,8 +50,10 @@ function reducer(todos: Todo[], action: Action) {

const initialTodos: Todo[] = getLocalStorage<Todo[]>('todos', []);

const defaultDispatch: Dispatch<Action> = () => {};

export const TodosContext = createContext(initialTodos);
export const DispatchContext = createContext<Dispatch>(() => {});
export const DispatchContext = createContext(defaultDispatch);

type Props = {
children: ReactNode;
Expand All @@ -55,9 +62,11 @@ type Props = {
export const GlobalStateProvider: FC<Props> = ({ children }) => {
const [todos, dispatch] = useReducer(reducer, initialTodos);

<DispatchContext.Provider value={dispatch}>
<TodosContext.Provider value={todos}>{children}</TodosContext.Provider>
</DispatchContext.Provider>;
return (
<DispatchContext.Provider value={dispatch}>
<TodosContext.Provider value={todos}>{children}</TodosContext.Provider>
</DispatchContext.Provider>
);
};

export const useGlobalState = () => useContext(TodosContext);
Expand Down

0 comments on commit a93a9c9

Please sign in to comment.