This repository was archived by the owner on May 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathindex.js
More file actions
176 lines (154 loc) · 4.92 KB
/
index.js
File metadata and controls
176 lines (154 loc) · 4.92 KB
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
// @flow
import * as React from 'react';
import { connect } from 'react-redux';
import { getDefaultCategoryId, getCategories } from 'selectors/categories';
import { actions } from 'modules/transactions';
import Form from 'components/Form';
import Field from 'components/Field';
import DataSelector from 'components/DataSelector';
import type { FormData } from 'components/Form';
import styles from './style.module.scss';
type EntryFormRowProps = {
defaultCategoryId: string,
categories: Object,
setEditTransaction: Function,
addTransaction: Function,
deleteTransaction: Function,
updateTransaction: Function,
};
type EntryFormRowState = {
formData: ?FormData,
};
export class EntryFormRow extends React.Component<EntryFormRowProps, EntryFormRowState> {
static formFields = ['id', 'categoryId', 'description', 'value'];
static validateForm = ({ value }) => {
const errors = {};
if (!value) {
errors._error = 'You must provide a value';
}
return errors;
};
state = {
formData: null,
};
handleSubmit = (values): void => {
const { id, categoryId, description, value } = values;
if (!id) {
this.props.addTransaction({ categoryId, description, value });
} else {
this.props.updateTransaction({ id, categoryId, description, value });
this.props.setEditTransaction('');
}
};
handleDelete = (id): void => {
this.props.setEditTransaction('');
this.props.deleteTransaction(id);
};
focusValueField = (): void => {
if (this.valueRef) {
this.valueRef.focus();
}
};
handleSubmitSuccess = (): void => {
const { formData } = this.state;
// keep the chosen category but clear everything else
if (formData) {
formData.initializeForm({
categoryId: formData.fields.categoryId.value,
});
}
this.focusValueField();
};
handleSubmitFail = (): void => {
this.focusValueField();
};
valueRef: ?HTMLElement;
handleValueRefUpdate = (ref: ?HTMLElement) => {
this.valueRef = ref;
};
handleFormDataChange = formData => {
this.setState({
formData: formData,
});
};
render() {
const { categories, defaultCategoryId, transaction, setEditTransaction } = this.props;
const id = transaction ? transaction.id : '';
const initialValues = {
id,
categoryId: transaction ? transaction.categoryId : defaultCategoryId,
description: transaction ? transaction.description : '',
value: transaction ? transaction.value : '',
};
const { formData } = this.state;
const isValid = formData && formData.valid;
return (
<tr className={styles.entryFormRow}>
<td>
<Form
fields={EntryFormRow.formFields}
initialValues={initialValues}
validate={EntryFormRow.validateForm}
onFormDataChange={this.handleFormDataChange}
onSubmit={this.handleSubmit}
onSubmitSuccess={this.handleSubmitSuccess}
onSubmitFail={this.handleSubmitFail}
>
<div className={styles.formSection}>
<Field component={DataSelector} name="categoryId" data={categories} />
</div>
<div className={styles.formSection}>
<Field component="input" name="description" type="text" placeholder="Description" />
</div>
<div className={styles.formSection}>
<Field
component="input"
name="value"
type="number"
placeholder="Value"
handleRef={this.handleValueRefUpdate}
/>
</div>
<div className={styles.formSection}>
<Field component="input" name="id" type="hidden" value={id} />
<button className={`submit ${styles.btn} ${styles.btnGreen}`} type="submit" disabled={!isValid}>
{!id ? 'Add' : 'Update'}
</button>
{id ? (
<>
<button
className={`cancel ${styles.btn} ${styles.btnDefault}`}
type="button"
onClick={() => setEditTransaction('')}
>
Cancel
</button>
<button
className={`delete ${styles.btn} ${styles.btnRed}`}
type="button"
onClick={() => this.handleDelete(id)}
>
Delete
</button>
</>
) : null}
</div>
</Form>
</td>
</tr>
);
}
}
const mapStateToProps = state => ({
defaultCategoryId: getDefaultCategoryId(),
categories: getCategories(state),
});
const mapDispatchToProps = {
addTransaction: actions.addTransaction,
deleteTransaction: actions.deleteTransaction,
updateTransaction: actions.updateTransaction,
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(EntryFormRow);