Skip to content

[fix]: dim and order of to-do #31

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 1 commit into
base: main
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
3 changes: 2 additions & 1 deletion client/src/components/ItemDisplay.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
}

&.completed {
text-decoration: line-through;
color: #aaa;
opacity: 0.5;
}
}

Expand Down
40 changes: 26 additions & 14 deletions client/src/components/TodoListCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,49 @@ import { ItemDisplay } from './ItemDisplay';
export function TodoListCard() {
const [items, setItems] = useState(null);

const sortItems = (items) => {
// Sort items: incomplete first, completed last, and newly added at the top
return items.sort((a, b) => {
if (a.completed === b.completed) {
return b.id.localeCompare(a.id); // Compare by ID to show newly added items first
}
return a.completed - b.completed; // Incomplete items first
});
};

useEffect(() => {
fetch('/api/items')
.then((r) => r.json())
.then(setItems);
.then((data) => setItems(sortItems(data))); // Apply sorting after fetching items
}, []);

const onNewItem = useCallback(
(newItem) => {
setItems([...items, newItem]);
setItems((prevItems) => sortItems([...prevItems, newItem]));
},
[items],
[],
);

const onItemUpdate = useCallback(
(item) => {
const index = items.findIndex((i) => i.id === item.id);
setItems([
...items.slice(0, index),
item,
...items.slice(index + 1),
]);
(updatedItem) => {
setItems((prevItems) => {
const updatedItems = prevItems.map((item) =>
item.id === updatedItem.id ? updatedItem : item
);
return sortItems(updatedItems);
});
},
[items],
[],
);

const onItemRemoval = useCallback(
(item) => {
const index = items.findIndex((i) => i.id === item.id);
setItems([...items.slice(0, index), ...items.slice(index + 1)]);
setItems((prevItems) => {
const updatedItems = prevItems.filter((i) => i.id !== item.id);
return sortItems(updatedItems);
});
},
[items],
[],
);

if (items === null) return 'Loading...';
Expand Down