Commit 5e6e38c9 by Madhankumar

code optimization

parent 35b98601
{
"task": [
{
"title": "go to office",
"checked": false,
"id": 4
"title": "swsws",
"isCompleted": false,
"id": 1
},
{
"title": "meeting in office",
"checked": true,
"id": 5
"title": "sddd",
"isCompleted": false,
"id": 2
}
]
}
\ No newline at end of file
import { useEffect, useState } from "react";
import "./App.css";
import Layout from "./Components/Base/Layout";
import Tasks from "./Components/TopLevel/Tasks";
import Form from "./Todo/Form";
import Layout from "./components/base/layout";
import Form from "./components/top-level/form";
import Tasks from "./components/top-level/tasks";
function App() {
const [task, setTask] = useState([]);
......@@ -26,30 +26,27 @@ function App() {
const response = await tasktracker.json();
return response;
};
const remainder = async (datas) => {
const toogle = await getTaskById(datas.id);
const result = { ...toogle, checked: !toogle.checked };
const remainder = async (id) => {
const toogle = await getTaskById(id);
const result = { ...toogle, isCompleted: !toogle.isCompleted };
const res = await fetch(`http://192.168.1.91:5000/task/${datas.id}`, {
const res = await fetch(`http://192.168.1.91:5000/task/${id}`, {
method: "PUT",
headers: { "Content-type": "application/json" },
body: JSON.stringify(result),
});
const data = await res.json();
setTask(
task.map((e) => (e.id === data.id ? { ...e, checked: data.checked } : e))
task.map((e) =>
e.id === data.id ? { ...e, isCompleted: data.isCompleted } : e
)
);
};
const deleteTask = async (data) => {
var result = window.confirm("Want to delete?");
if (result) {
//Logic to delete the item
await fetch(`http://192.168.1.91:5000/task/${data.id}`, {
const deleteTask = async (id) => {
await fetch(`http://192.168.1.91:5000/task/${id}`, {
method: "DELETE",
});
setTask(task.filter((e) => e.id !== data.id));
}
setTask(task.filter((e) => e.id !== id));
};
useEffect(() => {
getTask();
......@@ -57,15 +54,13 @@ function App() {
return (
<div className="App">
<Layout count={task.filter((e) => !e.checked).length}>
{task.length > 0 && (
<Tasks
tasksData={task}
handleClick={remainder}
handleCloses={deleteTask}
/>
<Layout count={task.filter((e) => !e.isCompleted).length}>
{task.length > 0 ? (
<Tasks tasklist={task} remainder={remainder} deleted={deleteTask} />
) : (
<h3>No task</h3>
)}
<Form bottom={true} addtask={addTask} />
<Form onSubmit={addTask} />
</Layout>
</div>
);
......
.btn{
font-size:18px;
outline:none;
color:black;
button {
font-size: 18px;
outline: none;
color: black;
border: 1px solid #ccc;
border-radius: 5px;
}
.btn-sm{
background-color: white;
padding: 14px 18px;
}
.btn-md {
padding: 16px 20px;
}
.btn-lg {
padding: 18px 25px;
}
import Button from ".";
export default {
title: "Base/Button",
component: Button,
argTypes: { onClick: { action: "Clicked" } },
};
export const button = {
args: {
children: "Submit",
},
};
import React from "react";
import PropTypes from "prop-types";
import "../Button/button.css";
import "./button.css";
function Button({ label, backgroundColor, handleClick, size }) {
const style = {
backgroundColor,
};
return (
<div>
<button className={`btn btn-${size}`} style={style} onClick={handleClick}>
{label}
</button>
</div>
);
function Button({ children, ...props }) {
return <button {...props}>{children}</button>;
}
Button.propTypes = {
label: PropTypes.string,
backgroundColor: PropTypes.string,
handleClick: PropTypes.func,
};
Button.defaultProps = {
label: "Submit",
backgroundColor: "white",
};
export default Button;
......@@ -4,12 +4,7 @@ input[type="checkbox"]{
cursor: pointer;
}
label{
position: absolute;
top: 2.2em;
user-select: none;
}
.chk-bg{
padding: 1rem;
.checkbox-bg{
background: white;
}
import Checkbox from ".";
export default {
title: "Base/Checkbox",
component: Checkbox,
argTypes: { onChange: { action: "checked" } },
};
export const checkbox = {
isCompleted: false,
};
// const Template = (args) => <Checkbox {...args} />;
// export const checkbox = Template.bind({});
// checkbox.args = {
// checked: false,
// };
import React from "react";
import "../Checkbox/checkbox.css";
import "./checkbox.css";
function Checkbox({ handleClick, ...args }) {
const handleChange = () => {
args.checked = !args.checked;
let val = args.checked;
console.log(val);
//args.checked = !args.checked;
const result = val ? "checked" : "unChecked";
// console.log(result);
handleClick(result);
};
function Checkbox({ ...props }) {
return (
<div className="chk-bg">
<input
className="chkBox"
type="checkbox"
name="checkbox"
{...args}
onChange={handleChange}
/>
<div className="checkbox-bg">
<input type="checkbox" name="checkbox" {...props} />
</div>
);
}
......
import React from "react";
import "../Input/input.css";
import "./input.css";
function Input({ changeInput, inputValue }) {
const handleChange = (e) => {
e.preventDefault();
changeInput(e.target.value);
};
function Input({ ...props }) {
return (
<>
<input
type="text"
required
placeholder="Enter Item"
value={inputValue}
onChange={handleChange}
></input>
</>
<input type="text" required placeholder="Enter Item" {...props}></input>
);
}
......
import Input from "../Components/Base/Input";
import Input from ".";
export default {
title: "Base/Input",
component: Input,
};
const Template = (args) => <Input {...args} />;
export const input = Template.bind({});
input.args = {
changeInput: () => {},
};
export const input = {};
import "../Layout/layout.css";
import "./layout.css";
function Layout({ count, ...props }) {
console.log(props);
return (
<div className="container">
<h2 className="header">You have {count || 0} Todos</h2>
<div class="inner-section" {...props}></div>
<h2>
You have {count} {count > 1 ? "Todos" : "Todo"}
</h2>
{/* inner section that contain (tasklist and forms) */}
<div {...props}></div>
</div>
);
}
......
.container {
height: 300px;
min-height: 50vh;
width: min(100dvw - 5rem);
width: min(100dvw - 5rem);
margin-inline: auto;
box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
......@@ -9,7 +8,7 @@ width: min(100dvw - 5rem);
position: relative;
}
.header {
.container h2 {
box-shadow: rgba(99, 99, 99, 0.2) 0px 2px 8px 0px;
border: 1px solid #ccc;
text-align: center;
......@@ -18,9 +17,13 @@ width: min(100dvw - 5rem);
font-weight: 800px;
background: white;
}
@media screen and (min-width:1024px) {
h3 {
margin-left: 1rem;
height: 200px;
text-align: center;
}
@media screen and (min-width: 1024px) {
.container {
width: 40dvw
width: 40dvw;
}
}
import Layout from ".";
export default {
title: "Base/Layout",
component: Layout,
};
export const layout = {
args: {
count: 0,
},
};
import React from "react";
import { FaTimesCircle } from "react-icons/fa";
import CheckBox from "../../Base/Checkbox";
import "./task.css";
function Task({ handleClose, handleClicks, task }) {
return (
<>
<div className="task-section">
<div className="task">
<div className="task-title">
<CheckBox handleClick={handleClicks} checked={task.checked} />
<p className={`${task.checked ? "strike-through" : ""}`}>
{task.title}
</p>
</div>
<div className="task-close">
<FaTimesCircle className="fa-close" onClick={handleClose} />
</div>
</div>
</div>
</>
);
}
export default Task;
import React from "react";
import "./tasks.css";
import Task from "../../TopLevel/Task";
function Tasks({ handleClick, tasksData, handleCloses }) {
return (
<>
<div className="tasklist">
{tasksData.length > 0
? tasksData.map((tasks) => (
<Task
task={tasks}
handleClicks={() => handleClick(tasks)}
handleClose={() => handleCloses(tasks)}
/>
// <div className="tasks">
// <div className="task-section">
// <div className="task">
// <CheckBox handleClick={handleClicks} />
// <p>{e.title} </p>
// </div>
// <FaTimes
// className="fa-close"
// style={{ color: "red" }}
// onClick={handleClose}
// />
// </div>
// </div>
))
: "No task"}
</div>
</>
);
}
export default Tasks;
.form-section {
width: calc(100% - 2.5rem);
padding: 1rem;
background: white;
}
.form-section form {
display: flex;
align-items: center;
gap: 1rem;
}
import Form from ".";
export default {
title: "Top-Level/Form",
component: Form,
argTypes: { onSubmit: { action: "Submit" } },
};
export const form = {};
// const Template = (args) => <Form {...args} />;
// export const form = Template.bind({});
// form.args = {};
import { useState } from "react";
import Button from "../../base/button";
import Input from "../../base/input";
import "./form.css";
function Form({ onSubmit }) {
const [title, setTitle] = useState("");
const handleSubmit = (e) => {
e.preventDefault();
onSubmit({ title, isCompleted: false });
setTitle("");
};
return (
<div className="form-section">
<form onSubmit={handleSubmit}>
<Input
required
value={title}
onChange={(e) => setTitle(e.target.value)}
/>
<Button>Submits</Button>
</form>
</div>
);
}
export default Form;
import { FaTimesCircle } from "react-icons/fa";
import Checkbox from "../../base/checkbox";
import "./task.css";
function Task({ title, isCompleted, onDelete, onChange }) {
const handleDelete = () => {
const dialog = window.confirm("Do you want to delete?");
if (dialog) {
onDelete();
return true;
}
return false;
};
const handleChange = (e) => {
e.preventDefault();
onChange();
};
return (
<div className="task-section">
<Checkbox onChange={handleChange} checked={isCompleted} />
<p className={`${isCompleted ? "strike-through" : ""}`}>{title}</p>
<FaTimesCircle className="fa-close" onClick={handleDelete} />
</div>
);
}
export default Task;
.task-section {
background: white;
box-shadow: rgba(0, 0, 0, 0.06) 0px 2px 4px 0px inset;
clip-path: inset(0 -100vmax);
}
.task-section .task,
.task-title {
display: flex;
justify-content: space-between;
gap: 0.5rem;
padding: 0.2rem 0.8rem;
align-items: center;
padding: 0.5rem 0.8rem;
justify-content: space-between;
background: white;
box-shadow: rgba(0, 0, 0, 0.06) 0px 2px 4px 0px inset;
clip-path: inset(0 -100vmax);
}
.fa-close{
color:#ccc;
font-size:25px
}
.task-section .task,
.task-title>p{
color:#9e9b9b;
:nth-child(2) {
flex: 1;
}
.fa-close {
color: #ccc;
font-size: 25px;
}
p {
color: #9e9b9b;
}
.strike-through{
.strike-through {
text-decoration: line-through;
}
import Task from ".";
export default {
title: "Top-Level/Task",
component: Task,
argTypes: {
onDelete: { action: "Deleted" },
onChange: { action: "checked" },
},
};
export const task = {
args: {
title: "Test Task",
isCompleted: false,
},
};
import "./tasks.css";
import Task from "../task";
function Tasks({ tasklist, deleted, remainder }) {
return (
<div className="tasklist">
{tasklist.length > 0 &&
tasklist.map(({ id, title, isCompleted }) => (
<Task
key={id}
id={id}
title={title}
isCompleted={isCompleted}
onChange={() => remainder(id)}
onDelete={() => deleted(id)}
/>
))}
</div>
);
}
export default Tasks;
.form-container{
display: flex;
gap:1rem;
width: calc(100% - 2.5rem);
padding: 1rem;
}
.form-container form{
flex:1;
overflow: auto;
}
.form-bottom{
position:absolute;
bottom: 0;
}
\ No newline at end of file
import React, { useState } from "react";
import Button from "../../Components/Base/Button";
import Input from "../../Components/Base/Input";
import "../Form/form.css";
function Form({ addtask, bottom }) {
const [input, setInput] = useState("");
const submitdata = (e) => {
e.preventDefault();
setInput("");
addtask({ title: input, checked: false });
};
const handleInput = (e) => {
setInput(e);
};
return (
<>
<form onSubmit={submitdata}>
<div className={`form-container ${bottom ? "form-bottom" : ""}`}>
<Input
required={true}
changeInput={(e) => handleInput(e)}
inputValue={input}
/>
<Button size={"md"} />
</div>
</form>
</>
);
}
export default Form;
{
"task":[
]
}
\ No newline at end of file
import Button from "../Components/Base/Button";
export default {
title: "Base/Button",
component: Button,
argTypes: { handleClick: { action: "Clicked" } },
};
const Template = (args) => <Button {...args} />;
export const button = Template.bind({});
button.args = {
label: "Submit",
backgroundColor: "white",
size: "sm",
};
import Checkbox from "../Components/Base/Checkbox";
export default {
title: "Base/Checkbox",
component: Checkbox,
argTypes: { handleClick: { action: "clicked" } },
};
const Template = (args) => <Checkbox {...args} />;
export const checkbox = Template.bind({});
checkbox.args = {
checked: false,
};
import Form from "../Todo/Form";
export default {
title: "Form",
component: Form,
argTypes: { handleSubmit: { action: "Submit" } },
};
const Template = (args) => <Form {...args} />;
export const form = Template.bind({});
form.args = {};
import Layout from "../Components/Base/Layout";
export default {
title: "Base/Layout",
component: Layout,
};
const Template = (args) => <Layout {...args} />;
export const layout = Template.bind({});
layout.args = {};
import Task from "../Components/TopLevel/Task";
export default {
title: "TopLevel/Task",
component: Task,
argTypes: {
handleClose: { action: "Deleted" },
handleClicks: { action: "checked" },
},
};
const taskData = {
id: "1",
title: "Test Task",
checked: false,
};
const Template = (args) => <Task task={{ ...taskData }} {...args} />;
export const task = Template.bind({});
task.args = {};
import Tasks from "../Components/TopLevel/Tasks";
import { taskData } from "../Components/TopLevel/Task";
export default {
title: "TopLevel/Tasks",
component: Tasks,
argTypes: {
handleCloses: { action: "Deleted" },
handleClick: { action: "checked" },
},
};
const defaultTasksData = [
{ ...taskData, id: "1", title: "Task 1", checked: false },
{ ...taskData, id: "2", title: "Task 2", checked: true },
{ ...taskData, id: "3", title: "Task 3", checked: true },
];
const Template = (args) => <Tasks tasksData={defaultTasksData} {...args} />;
export const tasks = Template.bind({});
tasks.args = {};
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