-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHome.js
190 lines (178 loc) · 7.04 KB
/
Home.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
import React, {useEffect, useState} from "react";
import {Chip, Grid, Container, Grow, Paper, Typography, Button, FormControl, Radio, RadioGroup, FormControlLabel} from "@mui/material";
import Table from '@mui/material/Table';
import TableBody from '@mui/material/TableBody';
import TableCell from '@mui/material/TableCell';
import TableContainer from '@mui/material/TableContainer';
import TableHead from '@mui/material/TableHead';
import TableRow from '@mui/material/TableRow';
import { jwtDecode } from "jwt-decode";
import Input from "../Login/Input";
import {styles} from "../Login/styles";
import { useDispatch } from "react-redux";
import * as api from "../../api";
import {WAGER} from "../../constants/actionTypes";
import * as messages from "../../messages";
const formDataInitVal = {
amount_to_wager: 0,
heads_or_tails: "heads",
};
const Home = () => {
const user = localStorage.getItem("profile")
? jwtDecode(JSON.parse(localStorage.getItem("profile")).token)
: "null";
const [formData, setFormData] = useState(formDataInitVal);
// Store wager token state
const [wagerTokens, setWagerTokens] = useState(0);
const [pastTenWagers, setPastTenWagers] = useState([]);
const isSingedIn = user;
const dispatch = useDispatch();
const handleSubmit = async (e) => {
e.preventDefault();
try {
const { data } = await api.wager(formData, user);
setWagerTokens(data.new_balance);
dispatch({ type: WAGER, data });
if (data.wager && data.wager.win) {
messages.success(data.message);
} else {
messages.info(data.message);
}
} catch (error) {
messages.error(error.response.data.message);
}
};
const handleChange = (e) => {
setFormData({ ...formData, [e.target.name]: e.target.value });
};
// Display last 10 wagers
useEffect(() => {
const fetchUserData = async () => {
try {
const { data } = await api.currentUserData();
setWagerTokens(data.wager_tokens);
setPastTenWagers(data.past_wagers);
console.log('data', data);
} catch (error) {
messages.error(error.response.data.message);
}
};
fetchUserData();
}, [wagerTokens]);
return (
<Grow in>
<Container component="main" maxWidth="md" sx={{ paddingTop: "10px", paddingBottom: "30px" }}>
<Paper elevation={3}>
{isSingedIn !== "null" && isSingedIn !== null ? (
<div className="space-y-2">
<Typography variant="h4" align="center" color="primary" sx={{ paddingTop: "30px", paddingBottom: "20px" }}>
{`Play Coin Toss`}
</Typography>
<Typography variant="h6" align="center" color="default" sx={{ paddingTop: "0px", paddingBottom: "30px" }}>
Tokens: <b>{wagerTokens}</b>
</Typography>
<form onSubmit={handleSubmit}>
<Grid container
direction="column"
justifyContent="center"
alignItems="center"
>
<Grid item lg>
<Input
name="amount_to_wager"
label="Wager Amount"
type="number"
min="0.00" max="10000.00" step="0.01"
handleChange={handleChange}
autoFocus
/>
</Grid>
<Grid item lg sx={{ paddingTop: "15px", paddingBottom: "0px" }}>
<FormControl>
<RadioGroup
row
aria-labelledby="demo-row-radio-buttons-group-label"
name="heads_or_tails"
onChange={handleChange}
defaultValue="heads"
>
<FormControlLabel value="heads" control={<Radio />} label="Heads" />
<FormControlLabel value="tails" control={<Radio />} label="Tails" />
</RadioGroup>
</FormControl>
</Grid>
<Grid item sm>
<Button
type="submit"
sx={styles.submit}
fullWidth
variant="contained"
color="primary"
size="large"
>
Play
</Button>
</Grid>
</Grid>
</form>
{/* Table: past 10 coinflip result */}
<TableContainer component={Paper} sx={{ paddingTop: "30px", paddingBottom: "0px" }}>
<Table sx={{ minWidth: 650 }} aria-label="simple table">
<TableHead>
<TableRow>
<TableCell><b>Balance</b></TableCell>
<TableCell align="right"><b>Game Result</b></TableCell>
<TableCell align="right"><b>Amount Wagered</b></TableCell>
<TableCell align="right"><b>Flip Result</b></TableCell>
</TableRow>
</TableHead>
<TableBody>
{pastTenWagers.map((wager) => (
<TableRow
key={wager._id}
sx={{ '&:last-child td, &:last-child th': { border: 0 } }}
>
<TableCell component="th" scope="row">
{wager.balance_after}
</TableCell>
<TableCell align="right">
{wager.multiplier > 2 && (
<b style={{ paddingRight: "10px" }}>
<Chip
size="small"
label={`Bonus x${wager.multiplier}`}
color="info"
variant="outlined"
/>
</b>
)}
<b>
<Chip
size="small"
label={wager.win ? 'Win' : 'Loss'}
color={wager.win ? 'success' : 'error'}
variant="outlined"
/>
</b>
</TableCell>
<TableCell align="right">
{wager.amount_wagered}
</TableCell>
<TableCell align="right" style={{ textTransform: 'capitalize' }}>{wager.coin_flip_result}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</div>
) : (
<Typography variant="h4" align="center" color="primary">
Login to Play
</Typography>
)}
</Paper>
</Container>
</Grow>
);
};
export default Home;