Skip to content

Commit

Permalink
input ref error, setTitleEdition deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
HaniaNassalska committed Sep 22, 2023
1 parent c5f6943 commit aaa3670
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
20 changes: 10 additions & 10 deletions src/components/Task/Task.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ type Props = {
export const Task = ({ todo }: Props) => {
const {
temptTodo,
removeTask, editedTodo,
editedTodo,
todos,
newTitle,
removeTask,
toggleCompletedTodos,
titleEdition,
onTitleEdition,
todos,
closeTitleEdition,
setNewTitle,
todoTitleEdition,
newTitle,
setTitleEdition,
} = useTodo();
const inputRef = useRef<HTMLInputElement>(null);

useEffect(() => {
if (titleEdition && todo.isOnTitleEdition) {
inputRef.current!.focus();
if (todo.isOnTitleEdition) {
inputRef.current?.focus();
}
}, [titleEdition, todo.isOnTitleEdition]);
}, [todo.isOnTitleEdition]);

const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter') {
Expand All @@ -34,7 +34,7 @@ export const Task = ({ todo }: Props) => {
}

if (e.key === 'Escape') {
setTitleEdition(false);
closeTitleEdition(todos, todo.id);
}
};

Expand All @@ -53,7 +53,7 @@ export const Task = ({ todo }: Props) => {
/>
</label>

{titleEdition && todo.isOnTitleEdition
{todo.isOnTitleEdition
? (
<form>
<input
Expand Down
28 changes: 17 additions & 11 deletions src/provider/todoProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export const ToDoProvider = ({ children }: Props) => {
const [temptTodo, setTempTodo] = useState<Todo | null>(null);
const [temptTodos, setTempTodos] = useState<Todo[]>([]);
const [editedTodo, setEditedTodo] = useState<boolean>(false);
const [titleEdition, setTitleEdition] = useState<boolean>(false);
const [newTitle, setNewTitle] = useState<string>('null');

const handleShowError = (err: Errors) => {
Expand Down Expand Up @@ -154,7 +153,6 @@ export const ToDoProvider = ({ children }: Props) => {
};

const onTitleEdition = (tasks: Todo[], taskId: number) => {
setTitleEdition(true);
setTodos(
tasks.map((t) => {
if (t.id === taskId) {
Expand All @@ -171,15 +169,27 @@ export const ToDoProvider = ({ children }: Props) => {
);
};

const closeTitleEdition = (tasks: Todo[], taskId: number) => {
setTodos(
tasks.map((t) => {
if (t.id === taskId) {
return {
...t,
isOnTitleEdition: false,
};
}

return t;
}),
);
};

const todoTitleEdition = (
task: Todo,
updatedTitle: string,
tasks: Todo[],
) => {
if (updatedTitle === task.title) {
setTitleEdition(false);
} else if (updatedTitle === '') {
setTitleEdition(false);
if (updatedTitle === '') {
removeTask(task);
} else {
setTodos(
Expand All @@ -201,9 +211,6 @@ export const ToDoProvider = ({ children }: Props) => {
})
.catch(() => {
handleShowError(Errors.Update);
})
.finally(() => {
setTitleEdition(false);
});
}
};
Expand All @@ -219,7 +226,6 @@ export const ToDoProvider = ({ children }: Props) => {
temptTodos,
newTitle,
allTodosAreActive,
titleEdition,
setNewTodoName,
handleShowError,
handleSetFilterTodos,
Expand All @@ -231,8 +237,8 @@ export const ToDoProvider = ({ children }: Props) => {
toggleCompletedTodos,
todoTitleEdition,
onTitleEdition,
closeTitleEdition,
setNewTitle,
setTitleEdition,
}}
>
{children}
Expand Down
3 changes: 1 addition & 2 deletions src/provider/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export interface TodoContextType {
temptTodos: Todo[];
allTodosAreActive: boolean;
newTitle: string;
titleEdition: boolean;
setNewTodoName: React.Dispatch<React.SetStateAction<string>>;
handleShowError: (err: Errors) => void;
handleSetFilterTodos: (filterType: FilterType) => void;
Expand All @@ -24,8 +23,8 @@ export interface TodoContextType {
toggleCompletedTodos: (task: Todo) => void;
todoTitleEdition: (task: Todo, newTitle: string, tasks: Todo[]) => void;
onTitleEdition: (tasks: Todo[], taskId: number) => void;
closeTitleEdition: (tasks: Todo[], taskId: number) => void;
setNewTitle: React.Dispatch<React.SetStateAction<string>>;
setTitleEdition: React.Dispatch<React.SetStateAction<boolean>>;
}

export enum Errors {
Expand Down

0 comments on commit aaa3670

Please sign in to comment.