Skip to content

Commit e3ad58a

Browse files
committed
Implement Delete Icon ❌
1 parent fe37020 commit e3ad58a

File tree

4 files changed

+87
-6
lines changed

4 files changed

+87
-6
lines changed

src/components/Main/DeleteTodo.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import React from "react";
2+
import styled from "react-emotion";
3+
import { withTheme } from "emotion-theming";
4+
import { Query, Mutation } from "react-apollo";
5+
import Icon from "react-icons-kit";
6+
import { bin } from "react-icons-kit/icomoon/bin";
7+
8+
import { DELETE_TODO } from "../../graphql/mutation/DELETE_TODO";
9+
import { GET_TODOS_BY_PRODUCT } from "../../graphql/queries/GET_TODOS_BY_PRODUCT";
10+
11+
const BinIcon = styled(Icon)`
12+
padding-left: 1rem;
13+
`;
14+
15+
const DeleteTodoContainer = ({ id, productId, completed, theme }) => {
16+
return (
17+
<Mutation
18+
mutation={DELETE_TODO}
19+
optimisticResponse={{
20+
__typename: "Mutation",
21+
deleteTodo: {
22+
__typename: "Todo",
23+
id,
24+
},
25+
}}
26+
update={(cache, { data }) => {
27+
// delete todo item
28+
const cacheData = cache.readQuery({
29+
query: GET_TODOS_BY_PRODUCT,
30+
variables: {
31+
id: productId,
32+
completed,
33+
},
34+
});
35+
36+
const todos = cacheData.product.todos.filter(t => t.id !== id); // make a shallow copy otherwise error "Object is not extensible" is thrown
37+
38+
const newData = {
39+
...cacheData,
40+
product: {
41+
...cacheData.product,
42+
todos: todos,
43+
},
44+
};
45+
46+
cache.writeQuery({
47+
query: GET_TODOS_BY_PRODUCT,
48+
variables: {
49+
id: productId,
50+
completed,
51+
},
52+
data: newData,
53+
});
54+
}}
55+
>
56+
{mutate => (
57+
<BinIcon
58+
onClick={() => {
59+
mutate({ variables: { id } });
60+
}}
61+
icon={bin}
62+
color={theme.binIcon.color}
63+
size={16}
64+
/>
65+
)}
66+
</Mutation>
67+
);
68+
};
69+
70+
export const DeleteTodo = withTheme(DeleteTodoContainer);

src/components/Main/index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { checkmark } from "react-icons-kit/icomoon/checkmark";
99
import { cross } from "react-icons-kit/icomoon/cross";
1010

1111
import { CreateTodo } from "./CreateTodo";
12+
import { DeleteTodo } from "./DeleteTodo";
1213
import { Loading } from "../Loading/index";
1314
import { Error } from "../Error/index";
1415
import { Status } from "./Status";
@@ -210,12 +211,11 @@ class MainContainer extends React.Component {
210211
}}
211212
>
212213
{mutate => (
213-
<TodoBox
214-
onClick={() => {
215-
mutate({ variables: { id: todo.id } });
216-
}}
217-
>
214+
<TodoBox>
218215
<StatusIcon
216+
onClick={() => {
217+
mutate({ variables: { id: todo.id } });
218+
}}
219219
icon={completed ? checkmark : cross}
220220
color={
221221
completed
@@ -226,6 +226,11 @@ class MainContainer extends React.Component {
226226
/>
227227
<Todo>
228228
{this._getHashtag(todo.body, hashtag)}
229+
<DeleteTodo
230+
id={todo.id}
231+
productId={productId}
232+
completed={completed}
233+
/>
229234
</Todo>
230235
</TodoBox>
231236
)}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import gql from "graphql-tag";
22

33
export const DELETE_TODO = gql`
4-
mutation($id: ID!) {
4+
mutation deleteTodo($id: ID!) {
55
deleteTodo(id: $id)
66
}
77
`;

src/styles/theme.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ export const theme = {
4848
doneColor: "#000000",
4949
pendingColor: "#000000",
5050
},
51+
binIcon: {
52+
color: "#000000",
53+
},
5154
},
5255
DARK: {
5356
global: {
@@ -95,5 +98,8 @@ export const theme = {
9598
doneColor: "#ffffff",
9699
pendingColor: "#ffffff",
97100
},
101+
binIcon: {
102+
color: "#ffffff",
103+
},
98104
},
99105
};

0 commit comments

Comments
 (0)