forked from relay-tools/relay-compiler-language-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TodoApp.tsx
150 lines (137 loc) · 3.99 KB
/
TodoApp.tsx
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* This file provided by Facebook is for non-commercial testing and evaluation
* purposes only. Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import TodoList from "./TodoList"
import TodoListFooter from "./TodoListFooter"
import TodoTextInput from "./TodoTextInput"
import React from "react"
import { graphql } from "react-relay"
import {
TodoAppData$key,
useRefetchableTodoAppDataFragment,
} from "../__relay_artifacts__/TodoAppData.graphql"
import { useTodoAppAddTodoMutation } from "../__relay_artifacts__/TodoAppAddTodoMutation.graphql"
import { useTodoAppSetAppendingMutation } from "../__relay_artifacts__/TodoAppSetAppendingMutation.graphql"
interface Props {
frag: TodoAppData$key
}
graphql`
fragment TodoAppData on User
@refetchable(queryName: "TodoAppRefetchQuery")
@argumentDefinitions(
last: { type: "Int" }
first: { type: "Int" }
after: { type: "String" }
before: { type: "String" }
) {
id
totalCount
isAppending
...TodoListFooterData
...TodoList
@arguments(last: $last, first: $first, after: $after, before: $before)
}
mutation TodoAppSetAppendingMutation($isAppending: Boolean!) {
setAppending(appending: $isAppending) {
isAppending
}
}
mutation TodoAppAddTodoMutation(
$input: AddTodoInput!
$connections: [ID!]!
$append: Boolean! = true
) {
addTodo(input: $input) {
todoEdge @include(if: $append) @appendEdge(connections: $connections) {
__typename
cursor
node {
complete
id
text
}
}
todoEdge @skip(if: $append) @prependEdge(connections: $connections) {
__typename
cursor
node {
complete
id
text
}
}
viewer {
id
totalCount
}
}
}
`
let tempID = 0
const TodoApp = (props: Props) => {
const [viewer, refetch] = useRefetchableTodoAppDataFragment(props.frag)
const [addTodo] = useTodoAppAddTodoMutation()
const [setAppending] = useTodoAppSetAppendingMutation()
const handleTextInputSave = (text: string) => {
addTodo({
variables: {
input: {
text,
clientMutationId: (tempID++).toString(),
},
append: viewer.isAppending,
connections: [`client:${viewer.id}:__TodoList_todos_connection`],
},
})
}
const hasTodos = (viewer.totalCount || 0) > 0
const onSetAppend = () => {
const nextAppendVal = !viewer.isAppending
setAppending({
variables: { isAppending: nextAppendVal },
onCompleted() {
// Refetch when we reload the fragment
refetch(
{ first: nextAppendVal ? null : 10, last: nextAppendVal ? 10 : null },
{ fetchPolicy: "store-and-network" },
)
},
})
}
return (
<div>
<section className="todoapp">
<header className="header">
<h1>todos</h1>
<TodoTextInput
autoFocus={true}
className="new-todo"
onSave={handleTextInputSave}
placeholder="What needs to be done?"
/>
</header>
<TodoList viewer={viewer} />
{hasTodos && <TodoListFooter frag={viewer} onSetAppend={onSetAppend} />}
</section>
<footer className="info">
<p>Double-click to edit a todo</p>
<p>
Created by the{" "}
<a href="https://facebook.github.io/relay/">Relay team</a>
</p>
<p>
Part of <a href="http://todomvc.com">TodoMVC</a>
</p>
</footer>
</div>
)
}
export default TodoApp