Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
Todo-Storybook
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
Madhankumar
Todo-Storybook
Commits
02a0ef97
Commit
02a0ef97
authored
Sep 26, 2023
by
Madhankumar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
code changes
parent
98006a02
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
21 deletions
+19
-21
index.js
src/app-context/index.js
+12
-13
index.js
src/pages/index.js
+7
-8
No files found.
src/app-context/index.js
View file @
02a0ef97
...
...
@@ -5,14 +5,14 @@ export const TodoContext = createContext();
export
const
useAppContext
=
()
=>
useContext
(
TodoContext
);
export
function
TodoProvider
({
children
})
{
const
[
todo
,
setTodo
]
=
useState
([]);
const
[
loading
,
set
Loading
]
=
useState
(
false
);
const
[
todo
s
,
setTodos
]
=
useState
([]);
const
[
isLoading
,
setIs
Loading
]
=
useState
(
false
);
//POST METHOD
async
function
addTodo
({
title
})
{
try
{
//default isCompleted is false
const
response
=
await
api
.
addTodo
({
title
,
isCompleted
:
false
});
setTodo
([...
todo
,
response
]);
setTodo
s
([...
todos
,
response
]);
}
catch
(
err
)
{
toast
.
error
(
err
.
message
);
}
...
...
@@ -21,11 +21,10 @@ export function TodoProvider({ children }) {
async
function
updateTodo
({
id
,
title
,
isCompleted
})
{
try
{
const
response
=
await
api
.
updateTodo
({
id
,
title
,
isCompleted
});
// const index = todo.map((item) => item.id).indexOf(id);
// todo.splice(index, 1, { id, title, isCompleted });
const
data
=
await
response
.
json
();
setTodo
(
todo
.
map
((
e
)
=>
(
e
.
id
===
id
?
data
:
e
)));
const
data
=
await
response
.
json
();
const
result
=
todos
.
map
((
e
)
=>
(
e
.
id
===
id
?
data
:
e
));
setTodos
(
result
);
}
catch
(
err
)
{
toast
.
error
(
err
.
message
);
}
...
...
@@ -35,8 +34,8 @@ export function TodoProvider({ children }) {
async
function
getTodos
()
{
try
{
const
response
=
await
api
.
getTodos
();
setTodo
(
response
);
setLoading
(
true
);
setTodo
s
(
response
);
set
Is
Loading
(
true
);
return
response
;
}
catch
(
err
)
{
toast
.
error
(
err
.
message
);
...
...
@@ -47,16 +46,16 @@ export function TodoProvider({ children }) {
async
function
deleteTodo
(
id
)
{
try
{
await
api
.
deleteTodo
(
id
);
setTodo
(
todo
.
filter
((
e
)
=>
e
.
id
!==
id
)
);
const
result
=
todos
.
filter
((
e
)
=>
e
.
id
!==
id
);
setTodo
s
(
result
);
}
catch
(
err
)
{
toast
.
error
(
err
.
message
);
}
}
const
value
=
{
todo
,
l
oading
,
todo
s
,
isL
oading
,
addTodo
,
getTodos
,
deleteTodo
,
...
...
src/pages/index.js
View file @
02a0ef97
import
React
,
{
useState
}
from
"react"
;
import
Layout
from
"../components/base/layout"
;
import
Form
from
"../components/top-level/form"
;
import
Tasks
from
"../components/top-level/tasks"
;
...
...
@@ -6,19 +5,19 @@ import { useAppContext } from "../app-context/index";
import
{
useEffect
}
from
"react"
;
export
default
function
Pages
()
{
const
{
todo
,
l
oading
,
addTodo
,
getTodos
,
deleteTodo
,
updateTodo
}
=
const
{
todo
s
,
isL
oading
,
addTodo
,
getTodos
,
deleteTodo
,
updateTodo
}
=
useAppContext
();
console
.
log
(
loading
);
useEffect
(()
=>
{
getTodos
();
},
[]);
const
count
=
todos
?.
filter
((
e
)
=>
!
e
.
isCompleted
).
length
;
return
(
<>
{
l
oading
?
(
<
Layout
count
=
{
todo
?.
filter
((
e
)
=>
!
e
.
isCompleted
).
length
}
>
{
todo
?.
length
>
0
?
(
<
Tasks
tasks
=
{
todo
}
onChange
=
{
updateTodo
}
onDelete
=
{
deleteTodo
}
/
>
{
isL
oading
?
(
<
Layout
count
=
{
count
}
>
{
todo
s
?.
length
>
0
?
(
<
Tasks
tasks
=
{
todo
s
}
onChange
=
{
updateTodo
}
onDelete
=
{
deleteTodo
}
/
>
)
:
(
<
h3
>
No
task
<
/h3
>
)}
...
...
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