-
Notifications
You must be signed in to change notification settings - Fork 0
/
atm-improvements.jsx
248 lines (231 loc) · 8.92 KB
/
atm-improvements.jsx
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
const ATMDeposit = ({onChange, isDeposit, isValid}) => {
const choice = ['Deposit', 'Cash Back'];
console.log(`ATM isDeposit: ${isDeposit}`);
return (
<React.Fragment>
<h3 style={{marginBottom: "5px", marginTop: "7px"}}><i
className="fas fa-money-bill-wave"></i> {choice[Number(!isDeposit)]}</h3>
<input style={{
border: "3px solid #7FFF00",
borderRadius: "7px",
fontWeight: "bold",
color: "#006dad",
paddingRight: "5px",
marginRight: "7px"
}} id="number-input" type="number" width="200" step="20" min="0" max="1000" onChange={onChange}
onKeyPress={preventMe} onPaste={preventMe}/>
<input style={{border: "3px solid #7FFF00", borderRadius: "7px", fontWeight: "bold", color: "#006dad"}}
type="submit" disabled={!isValid} width="200" value="Submit" id="submit-input"/>
</React.Fragment>
);
};
const preventMe = (e) => {
e.preventDefault();
};
const Account = () => {
const defAtmMsg = 'PIGGY Bank ATM';
const [deposit, setDeposit] = React.useState(0);
const [totalState, setTotalState] = React.useState(2700);
const [isDeposit, setIsDeposit] = React.useState(true);
const [atmMsg, setAtmMsg] = React.useState('Welcome to PIGGY Bank');
const [atmMode, setAtmMode] = React.useState('');
const [isValid, setIsValid] = React.useState(false);
const [grossTotalD, setGrossTotalD] = React.useState(0);
const [grossTotalW, setGrossTotalW] = React.useState(0);
const [lightD, setLightD] = React.useState('#FFFFFF00');
const [lightW, setLightW] = React.useState('#FFFFFF00');
let status = `Account Balance $ ${totalState} `;
console.log(`Account Rendered with isDeposit: ${isDeposit}`);
const handleChange = (event) => {
console.log(Number(event.target.value));
if (Number(event.target.value) <= 0) {
return setIsValid(false);
}
if (atmMode === 'Cash Back' && Number(event.target.value) > totalState) {
setIsValid(false);
} else {
setIsValid(true);
}
setDeposit(Number(event.target.value));
};
const handleSubmit = (event) => {
let newTotal = isDeposit ? totalState + deposit : totalState - deposit;
let grossTotal = isDeposit ? grossTotalD + deposit : grossTotalW - deposit;
if (isDeposit) {
setGrossTotalD(grossTotal)
} else {
setGrossTotalW(grossTotal)
}
console.log('Gross Total:' + grossTotal);
console.log('Gross TotalD:' + grossTotalD);
console.log('Gross TotalW:' + grossTotalW);
setTotalState(newTotal);
setIsValid(false);
event.preventDefault();
let msg = isDeposit ? 'Depositing...' : 'Please take your Cash...';
setAtmMsg(msg);
setTimeout(function () {
setAtmMsg('Transaction Complete...');
}, 2000);
setTimeout(function () {
setAtmMsg(defAtmMsg);
}, 4000);
};
const handleModeSelect = (event) => {
console.log(event.target.value);
setAtmMode(event.target.value);
setIsValid(false);
if (event.target.value === 'Deposit') {
setAtmMsg('Enter Cash Below...');
setTimeout(function () {
setAtmMsg('Up to $1000...');
}, 2000);
setLightW('#FFFFFF00');
setLightD('#7FFF00');
setIsDeposit(true);
} else {
setAtmMsg('Preparing to dispense...');
setTimeout(function () {
setAtmMsg('Enter Amount Below...');
}, 2000);
setLightD('#FFFFFF00')
setLightW('#7FFF00');
setIsDeposit(false);
}
setTimeout(function () {
setAtmMsg(defAtmMsg);
}, 4000);
};
const handleQuick = (num) => {
setTotalState(totalState - num);
setAtmMsg('QuickCash: $' + num);
setGrossTotalW(grossTotalW - num)
setTimeout(function () {
setAtmMsg(defAtmMsg);
}, 1000);
// setLightW('#7FFF00');
// setTimeout(function () {
// setLightW('#fFFFFF00');
// }, 1000);
};
const handleLowFunds = () => {
setAtmMsg('Insufficient Funds...');
setTimeout(function () {
setAtmMsg(defAtmMsg);
}, 1000);
};
return (
<React.Fragment>
<div style={{
height: "70px",
position: "absolute",
left: "265px",
top: "355px",
border: "2px dashed #7FFF00",
padding: "5px"
}}>
<label><i
className="fas fa-file-invoice-dollar"></i> Transaction Log</label>
<hr style={{borderTop: "1px dotted #7FFF00", marginTop: "2px"}}/>
<label><i
className="far fa-plus-square"></i> : {grossTotalD}</label><br/>
<label><i
className="far fa-minus-square"></i> : {grossTotalW}</label>
</div>
<div style={{
height: "10px",
width: "142px",
position: "absolute",
left: "342px",
top: "500px",
border: "0",
borderRadius:"15px",
padding: "0px",
backgroundColor: lightD
}} className="blink_me"/>
<div style={{
height: "37px",
width: "321px",
position: "absolute",
left: "96px",
top: "610px",
border: "0",
borderRadius:"15px",
padding: "0px",
backgroundColor: lightW
}} className="blink_me"/>
<button
style={{left: "26px", top: "285px"}}
// onClick={() => totalState >= 20 ? setTotalState(totalState - 20) : ''}
onClick={() => totalState >= 20 ? handleQuick(20) : handleLowFunds()}
>20
</button>
<button
style={{left: "26px", top: "326px"}}
onClick={() => totalState >= 40 ? handleQuick(40) : handleLowFunds()}
>40
</button>
<button
style={{left: "26px", top: "367px"}}
onClick={() => totalState >= 60 ? handleQuick(60) : handleLowFunds()}
>60
</button>
<button
style={{left: "26px", top: "408px"}}
onClick={() => totalState >= 80 ? handleQuick(80) : handleLowFunds()}
>80
</button>
<button
style={{left: "463px", top: "285px"}}
onClick={() => totalState >= 100 ? handleQuick(100) : handleLowFunds()}
>100
</button>
<button
style={{left: "463px", top: "326px"}}
onClick={() => totalState >= 200 ? handleQuick(200) : handleLowFunds()}
>200
</button>
<button
style={{left: "463px", top: "367px"}}
onClick={() => totalState >= 500 ? handleQuick(500) : handleLowFunds()}
>500
</button>
<button
style={{left: "460px", top: "408px", width: "50px"}}
onClick={() => totalState >= 1000 ? handleQuick(1000) : handleLowFunds()}
>1000
</button>
<form onSubmit={handleSubmit}>
<h3 style={{marginLeft: "0px"}}><i className="fas fa-piggy-bank fa-2x"></i> {atmMsg}</h3>
<h3 id="total"><i className="fas fa-chart-bar fa-lg"></i> {status}</h3>
<label>Select or use QuickCash Buttons</label>
<select style={{
border: "3px solid #7FFF00",
borderRadius: "7px",
fontWeight: "bold",
color: "#006dad",
marginTop: "5px"
}}
onChange={(e) => handleModeSelect(e)}
name="mode" id="mode-select">
<option id="no-selection" value="">Transaction</option>
<option id="deposit-selection" value="Deposit">
Deposit
</option>
<option id="cashback-selection" value="Cash Back">
Cash Back
</option>
</select>
{atmMode && (
<ATMDeposit
onChange={handleChange}
isDeposit={isDeposit}
isValid={isValid}
/>
)}
</form>
</React.Fragment>
);
};
// ========================================
ReactDOM.render(<Account/>, document.getElementById('root'));