From a5c7d3059a1c889ada4e0745763a7ca7fd93eeb1 Mon Sep 17 00:00:00 2001 From: Tobias Prima <88434271+tobiasprima@users.noreply.github.com> Date: Thu, 21 Mar 2024 17:27:28 +0800 Subject: [PATCH] feat: scroll to new todo (#11) --- src/features/todos/TodoList.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/features/todos/TodoList.js b/src/features/todos/TodoList.js index 4f7234c..a539ef8 100644 --- a/src/features/todos/TodoList.js +++ b/src/features/todos/TodoList.js @@ -1,7 +1,7 @@ import React from 'react' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import { faPlus, faXmark } from '@fortawesome/free-solid-svg-icons' -import { useState, useEffect } from 'react' +import { useState, useEffect, useRef } from 'react' import { useGetTodosQuery, useAddTodoMutation, @@ -12,10 +12,12 @@ import { } from '../../app/api/apiSlice' const TodoList = () => { - const [ isResetDone, setisResetDone ] = useState(false); + const [ isResetDone, setisResetDone ] = useState(false) const [ todoWithoutDB, setTodoWithoutDB ] = useState([]) const [ newTodo, setNewTodo ] = useState('') - const [ moveDoneToEnd, setMoveDoneToEnd ] = useState(false); + const [ moveDoneToEnd, setMoveDoneToEnd ] = useState(false) + const [ newTodoId, setNewTodoId ] = useState(null) + const scrollToRef = useRef(null) const { data: todos, @@ -63,15 +65,26 @@ const TodoList = () => { } }, [todos, isError]) + // Effect to scroll to the newly created todo + useEffect(()=> { + if (scrollToRef.current) { + scrollToRef.current.scrollIntoView({ behavior: 'smooth', block: 'end', inline: 'nearest'}) + } + }) + const handleSubmit = (e)=> { e.preventDefault() if (newTodo.trim() === '') return // Prevent adding empty todos if (!isError){ - addTodo({title: newTodo}) + addTodo({title: newTodo}).then((response) => { + // Scroll to New todo + setNewTodoId(response.data._id) + }) } else { // Handle submit without database const newTodoWithoutDB = {_id: Date.now(), title: newTodo, status: false} setTodoWithoutDB(prevTodos => [...prevTodos, newTodoWithoutDB]) + setNewTodoId(newTodoWithoutDB._id) } setNewTodo('') @@ -108,6 +121,7 @@ const TodoList = () => { } + const newItemSection =
@@ -141,7 +155,7 @@ const TodoList = () => { } content = sortedTodos.map(todo => { return ( -
+