-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
129 lines (111 loc) · 3.15 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
import React, { useState } from 'react';
import styled from 'styled-components';
import compose from '@shopify/react-compose';
import { graphql } from '@apollo/react-hoc';
import { EXECUTE_ACTION } from '@nostack/no-stack';
import { CREATE_TO_DO_FOR_TO_DO_SOURCE_ACTION_ID, CREATE_IS_COMPLETED_FOR_TO_DO_SOURCE_ACTION_ID } from '../../config';
const Form = styled.div`
margin: 2em;
padding: 1.5em;
border: none;
border-radius: 5px;
background-color: #F5F5F5;
`;
const Button = styled.button`
margin-left: 1em;
`;
function CreateTodoForm({ projectId, createTodo, createIsCompleted, onAdd }) {
const [ todoValue, updateTodoValue ] = useState('');
const [ loading, updateLoading ] = useState(false);
function handleChange(e) {
updateTodoValue(e.target.value);
}
async function handleSubmit(e) {
e.preventDefault();
if (!todoValue) {
return;
}
updateLoading(true);
const createTodoResponse = await createTodo({
variables: {
actionId: CREATE_TO_DO_FOR_TO_DO_SOURCE_ACTION_ID,
executionParameters: JSON.stringify({
parentInstanceId: projectId,
value: todoValue,
}),
unrestricted: false,
},
});
const newTodoData = JSON.parse(createTodoResponse.data.ExecuteAction);
await createIsCompleted({
variables: {
actionId: CREATE_IS_COMPLETED_FOR_TO_DO_SOURCE_ACTION_ID,
executionParameters: JSON.stringify({
parentInstanceId: newTodoData.instanceId,
value: 'false',
}),
unrestricted: false,
},
update: (cache, response) => {
const isCompletedData = JSON.parse(response.data.ExecuteAction);
const newTodo = {
instance: {
id: newTodoData.instanceId,
value: newTodoData.value,
__typename: 'Instance',
},
children: [
{
instances: [
{
instance: {
id: isCompletedData.instanceId,
value: isCompletedData.value,
__typename: 'Instance',
},
__typename: 'InstanceWithTypedChildren',
},
],
__typename: 'TypeWithInstances',
},
],
__typename: 'InstanceWithTypedChildren',
};
onAdd(newTodo)(cache);
},
});
updateTodoValue('');
updateLoading(false);
}
function handleKeyPress(e) {
if (e.charCode === 13) {
handleSubmit(e);
}
}
return (
<Form>
<label htmlFor='todo-name'>
Todo Value:
<input
id='todo-name'
type="text"
onChange={handleChange}
onKeyPress={handleKeyPress}
value={todoValue}
disabled={loading}
/>
</label>
<Button type="submit" disabled={loading} onClick={handleSubmit}>
{
loading
? 'Adding Todo...'
: 'Add Todo'
}
</Button>
</Form>
);
}
export default compose(
graphql(EXECUTE_ACTION, { name: 'createTodo' }),
graphql(EXECUTE_ACTION, { name: 'createIsCompleted' }),
)(CreateTodoForm);