/
truck.js
182 lines (167 loc) · 6.47 KB
/
truck.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
import React from 'react';
import './css/stock-update-modal.css';
import Modal from "react-modal";
import TruckTable from './truck-table';
import processRequest from "./api";
Modal.setAppElement(document.body);
Modal.defaultStyles.overlay.backgroundColor = 'cornflowerblue';
const REGULAR_STYLES = {
content: {
top: '50%',
left: '50%',
right: 'auto',
bottom: 'auto',
marginRight: '-50%',
transform: 'translate(-50%, -50%)',
backgroundColor: '#001e00',
color: 'yellow',
border: '1px solid yellow',
borderRadius: '7px 7px 7px 7px',
boxShadow: '-7px -7px 17px 7px #001e00',
maxWidth: '800px',
height: '50%'
},
overlay: {
backgroundColor: '#2a3517',
}
};
// const DANGER_STYLES = {
// content: {
// top: '50%',
// left: '50%',
// right: 'auto',
// bottom: 'auto',
// marginRight: '-50%',
// transform: 'translate(-50%, -50%)',
// backgroundColor: 'darkred',
// color: 'white',
// border: '1px solid yellow',
// borderRadius: '7px 7px 7px 7px',
// boxShadow: '-7px -7px 17px 7px black',
// maxWidth: '800px',
// },
// overlay: {
// backgroundColor: 'gold',
// }
// };
/*
Note: Various ways to style the modal:
- css stylesheets
- inline (as above, set in state (modalStyles))
- default styles (also as above, as used for overlay background color).
For more info. see: https://reactcommunity.org/react-modal/examples/css_classes.html
*/
class TruckModal extends React.Component {
constructor(props) {
super(props);
this.state = {
modalStyles: REGULAR_STYLES,
modalIsOpen: this.props.openTruckModal,
};
// Remember! This binding is necessary to make `this` work in the callback
this.handleAfterOpenModal = this.handleAfterOpenModal.bind(this);
}
componentWillMount() {
}
componentWillUnmount() {
console.log('Unmounting truck modal')
}
componentWillReceiveProps(nextProps, nextContext) {
// set modal open/close state (source of truth in parent component - app.js)
this.setState({modalIsOpen: nextProps.openTruckModal});
}
componentDidUpdate(prevProps, prevState, snapshot) {
}
componentDidMount() {
}
handleAfterOpenModal() {
// this.subtitle.style.color = 'yellow'
}
dispatchTruck() {
/*
method to make API request to transfer stock (PATCH request).
*/
// copy truck obj to work on, by converting to string representation, then back (use JSON obj representation)
let truck = JSON.parse(JSON.stringify(this.props.getTruck()));
if (truck.length > 0) {
try {
/* if transferring, delete all superfluous fields from the query to prevent auth issues */
truck.forEach((consignment, index) => {
let cargo = consignment.cargo;
truck[index] = {id: cargo.id, units_to_transfer: cargo.units_to_transfer}
});
// hit the api
const apiRequest = processRequest({
stockRecord: {records: truck},
apiMode: this.props.apiOptions.PATCH_STOCK
});
if (apiRequest) {
apiRequest.then((response) => {
if (response) {
if (response.status === 200) {
this.props.emptyTruck();
// update the main table with the new values
this.props.openTruckModalHandler({actionCancelled: false, returnedRecords: response.data})
}
}
}).catch(error => {
console.log(`API error: ${error}`);
this.props.setMessage({
message: 'Transfer failed! The API rejected the request.',
messageClass: 'alert alert-danger'
});
this.props.openTruckModalHandler({actionCancelled: false})
});
}
} catch (err) {
// allow fall through to return false by default
console.log(`API error: ${err}`);
this.props.openTruckModalHandler({actionCancelled: false})
}
}
}
render() {
if (this.props.stockRecord) {
const dispatchButtonClasses = ['btn', 'btn', 'btn-warning', 'm-2', 'table-btn'];
if (!this.props.getTruck().length > 0) {
dispatchButtonClasses.push('d-none')
}
return (
<Modal
isOpen={this.state.modalIsOpen}
onAfterOpen={this.handleAfterOpenModal}
onRequestClose={this.handleCloseModal}
style={this.state.modalStyles}
closeTimeoutMS={250}
contentLabel="Truck Action"
>
<div className="container">
<div className="row">
<div className="col-sm">
<h2 ref={subtitle => this.subtitle = subtitle}>Truck</h2>
</div>
<TruckTable
truck={this.props.getTruck()}
changeUnits={this.props.changeTruckUnits}
/>
<div className="col-sm modal-button-cell">
<button className={dispatchButtonClasses.join(' ')}
onClick={() => {
this.dispatchTruck();
}}>Request Truck Dispatch!
</button>
<button className={'btn btn btn-outline-light close-button'}
onClick={() => {
this.props.openTruckModalHandler({actionCancelled: true})
}}>Cancel
</button>
</div>
</div>
</div>
</Modal>
);
}
return null;
}
}
export default TruckModal;