Skip to content

Instantly share code, notes, and snippets.

@ljmotta
Last active October 14, 2020 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ljmotta/388f1366bfd48a5635599fcf003bdec5 to your computer and use it in GitHub Desktop.
Save ljmotta/388f1366bfd48a5635599fcf003bdec5 to your computer and use it in GitHub Desktop.
TodoListEnvelopeView Api Impl
import { useCallback, useMemo } from "react";
import { Item, TodoListChannelApi } from "../api";
export const TodoListEnvelopeView = React.forwardRef<TodoListEnvelopeViewApi, Props>((props, forwardedRef) => {
// ...
const removeItem = useCallback(
(e: React.MouseEvent<HTMLAnchorElement, MouseEvent>, item: Item) => {
e.preventDefault();
const itemsCopy = [...items];
const i = itemsCopy.indexOf(item);
if (i >= 0) {
itemsCopy.splice(i, 1);
setItems(itemsCopy);
props.channelApi.notifications.todoList__itemRemoved(item.label);
}
},
[items]
);
const updateItemCompletedStatus = useCallback(
(e: React.MouseEvent<HTMLAnchorElement, MouseEvent>, item: Item, completed: boolean) => {
e.preventDefault();
const itemsCopy = [...items];
const i = itemsCopy.indexOf(item);
if (i >= 0) {
itemsCopy[i].completed = completed;
setItems(itemsCopy);
}
},
[items]
);
const allCompleted = useMemo(() => {
const completedItems = items.filter((i) => i.completed);
return items.length > 0 && completedItems.length === items.length;
}, [items]);
// ...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment