/
TodoList.tsx
139 lines (121 loc) · 3.39 KB
/
TodoList.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
// @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 MarkAllTodosMutation from "../mutations/MarkAllTodosMutation";
import Todo from "./Todo";
import React, { SyntheticEvent } from "react";
import { createFragmentContainer, graphql } from "react-relay";
import styled from "styled-components";
import { useFragment, useRelayEnvironment } from "relay-hooks";
/*type Todos = $NonMaybeType<$ElementType<TodoList_user, 'todos'>>;
type Edges = $NonMaybeType<$ElementType<Todos, 'edges'>>;
type Edge = $NonMaybeType<$ElementType<Edges, number>>;
type Node = $NonMaybeType<$ElementType<Edge, 'node'>>;*/
type Props = {
user: any;
};
const StyledSection = styled.section`
position: relative;
z-index: 2;
border-top: 1px solid #e6e6e6;
`;
const StyledList = styled.ul`
margin: 0;
padding: 0;
list-style: none;
`;
const StyledInput = styled.input`
position: absolute;
top: -55px;
left: -12px;
width: 60px;
height: 34px;
text-align: center;
border: none;
@media screen and (-webkit-min-device-pixel-ratio: 0) {
background: none;
-webkit-transform: rotate(90deg);
transform: rotate(90deg);
-webkit-appearance: none;
appearance: none;
}
&:before {
content: "❯";
font-size: 22px;
color: #e6e6e6;
padding: 10px 27px 10px 27px;
}
&:checked:before {
color: #737373;
}
`;
const TodoList_user = graphql`
fragment TodoList_user on User {
todos(
first: 2147483647 # max GraphQLInt
) @connection(key: "TodoList_todos") {
edges {
node {
id
complete
...Todo_todo
}
}
}
id
userId
totalCount
completedCount
...Todo_user
}
`;
const StyledLabelMark = styled.label`
display: none;
`;
const TodoList = (props: Props) => {
console.log("props", props);
const environment = useRelayEnvironment();
const user = useFragment(TodoList_user, props.user);
const { todos, completedCount, totalCount, userId } = user;
const handleMarkAllChange = (e: SyntheticEvent<HTMLInputElement>) => {
const complete = e.currentTarget.checked;
if (todos) {
MarkAllTodosMutation.commit(environment, complete, todos, user);
}
};
const nodes: ReadonlyArray<any> =
todos && todos.edges
? todos.edges
.filter(Boolean)
.map((edge: any) => edge.node)
.filter(Boolean)
: [];
console.log("user", user);
return (
<StyledSection>
<StyledInput
checked={totalCount === completedCount}
onChange={handleMarkAllChange}
type="checkbox"
/>
<StyledLabelMark htmlFor="toggle-all">
Mark all as complete
</StyledLabelMark>
<StyledList>
{nodes.map((node: any) => (
<Todo key={node.id} todo={node} user={user} />
))}
</StyledList>
</StyledSection>
);
};
export default TodoList;