-
Notifications
You must be signed in to change notification settings - Fork 0
/
detail.js
220 lines (212 loc) · 6.36 KB
/
detail.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import React, { useState, useEffect } from 'react';
import { push } from 'connected-react-router';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import {
fetchCustomer as fetchCustomerAction,
updateCustomer as updateCustomerAction,
} from '../../modules/customer';
import BootstrapTable from 'react-bootstrap-table-next';
import { toast } from 'react-smart-toaster';
const CustomerDetail = props => {
const {
fetchCustomerAction: fetchCustomer,
updateCustomerAction: updateCustomer,
currentCustomer,
toastMessage,
match,
goBack,
} = props;
const { params } = match;
const [disableAction, updateDisableAction] = useState(false);
const [notes, updateNotes] = useState([]);
const [status, updateStatus] = useState('current');
// local actions
const addNote = () => {
updateNotes(prev => {
const newNote = {
idx: prev.length,
description: '',
};
return prev.concat([newNote]);
});
};
const updateNote = (idx, e) => {
const val = e.target.value;
updateNotes(prev => {
prev[idx].description = val;
return [...prev];
});
};
const deleteNote = idx => {
updateNotes(prev => {
prev[idx].deleted = true;
return [...prev];
});
};
const changeStatus = e => {
updateStatus(e.target.value);
};
const mainColumns = [
{
dataField: 'id',
text: 'ID',
},
{
dataField: 'name',
text: 'Name',
},
{
dataField: 'phone',
text: 'Phone',
},
{
dataField: 'address',
text: 'Address',
},
{
dataField: 'email',
text: 'Email',
},
{
dataField: 'createdAt',
text: 'CreatedAt',
},
{
dataField: '',
text: 'Status',
formatter: (cell, row, rowIndex, extraData) => (
<select className="form-control" value={status} onChange={changeStatus}>
<option value="prospective">prospective</option>
<option value="current">current</option>
<option value="non-active">non-active</option>
</select>
),
formatExtraData: { status, changeStatus },
},
];
const noteColumns = [
{
dataField: 'notes',
text: 'Notes',
formatter: (cell, row, rowIndex, extraData) => {
if (row.deleted) {
return <></>;
} else {
return (
<textarea
rows="1"
cols="50"
className="form-control"
placeholder="write some descriptions here"
value={row.description}
onChange={e => extraData.updateNote(row.idx, e)}
></textarea>
);
}
},
formatExtraData: { updateNote },
},
{
dataField: 'id',
text: 'Delete',
formatter: (cell, row, rowIndex, extraData) => (
<button
disabled={extraData.disableAction}
className="btn btn-dark"
onClick={() => extraData.deleteNote(row.idx)}
>
Delete
</button>
),
formatExtraData: { deleteNote, disableAction },
},
];
const hiddenRowKeys = notes
.map((note, idx) => {
return note.deleted ? idx : null;
})
.filter(idx => idx !== null);
// fetch customer data
useEffect(() => {
fetchCustomer(params.id);
}, [fetchCustomer, params]);
useEffect(() => {
if (toastMessage) {
toast[toastMessage.type](toastMessage.msg);
updateDisableAction(true);
} else {
updateDisableAction(false);
}
}, [toastMessage]);
// pass data to local state
useEffect(() => {
updateNotes(
currentCustomer.CustomerNotes.map((row, idx) => {
row.idx = idx;
return row;
}),
);
}, [currentCustomer.CustomerNotes]);
useEffect(() => {
updateStatus(currentCustomer.status);
}, [currentCustomer.status]);
return (
<div data-testid="customer-detail">
<p>
<button type="button" className="btn btn-info btn-action" onClick={() => goBack()}>
Back to List
</button>
</p>
<BootstrapTable
keyField="id"
data={[currentCustomer]}
columns={mainColumns}
bootstrap4={true}
/>
<BootstrapTable
keyField="idx"
data={notes}
columns={noteColumns}
bootstrap4={true}
hiddenRows={hiddenRowKeys}
/>
<button
type="button"
disabled={disableAction}
className="btn btn-warning btn-action"
onClick={addNote}
>
Add Note
</button>
<div className="row h-100 justify-content-center align-items-center">
<button
type="button"
disabled={disableAction}
className="btn btn-primary btn-save"
onClick={() => updateCustomer(currentCustomer.id, status, notes)}
>
Save
</button>
</div>
</div>
);
};
const mapStateToProps = ({ customer }) => ({
loading: customer.loading,
currentCustomer: customer.currentCustomer,
toastMessage: customer.toastMessage,
});
const mapDispatchToProps = dispatch =>
bindActionCreators(
{
fetchCustomerAction,
updateCustomerAction,
goBack: () => push(`/customers`),
},
dispatch,
);
export default connect(
mapStateToProps,
mapDispatchToProps,
)(CustomerDetail);