Skip to content

Commit

Permalink
Extract constants
Browse files Browse the repository at this point in the history
  • Loading branch information
AmadeuszAndroid committed Oct 16, 2023
1 parent 9707db6 commit 9ec83f9
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/components/TodoItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ type TodoInfoProps = {
todo: Todo
};

const NO_EDITED_ID = -1;
const DOUBLE_CLICK_CODE = 2;

export const TodoItem = ({ todo: { id, title, completed } }: TodoInfoProps) => {
const [editedId, setEditedId] = useState(-1);
const [editedId, setEditedId] = useState(NO_EDITED_ID);
const [editedTitle, setEdtedTitle] = useState(title);
const { todos, setNewTodos } = useContext(TodosContext);
const [, setStorageTodos] = useLocalStorage();
Expand Down Expand Up @@ -46,7 +49,7 @@ export const TodoItem = ({ todo: { id, title, completed } }: TodoInfoProps) => {
};

const handleDoubleClick = (event: React.MouseEvent) => {
if (event.detail === 2) {
if (event.detail === DOUBLE_CLICK_CODE) {
setEditedId(id);
}
};
Expand All @@ -72,15 +75,15 @@ export const TodoItem = ({ todo: { id, title, completed } }: TodoInfoProps) => {
handleClick();
}

setEditedId(-1);
setEditedId(NO_EDITED_ID);
};

const handleKeyClick = (event: KeyboardEvent) => {
if (editedId !== -1) {
if (editedId !== NO_EDITED_ID) {
const clickedKey = event.key;

if (clickedKey === 'Escape') {
setEditedId(-1);
setEditedId(NO_EDITED_ID);
setEdtedTitle(title);
} else if (clickedKey === 'Enter') {
handleBlur();
Expand Down

0 comments on commit 9ec83f9

Please sign in to comment.