blob: 9b0937837c08bf25cb56623811a72186e61e3e67 (
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
44
45
46
47
48
49
50
51
52
53
54
55
|
import React from 'react';
import { Mutation } from 'react-apollo';
import PropTypes from 'prop-types';
import gql from 'graphql-tag';
import { ALL_ITEMS_QUERY } from './Items';
const DELETE_ITEM_MUTATION = gql`
mutation deleteItem($id: ID!) {
deleteItem(id: $id) {
id
title
description
}
}
`;
class DeleteItem extends React.Component {
static propTypes = {
id: PropTypes.string.isRequired,
};
update = (cache, payload) => {
const deletedItem = payload.data.deleteItem;
const data = cache.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 cache
cache.writeQuery({ query: ALL_ITEMS_QUERY, data });
};
render() {
return (
<Mutation
mutation={DELETE_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;
export { DELETE_ITEM_MUTATION };
|