|
10 | 10 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
11 | 11 | */
|
12 | 12 |
|
13 |
| -import ChangeTodoStatusMutation from '../mutations/ChangeTodoStatusMutation'; |
14 |
| -import RemoveTodoMutation from '../mutations/RemoveTodoMutation'; |
15 |
| -import RenameTodoMutation from '../mutations/RenameTodoMutation'; |
16 |
| -import TodoTextInput from './TodoTextInput'; |
| 13 | +import ChangeTodoStatusMutation from "../mutations/ChangeTodoStatusMutation" |
| 14 | +import RemoveTodoMutation from "../mutations/RemoveTodoMutation" |
| 15 | +import RenameTodoMutation from "../mutations/RenameTodoMutation" |
| 16 | +import TodoTextInput from "./TodoTextInput" |
17 | 17 |
|
18 |
| -import * as React from 'react'; |
19 |
| -import { |
20 |
| - createFragmentContainer, |
21 |
| - graphql, |
22 |
| - RelayProp, |
23 |
| -} from 'react-relay'; |
| 18 | +import * as React from "react" |
| 19 | +import { graphql, useRelayEnvironment, useFragment } from "react-relay" |
24 | 20 |
|
25 |
| -import classnames from 'classnames'; |
| 21 | +import classnames from "classnames" |
26 | 22 |
|
27 |
| -import { Todo_todo } from '../__relay_artifacts__/Todo_todo.graphql'; |
28 |
| -import { Todo_viewer } from '../__relay_artifacts__/Todo_viewer.graphql'; |
29 |
| -import { ChangeEvent } from 'react'; |
30 |
| -import { Environment } from 'relay-runtime'; |
| 23 | +import { Todo_todo$key } from "../__relay_artifacts__/Todo_todo.graphql" |
| 24 | +import { Todo_viewer$key } from "../__relay_artifacts__/Todo_viewer.graphql" |
| 25 | +import { ChangeEvent } from "react" |
31 | 26 |
|
32 | 27 | interface Props {
|
33 |
| - relay: RelayProp |
34 |
| - todo: Todo_todo |
35 |
| - viewer: Todo_viewer |
| 28 | + todo: Todo_todo$key |
| 29 | + viewer: Todo_viewer$key |
36 | 30 | }
|
37 | 31 |
|
38 |
| -class Todo extends React.Component<Props> { |
39 |
| - state = { |
40 |
| - isEditing: false, |
41 |
| - }; |
42 |
| - _handleCompleteChange = (e: ChangeEvent<HTMLInputElement>) => { |
43 |
| - const complete = e.target.checked; |
44 |
| - ChangeTodoStatusMutation.commit( |
45 |
| - (this.props.relay && this.props.relay.environment) as Environment, |
46 |
| - complete, |
47 |
| - this.props.todo, |
48 |
| - this.props.viewer, |
49 |
| - ); |
50 |
| - }; |
51 |
| - _handleDestroyClick = () => { |
52 |
| - this._removeTodo(); |
53 |
| - }; |
54 |
| - _handleLabelDoubleClick = () => { |
55 |
| - this._setEditMode(true); |
56 |
| - }; |
57 |
| - _handleTextInputCancel = () => { |
58 |
| - this._setEditMode(false); |
59 |
| - }; |
60 |
| - _handleTextInputDelete = () => { |
61 |
| - this._setEditMode(false); |
62 |
| - this._removeTodo(); |
63 |
| - }; |
64 |
| - _handleTextInputSave = (text: string) => { |
65 |
| - this._setEditMode(false); |
66 |
| - RenameTodoMutation.commit( |
67 |
| - (this.props.relay && this.props.relay.environment) as Environment, |
68 |
| - text, |
69 |
| - this.props.todo, |
70 |
| - ); |
71 |
| - }; |
72 |
| - _removeTodo() { |
73 |
| - RemoveTodoMutation.commit( |
74 |
| - (this.props.relay && this.props.relay.environment) as Environment, |
75 |
| - this.props.todo, |
76 |
| - this.props.viewer, |
77 |
| - ); |
| 32 | +const Todo = (props: Props) => { |
| 33 | + const [isEditing, setIsEditing] = React.useState(false) |
| 34 | + |
| 35 | + const environment = useRelayEnvironment() |
| 36 | + |
| 37 | + const todo = useFragment( |
| 38 | + graphql` |
| 39 | + fragment Todo_todo on Todo { |
| 40 | + complete |
| 41 | + id |
| 42 | + text |
| 43 | + } |
| 44 | + `, |
| 45 | + props.todo, |
| 46 | + ) |
| 47 | + |
| 48 | + const viewer = useFragment( |
| 49 | + graphql` |
| 50 | + fragment Todo_viewer on User { |
| 51 | + id |
| 52 | + totalCount |
| 53 | + completedCount |
| 54 | + } |
| 55 | + `, |
| 56 | + props.viewer, |
| 57 | + ) |
| 58 | + |
| 59 | + const handleCompleteChange = (e: ChangeEvent<HTMLInputElement>) => { |
| 60 | + const complete = e.target.checked |
| 61 | + ChangeTodoStatusMutation.commit(environment, complete, todo!, viewer!) |
| 62 | + } |
| 63 | + const handleDestroyClick = () => { |
| 64 | + removeTodo() |
| 65 | + } |
| 66 | + const handleLabelDoubleClick = () => { |
| 67 | + setIsEditing(true) |
| 68 | + } |
| 69 | + const handleTextInputCancel = () => { |
| 70 | + setIsEditing(false) |
78 | 71 | }
|
79 |
| - _setEditMode = (shouldEdit: boolean) => { |
80 |
| - this.setState({isEditing: shouldEdit}); |
81 |
| - }; |
82 |
| - renderTextInput() { |
| 72 | + const handleTextInputDelete = () => { |
| 73 | + setIsEditing(false) |
| 74 | + removeTodo() |
| 75 | + } |
| 76 | + const handleTextInputSave = (text: string) => { |
| 77 | + setIsEditing(false) |
| 78 | + RenameTodoMutation.commit(environment, text, todo!) |
| 79 | + } |
| 80 | + function removeTodo() { |
| 81 | + RemoveTodoMutation.commit(environment, todo!, viewer!) |
| 82 | + } |
| 83 | + |
| 84 | + function renderTextInput() { |
83 | 85 | return (
|
84 | 86 | <TodoTextInput
|
85 | 87 | className="edit"
|
86 | 88 | commitOnBlur={true}
|
87 |
| - initialValue={this.props.todo.text} |
88 |
| - onCancel={this._handleTextInputCancel} |
89 |
| - onDelete={this._handleTextInputDelete} |
90 |
| - onSave={this._handleTextInputSave} |
| 89 | + initialValue={todo!.text} |
| 90 | + onCancel={handleTextInputCancel} |
| 91 | + onDelete={handleTextInputDelete} |
| 92 | + onSave={handleTextInputSave} |
91 | 93 | />
|
92 |
| - ); |
93 |
| - } |
94 |
| - render() { |
95 |
| - return ( |
96 |
| - <li |
97 |
| - className={classnames({ |
98 |
| - completed: this.props.todo.complete, |
99 |
| - editing: this.state.isEditing, |
100 |
| - })}> |
101 |
| - <div className="view"> |
102 |
| - <input |
103 |
| - checked={!!this.props.todo.complete} |
104 |
| - className="toggle" |
105 |
| - onChange={this._handleCompleteChange} |
106 |
| - type="checkbox" |
107 |
| - /> |
108 |
| - <label onDoubleClick={this._handleLabelDoubleClick}> |
109 |
| - {this.props.todo.text} |
110 |
| - </label> |
111 |
| - <button |
112 |
| - className="destroy" |
113 |
| - onClick={this._handleDestroyClick} |
114 |
| - /> |
115 |
| - </div> |
116 |
| - {this.state.isEditing && this.renderTextInput()} |
117 |
| - </li> |
118 |
| - ); |
| 94 | + ) |
119 | 95 | }
|
| 96 | + |
| 97 | + return ( |
| 98 | + <li |
| 99 | + className={classnames({ |
| 100 | + completed: todo!.complete, |
| 101 | + editing: isEditing, |
| 102 | + })} |
| 103 | + > |
| 104 | + <div className="view"> |
| 105 | + <input |
| 106 | + checked={!!todo!.complete} |
| 107 | + className="toggle" |
| 108 | + onChange={handleCompleteChange} |
| 109 | + type="checkbox" |
| 110 | + /> |
| 111 | + <label onDoubleClick={handleLabelDoubleClick}>{todo!.text}</label> |
| 112 | + <button className="destroy" onClick={handleDestroyClick} /> |
| 113 | + </div> |
| 114 | + {isEditing && renderTextInput()} |
| 115 | + </li> |
| 116 | + ) |
120 | 117 | }
|
121 | 118 |
|
122 |
| -export default createFragmentContainer(Todo, { |
123 |
| - todo: graphql` |
124 |
| - fragment Todo_todo on Todo { |
125 |
| - complete, |
126 |
| - id, |
127 |
| - text, |
128 |
| - } |
129 |
| - `, |
130 |
| - viewer: graphql` |
131 |
| - fragment Todo_viewer on User { |
132 |
| - id, |
133 |
| - totalCount, |
134 |
| - completedCount, |
135 |
| - } |
136 |
| - `, |
137 |
| -}); |
| 119 | +export default Todo |
0 commit comments