Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
Todo
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Sujeeth AV
Todo
Commits
4b660503
Commit
4b660503
authored
May 20, 2025
by
Sujeeth AV
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Index.jsx
parent
df7f13da
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
39 deletions
+40
-39
Index.jsx
todo/src/Form/form/Index.jsx
+40
-39
No files found.
todo/src/Form/form/Index.jsx
View file @
4b660503
import
React
,
{
createContext
,
useState
,
useRef
,
useEffect
,
useCallback
}
from
'react'
;
import
React
,
{
createContext
,
useState
,
useRef
,
useEffect
,
useCallback
}
from
'react'
;
import
{
getTodo
,
addTodo
,
delTodo
,
uptTodo
}
from
'../../API/Api'
import
{
getTodo
,
addTodo
,
delTodo
,
uptTodo
}
from
'../../API/Api'
;
import
{
Header
}
from
'../../Layout/card/Index'
;
import
{
Header
}
from
'../../Layout/card/Index'
;
import
styles
from
'./styles.module.css'
;
import
styles
from
'./styles.module.css'
;
import
List
from
'../../todo/List/Index'
;
import
List
from
'../../todo/List/Index'
;
export
const
MyTask
=
createContext
();
const
debounce
=
(
func
,
delay
)
=>
{
export
const
MyTask
=
createContext
();
let
timeoutId
;
return
function
(...
args
)
{
clearTimeout
(
timeoutId
);
timeoutId
=
setTimeout
(()
=>
{
func
.
apply
(
this
,
args
);
},
delay
);
};
};
export
const
Input
=
()
=>
{
export
const
Input
=
()
=>
{
const
[
task
,
setTask
]
=
useState
(
""
);
const
[
task
,
setTask
]
=
useState
(
""
);
...
@@ -21,9 +12,13 @@ export const Input = () => {
...
@@ -21,9 +12,13 @@ export const Input = () => {
const
[
editIndex
,
setEditIndex
]
=
useState
(
null
);
const
[
editIndex
,
setEditIndex
]
=
useState
(
null
);
const
[
editedTask
,
setEditedTask
]
=
useState
(
""
);
const
[
editedTask
,
setEditedTask
]
=
useState
(
""
);
const
[
loading
,
setLoading
]
=
useState
(
false
);
const
[
loading
,
setLoading
]
=
useState
(
false
);
const
[
isNewTodoAdded
,
setIsNewTodoAdded
]
=
useState
(
false
);
const
editableRef
=
useRef
(
null
);
const
editableRef
=
useRef
(
null
);
const
lastCursorPos
=
useRef
(
0
);
const
lastCursorPos
=
useRef
(
0
);
const
debounceTimeout
=
useRef
(
null
);
const
fetchTodos
=
useCallback
(
async
()
=>
{
const
fetchTodos
=
useCallback
(
async
()
=>
{
try
{
try
{
setLoading
(
true
);
setLoading
(
true
);
...
@@ -108,25 +103,6 @@ export const Input = () => {
...
@@ -108,25 +103,6 @@ export const Input = () => {
}
}
},
[
editIndex
,
editedTask
]);
},
[
editIndex
,
editedTask
]);
const
debouncedSave
=
useCallback
(
debounce
(
async
(
id
,
newTask
)
=>
{
try
{
const
todo
=
store
.
find
(
item
=>
item
.
id
===
id
);
if
(
todo
&&
todo
.
task
!==
newTask
)
{
await
uptTodo
(
id
,
{
id
:
todo
.
id
,
task
:
newTask
,
completed
:
todo
.
completed
});
}
}
catch
(
error
)
{
console
.
error
(
"Error saving todo:"
,
error
);
}
},
500
),
[
store
]
);
const
handleChange
=
(
e
)
=>
setTask
(
e
.
target
.
value
);
const
handleEdit
=
(
e
)
=>
{
const
handleEdit
=
(
e
)
=>
{
try
{
try
{
...
@@ -134,17 +110,35 @@ export const Input = () => {
...
@@ -134,17 +110,35 @@ export const Input = () => {
const
newValue
=
e
.
currentTarget
.
textContent
;
const
newValue
=
e
.
currentTarget
.
textContent
;
setEditedTask
(
newValue
);
setEditedTask
(
newValue
);
const
newStore
=
[...
store
];
newStore
[
editIndex
]
=
{
...
newStore
[
editIndex
],
task
:
newValue
};
setStore
(
newStore
);
debouncedSave
(
store
[
editIndex
].
id
,
newValue
);
const
updatedStore
=
[...
store
];
const
original
=
updatedStore
[
editIndex
];
updatedStore
[
editIndex
]
=
{
...
original
,
task
:
newValue
};
setStore
(
updatedStore
);
if
(
debounceTimeout
.
current
)
{
clearTimeout
(
debounceTimeout
.
current
);
}
debounceTimeout
.
current
=
setTimeout
(()
=>
{
uptTodo
(
original
.
id
,
{
id
:
original
.
id
,
task
:
newValue
,
completed
:
original
.
completed
,
}).
catch
((
error
)
=>
{
console
.
error
(
"Error saving todo:"
,
error
);
});
},
1000
);
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
"Error during edit:"
,
error
);
console
.
error
(
"Error during edit:"
,
error
);
setEditIndex
(
null
);
setEditIndex
(
null
);
}
}
};
};
const
handleChange
=
(
e
)
=>
setTask
(
e
.
target
.
value
);
const
startEdit
=
(
index
)
=>
{
const
startEdit
=
(
index
)
=>
{
try
{
try
{
setEditIndex
(
index
);
setEditIndex
(
index
);
...
@@ -162,14 +156,16 @@ export const Input = () => {
...
@@ -162,14 +156,16 @@ export const Input = () => {
const
Add
=
async
()
=>
{
const
Add
=
async
()
=>
{
if
(
task
.
trim
()
===
''
)
return
;
if
(
task
.
trim
()
===
''
)
return
;
try
{
try
{
setIsNewTodoAdded
(
true
);
const
res
=
await
addTodo
(
task
);
const
res
=
await
addTodo
(
task
);
setStore
([...
store
,
res
.
data
]);
setStore
([...
store
,
res
.
data
]);
setTask
(
""
);
setTask
(
""
);
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
"Error adding todo:"
,
error
);
console
.
error
(
"Error adding todo:"
,
error
);
}
finally
{
setTimeout
(()
=>
setIsNewTodoAdded
(
false
),
500
);
}
}
};
};
const
Delete
=
async
(
id
)
=>
{
const
Delete
=
async
(
id
)
=>
{
const
confirmDelete
=
window
.
confirm
(
"Are you sure you want to delete?"
);
const
confirmDelete
=
window
.
confirm
(
"Are you sure you want to delete?"
);
if
(
confirmDelete
)
{
if
(
confirmDelete
)
{
...
@@ -214,6 +210,11 @@ export const Input = () => {
...
@@ -214,6 +210,11 @@ export const Input = () => {
}
}
}
}
}
}
className=
{
`${styles.task} ${store[editIndex]?.completed ? 'completed' : ''}`
}
className=
{
`${styles.task} ${store[editIndex]?.completed ? 'completed' : ''}`
}
style=
{
{
display
:
'inline-block'
,
width
:
'100%'
,
boxSizing
:
'border-box'
}
}
>
>
{
editedTask
}
{
editedTask
}
</
div
>
</
div
>
...
@@ -223,7 +224,6 @@ export const Input = () => {
...
@@ -223,7 +224,6 @@ export const Input = () => {
return
(
return
(
<
span
<
span
className=
{
`${styles.task} ${store[editIndex]?.completed ? styles.completed : ''}`
}
className=
{
`${styles.task} ${store[editIndex]?.completed ? styles.completed : ''}`
}
onClick=
{
()
=>
startEdit
(
editIndex
)
}
onClick=
{
()
=>
startEdit
(
editIndex
)
}
>
>
{
typeof
store
[
editIndex
]?.
task
===
'string'
{
typeof
store
[
editIndex
]?.
task
===
'string'
...
@@ -237,7 +237,7 @@ export const Input = () => {
...
@@ -237,7 +237,7 @@ export const Input = () => {
return
(
return
(
<
MyTask
.
Provider
value=
{
{
store
,
setStore
}
}
>
<
MyTask
.
Provider
value=
{
{
store
,
setStore
}
}
>
<
div
className=
"todo-container"
>
<
div
className=
"todo-container"
>
<
Header
/>
<
Header
/>
<
List
<
List
store=
{
store
}
store=
{
store
}
editIndex=
{
editIndex
}
editIndex=
{
editIndex
}
...
@@ -245,7 +245,8 @@ export const Input = () => {
...
@@ -245,7 +245,8 @@ export const Input = () => {
startEdit=
{
startEdit
}
startEdit=
{
startEdit
}
toggleComplete=
{
toggleComplete
}
toggleComplete=
{
toggleComplete
}
Delete=
{
Delete
}
Delete=
{
Delete
}
/>
isNewTodoAdded=
{
isNewTodoAdded
}
/>
<
form
onSubmit=
{
(
e
)
=>
e
.
preventDefault
()
}
>
<
form
onSubmit=
{
(
e
)
=>
e
.
preventDefault
()
}
>
<
div
className=
{
styles
.
merge
}
>
<
div
className=
{
styles
.
merge
}
>
<
input
<
input
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment