Commit 6581c9f4 by Madhankumar

update api changes

parent 3fc39fc9
{ {
"task": [ "task": [
{ {
"sId": 1,
"title": "rhrhrhrytyty", "title": "rhrhrhrytyty",
"isCompleted": true, "isCompleted": true,
"id": 1 "id": 1
}, },
{ {
"sId": 1,
"title": "helloxxsxsxsxsxsxsxsxsxsxsxsxsxssssssssssssssssssssxssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssadddadsdsdsadasasasadasaadadadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaavrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrryyyyy", "title": "helloxxsxsxsxsxsxsxsxsxsxsxsxsxssssssssssssssssssssxssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssadddadsdsdsadasasasadasaadadadaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaavrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrryyyyy",
"isCompleted": false, "isCompleted": false,
"id": 2 "id": 2
},
{
"title": "hello",
"isCompleted": false,
"id": 3
} }
] ]
} }
\ No newline at end of file
...@@ -3,29 +3,23 @@ import "./App.css"; ...@@ -3,29 +3,23 @@ import "./App.css";
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";
import { useAppContext } from "./appcontext/index"; import { useAppContext } from "./app-context/index";
function App() { function App() {
const { task, addTask, updateremainder, getTask, deleteTask, updatetitle } = const { todo, addTodo, getTodos, deleteTodo, updateTodo } = useAppContext();
useAppContext();
useEffect(() => { useEffect(() => {
getTask(); getTodos();
}, []); }, []);
return ( return (
<div className="App"> <div className="App">
<Layout count={task?.filter((e) => !e.isCompleted).length}> <Layout count={todo?.filter((e) => !e.isCompleted).length}>
{task?.length > 0 ? ( {todo?.length > 0 ? (
<Tasks <Tasks tasks={todo} onChange={updateTodo} onDelete={deleteTodo} />
tasklist={task}
onEdit={updatetitle}
remainder={updateremainder}
deleted={deleteTask}
/>
) : ( ) : (
<h3>No task</h3> <h3>No task</h3>
)} )}
<Form onSubmit={addTask} /> <Form onSubmit={addTodo} />
</Layout> </Layout>
</div> </div>
); );
......
import React, { createContext, useContext, useState } from "react"; import React, { createContext, useContext, useState } from "react";
import { import * as api from "../lib/api";
addTodo,
getTodo,
getTodoById,
updateRemainder,
updateTitle,
deleteTodo,
} from "../lib/api";
export const TodoContext = createContext(); export const TodoContext = createContext();
export const useAppContext = () => useContext(TodoContext); export const useAppContext = () => useContext(TodoContext);
export function TodoProvider({ children }) { export function TodoProvider({ children }) {
const [task, setTask] = useState([]); const [todo, setTodo] = useState([]);
//POST METHOD //POST METHOD
async function addTask(data) { async function addTodo({ title }) {
try { try {
const taskData = await addTodo(data); //default isCompleted is false
setTask([...task, taskData]); const response = await api.addTodo({ title, isCompleted: false });
setTodo([...todo, response]);
} catch (err) { } catch (err) {
console.log(err.message); console.log(err.message);
} }
} }
//PATCH METHOD //UPDATE METHOD
async function updateremainder(id) { async function updateTodo(item) {
try { try {
const response = await updateRemainder(id); const response = await api.updateTodo(item);
setTodo(response);
setTask(response);
} catch (err) {
console.log(err.message);
}
}
async function updatetitle(data) {
try {
const result = await updateTitle(data);
setTask([...task, result]);
} catch (err) { } catch (err) {
console.log(err.message); console.log(err.message);
} }
} }
//GET METHOD //GET METHOD
async function getTask() { async function getTodos() {
try { try {
const taskData = await getTodo(); const response = await api.getTodos();
setTask(taskData); setTodo(response);
} catch (err) { } catch (err) {
console.log(err.message); console.log(err.message);
} }
} }
async function getTaskById(id) {
try {
const response = await getTodoById(id);
setTask(response);
} catch (err) {
console.log(err.message);
}
}
//DELETE METHOD //DELETE METHOD
async function deleteTask(id) { async function deleteTodo(id) {
try { try {
const taskData = await deleteTodo(id); const response = await api.deleteTodo(id);
setTask(taskData); setTodo(response);
} catch (err) { } catch (err) {
console.log(err.message); console.log(err.message);
} }
} }
const value = { const value = {
task, todo,
addTask, addTodo,
updateremainder, getTodos,
getTask, deleteTodo,
getTaskById, updateTodo,
deleteTask,
updatetitle,
}; };
return <TodoContext.Provider value={value}>{children}</TodoContext.Provider>; return <TodoContext.Provider value={value}>{children}</TodoContext.Provider>;
} }
...@@ -5,15 +5,9 @@ import "./form.css"; ...@@ -5,15 +5,9 @@ import "./form.css";
function Form({ onSubmit }) { function Form({ onSubmit }) {
const [title, setTitle] = useState(""); const [title, setTitle] = useState("");
let [count, setCount] = useState(0);
const handleSubmit = (e) => { const handleSubmit = (e) => {
e.preventDefault(); e.preventDefault();
let len = count + 1; onSubmit({ title });
setCount(len);
const data = JSON.parse(
JSON.stringify({ sId: len, title, isCompleted: false })
);
onSubmit(data);
//setTitle(""); //setTitle("");
}; };
......
...@@ -9,7 +9,7 @@ import { ...@@ -9,7 +9,7 @@ import {
useCallback, useCallback,
} from "react"; } from "react";
function Task({ id, title, isCompleted, onDelete, onChange, onEdit }) { function Task({ id = 1, title, isCompleted, onChange, onDelete }) {
const [debouncetitle, setDebounceTitle] = useState(title); const [debouncetitle, setDebounceTitle] = useState(title);
const [ischeck, setIsCheck] = useState(isCompleted); const [ischeck, setIsCheck] = useState(isCompleted);
const textarea_ref = useRef(null); const textarea_ref = useRef(null);
...@@ -25,7 +25,7 @@ function Task({ id, title, isCompleted, onDelete, onChange, onEdit }) { ...@@ -25,7 +25,7 @@ function Task({ id, title, isCompleted, onDelete, onChange, onEdit }) {
}; };
const handleChange = useCallback(() => { const handleChange = useCallback(() => {
setIsCheck(!ischeck); setIsCheck(!ischeck);
onChange({ id, isCompleted: ischeck ? true : false }); onChange({ id, isCompleted: !ischeck });
}, [ischeck]); }, [ischeck]);
const handleContent = (e) => { const handleContent = (e) => {
...@@ -41,7 +41,7 @@ function Task({ id, title, isCompleted, onDelete, onChange, onEdit }) { ...@@ -41,7 +41,7 @@ function Task({ id, title, isCompleted, onDelete, onChange, onEdit }) {
useEffect(() => { useEffect(() => {
const handler = setTimeout(() => { const handler = setTimeout(() => {
if (textarea_ref.current.value != title) { if (textarea_ref.current.value != title) {
onEdit({ id, title: debouncetitle }); onChange({ id, title: debouncetitle });
} }
}, 2000); }, 2000);
...@@ -65,11 +65,11 @@ function Task({ id, title, isCompleted, onDelete, onChange, onEdit }) { ...@@ -65,11 +65,11 @@ function Task({ id, title, isCompleted, onDelete, onChange, onEdit }) {
<textarea <textarea
ref={textarea_ref} ref={textarea_ref}
className={`${isCompleted ? "strike-through" : ""}`} className={`${ischeck ? "strike-through" : ""}`}
contentEditable={true} contentEditable={true}
value={debouncetitle} value={debouncetitle}
onChange={handleContent} onChange={handleContent}
disabled={isCompleted} disabled={ischeck}
></textarea> ></textarea>
</div> </div>
<FaTimesCircle className="fa-close" onClick={handleDelete} /> <FaTimesCircle className="fa-close" onClick={handleDelete} />
......
import "./tasks.css"; import "./tasks.css";
import Task from "../task"; import Task from "../task";
function Tasks({ tasklist, deleted, remainder, onEdit }) { function Tasks({ tasks, onDelete, onChange }) {
const handleChange = (id) => {
remainder(id);
};
return ( return (
<div className="tasklist"> <div className="tasklist">
{tasklist.length > 0 && {tasks.length > 0 &&
tasklist.map(({ id, title, isCompleted }) => ( tasks.map(({ id, title, isCompleted }) => (
<Task <Task
key={id} key={id}
id={id} id={id}
title={title} title={title}
isCompleted={isCompleted} isCompleted={isCompleted}
onEdit={onEdit} onChange={onChange}
onChange={() => handleChange(id)} onDelete={() => onDelete(id)}
onDelete={() => deleted(id)}
/> />
))} ))}
</div> </div>
......
...@@ -3,7 +3,7 @@ import ReactDOM from "react-dom/client"; ...@@ -3,7 +3,7 @@ import ReactDOM from "react-dom/client";
import "./index.css"; import "./index.css";
import App from "./App"; import App from "./App";
import reportWebVitals from "./reportWebVitals"; import reportWebVitals from "./reportWebVitals";
import { TodoProvider } from "./appcontext"; import { TodoProvider } from "./app-context";
const root = ReactDOM.createRoot(document.getElementById("root")); const root = ReactDOM.createRoot(document.getElementById("root"));
root.render( root.render(
......
const url = "http://192.168.1.91:5000/task"; const url = "http://192.168.1.91:5000/task";
//POST METHOD //POST METHOD
export async function addTodo(data) { export async function addTodo(todo) {
try { try {
const response = await fetch(url, { const response = await fetch(url, {
method: "POST", method: "POST",
headers: { "Content-type": "application/json" }, headers: { "Content-type": "application/json" },
body: JSON.stringify(data), body: JSON.stringify(todo),
}); });
const result = await response.json(); return response.json();
return result;
} catch (err) { } catch (err) {
console.log(err.message); console.log(err.message);
} }
} }
//PATCH METHOD //PATCH METHOD
export async function updateRemainder(id) { export async function updateTodo({ id, ...todo }) {
try { try {
const response = await getTodoById(id); await fetch(url + `/${id}`, {
const result = { ...response, isCompleted: !response.isCompleted };
const updateresponse = await fetch(url + `/${id}`, {
method: "PUT",
headers: { "Content-type": "application/json" },
body: JSON.stringify(result),
});
const resultData = await getTodo();
return resultData;
} catch (err) {
console.log(err.message);
}
}
export async function updateTitle(data) {
try {
const response = await fetch(url + `/${data.id}`, {
method: "PATCH", method: "PATCH",
headers: { "Content-type": "application/json" }, headers: { "Content-type": "application/json" },
body: JSON.stringify({ title: data.title }), body: JSON.stringify({ ...todo }),
}); });
const resultData = await getTodo(); const response = await getTodos();
return resultData; return response;
} catch (err) { } catch (err) {
console.log(err.message); console.log(err.message);
} }
} }
//GET METHOD //GET METHOD
export async function getTodo() { export async function getTodos() {
try { try {
const response = await fetch(url); return await fetch(url).then((data) => data.json());
const result = await response.json();
return result;
} catch (err) { } catch (err) {
console.log(err.message); console.log(err.message);
} }
} }
export async function getTodoById(id) {
try {
const response = await fetch(url + `/${id}`);
const result = await response.json();
return result;
} catch (err) {
console.log(err.message);
}
}
//DELETE METHOD //DELETE METHOD
export async function deleteTodo(id) { export async function deleteTodo(id) {
try { try {
await fetch(url + `/${id}`, { await fetch(url + `/${id}`, {
method: "DELETE", method: "DELETE",
}); });
const resultData = await getTodo(); const response = await getTodos();
return resultData; return response;
} catch (err) { } catch (err) {
console.log(err.message); console.log(err.message);
} }
......
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