blob: 4ef227d8dc355bfc0fe6b95ce5a79ae937404304 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
import React from 'react';
import { Mutation } from 'react-apollo';
import PropTypes from 'prop-types';
import { REMOVE_ITEM_MUTATION, ALL_ITEMS_QUERY } from '../queries/queries.graphql';
class DeleteItem extends React.Component {
static propTypes = {
id: PropTypes.string.isRequired,
};
update = (proxy, payload) => {
const deletedItem = payload.data.deleteItem;
const data = proxy.readQuery({ query: ALL_ITEMS_QUERY });
// filter this one out
data.items = data.items.filter(item => item.id !== deletedItem.id);
// write the data back to the proxy
proxy.writeQuery({ query: ALL_ITEMS_QUERY, data });
};
render() {
return (
<Mutation
mutation={REMOVE_ITEM_MUTATION}
variables={{ id: this.props.id }}
update={this.update}
>
{(removeItem, { error }) => (
<button
onClick={() => {
if (confirm('Are you sure you want to delete this item?')) {
removeItem();
}
}}
>
{error ? error.message : '× Delete Item'}
</button>
)}
</Mutation>
);
}
}
export default DeleteItem;
|