Skip to content

Adding Time wise Filter Feauture in TO-DO LIST #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions TODO-LIST/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,9 @@ input:focus{
left:0;
border-radius: 999px;
transform: translateY(-10px) rotate(45deg);
}

.column{
display: 'flex';
flex-direction:'column';
}
15 changes: 12 additions & 3 deletions TODO-LIST/src/components/Todo.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import React from 'react'

const Todo = ({ todo,deleteTodo,moveUp,moveDown,total,index}) => {
const Todo = ({ todo,deleteTodo,moveUp,moveDown,total,index,createdAt}) => {

const formatDate = (dateString) => {
const date = new Date(dateString);
const options = { month: 'long', day: 'numeric', year: 'numeric', hour: '2-digit', minute: '2-digit',second: '2-digit' };
return new Intl.DateTimeFormat('en-US', options).format(date);
};

return (
<div className='todo'>
<p className='text'>{todo.text}</p>
<div className='utils'>
<div className='column' >
<p className='text'>{todo.text}</p>
<p className='date' style={{fontFamily:'monospace',fontSize:'10px'}} >{formatDate(todo.createdAt)}</p>
</div>
<div className='utils'>
<div className='go-up' style={{borderBottomColor:index === 0 ? 'grey' : '#1DA1F2'}} onClick={() => moveUp(todo.id)}></div>
<div className='go-down' style={{borderTopColor:index === total - 1 ? 'grey' : '#1DA1F2'}} onClick={() => moveDown(todo.id)}></div>
<div className='remove' onClick={() => deleteTodo(todo.id)}></div>
Expand Down
27 changes: 24 additions & 3 deletions TODO-LIST/src/pages/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@ const getTodos = () => {
return []
}

const getSort = () =>{
let sort=localStorage.getItem('sort');
return sort ? sort : true;
}

const Home = () => {
const [todos, setTodos] = useState(getTodos())
const [todo, setTodo] = useState('')
const [oldfirst, setOldfirst]= useState(getSort());

const addTodo = (e) => {
e.preventDefault()
e.preventDefault();
const current = new Date();
const currentDateTime = current.toISOString();
if (todo && todos.findIndex(t => t.text === todo) === -1) {
setTodos(prev => [...prev, {id:nanoid(),text:todo}])
setTodo('')
setTodos(prev => [...prev, { id: nanoid(), text: todo, createdAt: currentDateTime }]);
setTodo('');
}
}

Expand All @@ -45,6 +53,18 @@ const Home = () => {
setTodos([...todos])
}
}

const handleDateSort = () => {
let sortedTodos = [...todos];
if (oldfirst) {
sortedTodos.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
} else {
sortedTodos.sort((a, b) => new Date(a.createdAt) - new Date(b.createdAt));
}
setTodos(sortedTodos);
setOldfirst(!oldfirst);
localStorage.setItem('sort',!oldfirst);
};
useEffect(() => {
localStorage.setItem('todos', JSON.stringify(todos))
},[todos])
Expand All @@ -56,6 +76,7 @@ const Home = () => {
<input type="text" placeholder='Add item...' value={todo} onChange={(e) => setTodo(e.target.value)}/>
<input type='button' onClick={addTodo} className='btn-addTodo' value='Add' />
</form>
<div style={{width:'300px',fontWeight:700}} className='btn-addTodo' onClick={handleDateSort} >{oldfirst ?'Oldest To Newest' : 'Newest To Oldest'}</div>
{
todos.length? (
<div className='todo-list'>
Expand Down