Commit 02a0ef97 by Madhankumar

code changes

parent 98006a02
...@@ -5,14 +5,14 @@ export const TodoContext = createContext(); ...@@ -5,14 +5,14 @@ export const TodoContext = createContext();
export const useAppContext = () => useContext(TodoContext); export const useAppContext = () => useContext(TodoContext);
export function TodoProvider({ children }) { export function TodoProvider({ children }) {
const [todo, setTodo] = useState([]); const [todos, setTodos] = useState([]);
const [loading, setLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
//POST METHOD //POST METHOD
async function addTodo({ title }) { async function addTodo({ title }) {
try { try {
//default isCompleted is false //default isCompleted is false
const response = await api.addTodo({ title, isCompleted: false }); const response = await api.addTodo({ title, isCompleted: false });
setTodo([...todo, response]); setTodos([...todos, response]);
} catch (err) { } catch (err) {
toast.error(err.message); toast.error(err.message);
} }
...@@ -21,11 +21,10 @@ export function TodoProvider({ children }) { ...@@ -21,11 +21,10 @@ export function TodoProvider({ children }) {
async function updateTodo({ id, title, isCompleted }) { async function updateTodo({ id, title, isCompleted }) {
try { try {
const response = await api.updateTodo({ id, title, isCompleted }); 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) { } catch (err) {
toast.error(err.message); toast.error(err.message);
} }
...@@ -35,8 +34,8 @@ export function TodoProvider({ children }) { ...@@ -35,8 +34,8 @@ export function TodoProvider({ children }) {
async function getTodos() { async function getTodos() {
try { try {
const response = await api.getTodos(); const response = await api.getTodos();
setTodo(response); setTodos(response);
setLoading(true); setIsLoading(true);
return response; return response;
} catch (err) { } catch (err) {
toast.error(err.message); toast.error(err.message);
...@@ -47,16 +46,16 @@ export function TodoProvider({ children }) { ...@@ -47,16 +46,16 @@ export function TodoProvider({ children }) {
async function deleteTodo(id) { async function deleteTodo(id) {
try { try {
await api.deleteTodo(id); await api.deleteTodo(id);
const result = todos.filter((e) => e.id !== id);
setTodo(todo.filter((e) => e.id !== id)); setTodos(result);
} catch (err) { } catch (err) {
toast.error(err.message); toast.error(err.message);
} }
} }
const value = { const value = {
todo, todos,
loading, isLoading,
addTodo, addTodo,
getTodos, getTodos,
deleteTodo, deleteTodo,
......
import React, { useState } from "react";
import Layout from "../components/base/layout"; import Layout from "../components/base/layout";
import Form from "../components/top-level/form"; import Form from "../components/top-level/form";
import Tasks from "../components/top-level/tasks"; import Tasks from "../components/top-level/tasks";
...@@ -6,19 +5,19 @@ import { useAppContext } from "../app-context/index"; ...@@ -6,19 +5,19 @@ import { useAppContext } from "../app-context/index";
import { useEffect } from "react"; import { useEffect } from "react";
export default function Pages() { export default function Pages() {
const { todo, loading, addTodo, getTodos, deleteTodo, updateTodo } = const { todos, isLoading, addTodo, getTodos, deleteTodo, updateTodo } =
useAppContext(); useAppContext();
console.log(loading);
useEffect(() => { useEffect(() => {
getTodos(); getTodos();
}, []); }, []);
const count = todos?.filter((e) => !e.isCompleted).length;
return ( return (
<> <>
{loading ? ( {isLoading ? (
<Layout count={todo?.filter((e) => !e.isCompleted).length}> <Layout count={count}>
{todo?.length > 0 ? ( {todos?.length > 0 ? (
<Tasks tasks={todo} onChange={updateTodo} onDelete={deleteTodo} /> <Tasks tasks={todos} onChange={updateTodo} onDelete={deleteTodo} />
) : ( ) : (
<h3>No task</h3> <h3>No task</h3>
)} )}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment