-
Notifications
You must be signed in to change notification settings - Fork 0
/
topbar.js
284 lines (257 loc) · 7.72 KB
/
topbar.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import React, { Fragment } from 'react';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import IconButton from '@material-ui/core/IconButton';
import MenuIcon from '@material-ui/icons/Menu';
import Typography from '@material-ui/core/Typography';
import clsx from 'clsx';
import { withStyles } from "@material-ui/core/styles";
import { AccountCircle } from '@material-ui/icons';
import Menu from '@material-ui/core/Menu';
import { Grid, MenuItem } from '@material-ui/core';
import { bindActionCreators, compose } from "redux";
import * as usersAction from "../../redux/actions/userAuthActions";
import LogoutButton from "../auth/logoutButton";
import { withRouter } from 'react-router-dom';
import { connect } from "react-redux";
import CustomModal from "../common/customModal";
import CustomTextField from "../common/customTextField";
import CustomButton from "../common/customButton";
import CustomSpinner from "../common/customSpinner";
const drawerWidth = 240;
const useStyles = theme => ({
spinnerPaper: {
backgroundColor: "transparent",
padding: theme.spacing(0, 0, 0),
border: 'none'
},
toolbar: {
paddingRight: 24,
},
appBar: {
zIndex: theme.zIndex.drawer + 1,
transition: theme.transitions.create(['width', 'margin'], {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen,
}),
},
appBarShift: {
marginLeft: drawerWidth,
width: `calc(100% - ${drawerWidth}px)`,
transition: theme.transitions.create(['width', 'margin'], {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.enteringScreen,
}),
},
menuButton: {
marginRight: 36,
},
menuButtonHidden: {
display: 'none',
},
title: {
flexGrow: 1,
}
});
export class TopBar extends React.Component {
constructor(props) {
super(props);
this.state = {
isloading: false,
anchorEl: null,
modalPassword: false,
newPassword: "",
validations: {
"newPassword": {
validation: () => { return this.state.newPassword.trim() === ""},
errorMessage: "Required"
}
}
};
}
validaData = () => {
var temp = {...this.state.validations};
var flag = false;
Object.keys(temp).forEach(function(key,index) {
if (temp[key].validation()) {
flag = true;
return;
}
});
return flag;
}
handleChange = (e) =>{
const {id, value} = e.target;
this.setState({
[id]: value
})
}
handleMenu = (event) => {
this.setState({
anchorEl: event.currentTarget
})
};
handleClose = () => {
this.setState({
anchorEl: false
})
};
handleModalPassword = () => {
this.setState({
anchorEl: false,
modalPassword: true
})
}
handleCloseModal = () => {
this.setState({
modalPassword: false
})
}
handlePassword = async() => {
this.setState({
isloading: true
})
var model = {
id: this.props.userAuth.id,
email: this.props.userAuth.email,
password: this.state.newPassword
}
await this.props.actions.ChangePassword(model)
.then((result) => {
alert("The password has been changed");
this.handleCloseModal();
})
.catch((error) => {
alert(error)
})
.finally(() => {
this.setState({
isloading: false,
});
});
}
handleProfile = () => {
this.setState({
anchorEl: false
})
this.props.history.push(`/profile`)
}
renderModalPassword = () => {
const { handleChange, handlePassword, validaData } = this;
const { newPassword } = this.state;
return(
<Grid container direction="column" style={{minWidth:264}}>
<Grid item xs={12} style={{padding:"24px"}}>
Type your new password
</Grid>
<Grid item xs={12}>
<CustomTextField
id={"newPassword"}
value={newPassword}
label={"password"}
isNumber={"password"}
handleChange={handleChange}
/>
</Grid>
<Grid item xs={12} style={{padding:"24px"}}>
<CustomButton
disabled={validaData()}
color="primary"
handleClick={handlePassword}
content={"Accept"}
fullWidth={true}
/>
</Grid>
</Grid>
);
}
render() {
const { classes, open, handleDrawerOpen } = this.props;
const {handleMenu, handleClose, handleModalPassword, renderModalPassword, handleCloseModal, handleProfile} = this;
const {anchorEl, modalPassword } = this.state;
return (
<Fragment>
{this.props.error &&
<div>Oops... {this.props.error.message}</div>
}
{this.props.isLoading &&
<div>loading...</div>
}
<AppBar position = "absolute" className={clsx(classes.appBar, open && classes.appBarShift)}>
<Toolbar className={classes.toolbar}>
<IconButton
edge="start"
color="inherit"
aria-label="open drawer"
onClick={handleDrawerOpen}
className={clsx(classes.menuButton, open && classes.menuButtonHidden)}
>
<MenuIcon />
</IconButton>
<Typography onClick={() => this.props.history.push("/")} style={{cursor: "pointer"}} component="h6" variant="h6" noWrap className={classes.title}>
Home
</Typography>
<IconButton
aria-label="account of current user"
aria-controls="menu-appbar"
aria-haspopup="true"
onClick = {handleMenu}
color="inherit"
>
<AccountCircle />
</IconButton>
<Menu
id="menu-appbar"
anchorEl={anchorEl}
anchorOrigin={{
vertical: 'top',
horizontal: 'right',
}}
keepMounted
transformOrigin={{
vertical: 'top',
horizontal: 'right',
}}
open={Boolean(anchorEl)}
onClose={handleClose}
>
{this.props.userAuth.id &&
<div>
<MenuItem onClick={handleProfile}>Profile</MenuItem>
<MenuItem onClick={handleClose}>My account</MenuItem>
<MenuItem onClick={handleModalPassword}>Change Password</MenuItem>
<MenuItem><LogoutButton></LogoutButton></MenuItem>
</div>
}
</Menu>
</Toolbar>
</AppBar>
<CustomModal
modal={modalPassword}
item={renderModalPassword()}
handleCloseModal={handleCloseModal}
/>
{this.state.isloading &&
<CustomSpinner open={true} paperClass={classes.spinnerPaper} />
}
</Fragment>
)
}
}
const mapStateToProps = (state) => {
return {
userAuth: state.userAuth
};
};
const mapDispatchToProps = (dispatch) => {
return {
actions: {
ChangePassword: bindActionCreators(usersAction.ChangePassword, dispatch)
}
};
};
export default compose(
withRouter,
withStyles(useStyles, { withTheme: true }),
connect(mapStateToProps, mapDispatchToProps)
)(TopBar);