-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
179 lines (163 loc) · 4.67 KB
/
index.js
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
import React, { useState } from 'react';
import styled from 'styled-components';
import { EXECUTE_ACTION } from '@nostack/no-stack';
import compose from '@shopify/react-compose';
import { graphql } from '@apollo/react-hoc';
import { UPDATE_PROJECT_FOR_PROJECTS_FOR_USER_ACTION_ID, DELETE_PROJECT_FOR_PROJECTS_FOR_USER_ACTION_ID } from '../../config';
import { PROJECT_FRAGMENT } from '../source-props/fragments';
import Todos from '../Todos';
const ProjectStyleWrapper = styled.div`
margin: 2em 1em;
padding: 1.5em;
border: none;
border-radius: 10px;
box-shadow: 5px 5px 10px #888888;
opacity: ${props => props.isDeleting ? 0.2 : 1.0};
`;
const Row = styled.div`
margin: 1em 0;
`;
const Button = styled.button`
background: none;
border: none;
cursor: pointer;
font-size: 1.25rem;
padding: 0;
color: #bbbbbb;
transition: color 0.5s ease;
&:hover {
color: ${props => props.hoverColor || '#000000'};
}
`;
const DeleteMenu = styled.div`
color: red;
margin: 1em;
padding: 1em;
border: 1px solid #eeeeee;
`;
function Project({ project, currentUserId, updateInstance, deleteInstance, onUpdate, onDelete }) {
const [ projectValue, updateProjectValue ] = useState(project.value);
const [ isEditMode, updateIsEditMode ] = useState(false);
const [ isSaving, updateIsSaving ] = useState(false);
const [ isDeleteMode, updateIsDeleteMode ] = useState(false);
const [ isDeleting, updateIsDeleting ] = useState(false);
function handleProjectValueChange(e) {
updateProjectValue(e.target.value);
}
async function handleProjectValueSave() {
updateIsSaving(true);
await updateInstance({
variables: {
actionId: UPDATE_PROJECT_FOR_PROJECTS_FOR_USER_ACTION_ID,
executionParameters: JSON.stringify({
value: projectValue,
instanceId: project.id,
}),
},
update: onUpdate(project.id, PROJECT_FRAGMENT),
});
updateIsEditMode(false);
updateIsSaving(false);
}
async function handleDelete() {
updateIsDeleting(true);
try {
await deleteInstance({
variables: {
actionId: DELETE_PROJECT_FOR_PROJECTS_FOR_USER_ACTION_ID,
executionParameters: JSON.stringify({
parentInstanceId: currentUserId,
instanceId: project.id,
}),
},
update: onDelete(project.id),
});
} catch (e) {
updateIsDeleting(false);
}
}
return (
<ProjectStyleWrapper isDeleting={isDeleting}>
{isEditMode ?
(
<Row>
<label htmlFor={project.id}>
Project Value:
<input
id={project.id}
type="text"
value={projectValue}
onChange={handleProjectValueChange}
disabled={isSaving}
/>
</label>
<Button
type="button"
hoverColor="#00FF00"
onClick={handleProjectValueSave}
disabled={isSaving}
>
✓
</Button>
<Button
type="button"
hoverColor="#FF0000"
onClick={() => updateIsEditMode(false)}
disabled={isSaving}
>
✕
</Button>
</Row>
) :
(
<h3>
{project.value}
{isDeleteMode ? (
<DeleteMenu>
Delete?
<Button
type="button"
hoverColor="#00FF00"
onClick={handleDelete}
disabled={isDeleting}
>
✓
</Button>
<Button
type="button"
hoverColor="#FF0000"
onClick={() => updateIsDeleteMode(false)}
disabled={isDeleting}
>
✕
</Button>
</DeleteMenu>
):
(
<>
<Button
type="button"
onClick={() => updateIsEditMode(true)}
>
✎
</Button>
<Button
type="button"
onClick={() => updateIsDeleteMode(true)}
>
🗑
</Button>
</>
)
}
</h3>
)
}
<Todos projectId={project.id} />
</ProjectStyleWrapper>
);
}
export default compose(
graphql(EXECUTE_ACTION, { name: 'updateInstance' }),
graphql(EXECUTE_ACTION, { name: 'deleteInstance' })
)(Project);