/
Todo.tsx
195 lines (173 loc) · 5.12 KB
/
Todo.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// @flow
/**
* 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 ChangeTodoStatusMutation from '../mutations/ChangeTodoStatusMutation';
import RemoveTodoMutation from '../mutations/RemoveTodoMutation';
import RenameTodoMutation from '../mutations/RenameTodoMutation';
import TodoTextInput from './TodoTextInput';
import React, {useState, SyntheticEvent} from 'react';
import {useFragment, graphql, useRelayEnvironment} from 'relay-hooks';
import styled, {css} from 'styled-components';
//import type {Todo_todo} from 'relay/Todo_todo.graphql';
//import type {Todo_user} from 'relay/Todo_user.graphql';
type Props = {
todo: any; // Todo_todo,
user: any; // Todo_user,
disabled?: boolean;
};
const DivView = styled.div`
display: ${props => (props.isEditing ? 'none' : 'flex')};
`;
const StyledLi = styled.li`
position: relative;
font-size: 24px;
border-bottom: 1px solid #ededed;
flex: 1;
${props =>
props.isEditing &&
css`
border-bottom: none;
padding: 0;
&:last-child {
margin-bottom: -1px;
}
`}
`;
const ButtonDestroy = styled.button`
display: none;
position: absolute;
top: 0;
right: 10px;
bottom: 0;
width: 40px;
height: 40px;
margin: auto 0;
font-size: 30px;
color: #cc9a9a;
margin-bottom: 11px;
transition: color 0.2s ease-out;
&:after {
content: '×';
}
${StyledLi}:hover & {
color: #af5b5e;
display: block;
}
`;
const InputToggle = styled.input`
text-align: center;
width: 40px;
height: auto;
margin: auto 0;
border: none;
-webkit-appearance: none;
appearance: none;
&:after {
content: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40" viewBox="-10 -18 100 135"><circle cx="50" cy="50" r="50" fill="none" stroke="%23bddad5" stroke-width="3"/></svg>');
}
${props =>
props.checked &&
css`
&:after {
content: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40" viewBox="-10 -18 100 135"><circle cx="50" cy="50" r="50" fill="none" stroke="%23bddad5" stroke-width="3"/><path fill="%235dc2af" d="M72 25L42 71 27 56l-4 4 20 20 34-52z"/></svg>');
}
`}
@media screen and (-webkit-min-device-pixel-ratio: 0) {
background: none;
height: 40px;
}
`;
const StyledLabel = styled.label`
word-break: break-all;
padding: 15px 0px 15px 15px;
display: block;
line-height: 1.2;
transition: color 0.4s;
${props =>
props.completed &&
css`
color: #d9d9d9;
text-decoration: line-through;
`}
`;
const fragmentSpecTodo = graphql`
fragment Todo_todo on Todo {
complete
id
text
}
`;
const fragmentSpecUser = graphql`
fragment Todo_user on User {
id
userId
totalCount
completedCount
}
`;
// TODO MEDIA
export const Todo = (props: Props) => {
const [isEditing, setIsEditing] = useState<boolean>(false);
const {disabled} = props;
const environment = useRelayEnvironment();
const user = useFragment(fragmentSpecUser, props.user);
const todo = useFragment(fragmentSpecTodo, props.todo);
console.log('renderer');
const handleCompleteChange = (e: SyntheticEvent<HTMLInputElement>) => {
const complete = e.currentTarget.checked;
ChangeTodoStatusMutation.commit(environment, complete, todo, user);
};
const handleDestroyClick = () => removeTodo();
const handleLabelDoubleClick = () => {
if (!disabled) setIsEditing(true);
};
const handleTextInputCancel = () => setIsEditing(false);
const handleTextInputDelete = () => {
setIsEditing(false);
removeTodo();
};
const handleTextInputSave = (text: string) => {
setIsEditing(false);
RenameTodoMutation.commit(environment, text, todo);
};
const removeTodo = () => RemoveTodoMutation.commit(environment, todo, user);
return (
<StyledLi isEditing={isEditing}>
<DivView isEditing={isEditing}>
<InputToggle
checked={todo.complete}
onChange={handleCompleteChange}
type="checkbox"
disabled={disabled}
/>
<StyledLabel
completed={todo.complete}
onDoubleClick={handleLabelDoubleClick}>
{todo.text}
</StyledLabel>
{!disabled && <ButtonDestroy onClick={handleDestroyClick} />}
</DivView>
{isEditing && (
<TodoTextInput
edit
isEditing={isEditing}
commitOnBlur={true}
initialValue={todo.text}
onCancel={handleTextInputCancel}
onDelete={handleTextInputDelete}
onSave={handleTextInputSave}
/>
)}
</StyledLi>
);
};
export default Todo;