-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathRemoveCompletedTodosMutation.js
More file actions
75 lines (75 loc) · 1.66 KB
/
RemoveCompletedTodosMutation.js
File metadata and controls
75 lines (75 loc) · 1.66 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
export default class RemoveCompletedTodosMutation extends Relay.Mutation {
static fragments = {
// TODO: Make completedCount, edges, and totalCount optional
todos: () => Relay.QL`
fragment on TodoConnection {
completedCount,
edges {
node {
complete,
id,
},
},
totalCount,
}
`,
viewer: () => Relay.QL`
fragment on User {
id,
}
`,
};
getMutation() {
return Relay.QL`mutation{removeCompletedTodos}`;
}
getFatQuery() {
return Relay.QL`
fragment on RemoveCompletedTodosPayload {
deletedTodoIds,
viewer {
todos {
completedCount,
totalCount,
},
},
}
`;
}
getConfigs() {
return [{
type: 'NODE_DELETE',
parentName: 'viewer',
parentID: this.props.viewer.id,
connectionName: 'todos',
deletedIDFieldName: 'deletedTodoIds',
}];
}
getVariables() {
return {};
}
getOptimisticResponse() {
var deletedTodoIds;
var newTotalCount;
if (this.props.todos) {
var {completedCount, totalCount} = this.props.todos;
if (completedCount !== null && totalCount !== null) {
newTotalCount = totalCount - completedCount;
}
if (this.props.todos.edges) {
deletedTodoIds = this.props.todos.edges
.filter(edge => edge.node.complete)
.map(edge => edge.node.id);
}
}
return {
deletedTodoIds,
viewer: {
id: this.props.viewer.id,
todos: {
completedCount: 0,
totalCount: newTotalCount,
},
},
};
}
}