-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphotoShare.jsx
More file actions
180 lines (170 loc) · 5.8 KB
/
photoShare.jsx
File metadata and controls
180 lines (170 loc) · 5.8 KB
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
import React from "react";
import ReactDOM from "react-dom";
import { HashRouter, Route, Switch, Redirect } from "react-router-dom";
import { Grid, Typography, Paper, Snackbar } from "@mui/material";
import axios from "axios";
import TopBar from "./components/topBar/TopBar";
import UserList from "./components/userList/userList";
import UserDetail from "./components/userDetail/userDetail";
import UserPhotos from "./components/userPhotos/userPhotos";
import LoginRegister from "./components/loginRegister/loginRegister";
class PhotoShare extends React.Component {
constructor(props) {
super(props);
this.state = {
labelOnTopBar: undefined,
isLoggedIn: false,
loggedInUserId: undefined,
currentLoggedInUser: undefined,
snackbarOpen: false,
snackbarMessage: '',
};
// Bind necessary methods
this.changeLabelOnTopBar = this.changeLabelOnTopBar.bind(this);
this.changeCurrentLoggedInUser = this.changeCurrentLoggedInUser.bind(this);
this.toggleLogin = this.toggleLogin.bind(this);
this.handleLogout = this.handleLogout.bind(this);
this.handleDeleteAccount = this.handleDeleteAccount.bind(this);
this.handleSnackbarClose = this.handleSnackbarClose.bind(this);
}
changeLabelOnTopBar(label) {
this.setState({ labelOnTopBar: label });
}
changeCurrentLoggedInUser(username, loggedInUserId) {
this.setState({
currentLoggedInUser: username,
loggedInUserId: loggedInUserId,
});
}
toggleLogin(isLoggedIn) {
this.setState({ isLoggedIn });
}
handleLogout() {
axios
.post("/admin/logout")
.then(() => {
console.log("Logout successful");
this.setState({
isLoggedIn: false,
loggedInUserId: undefined,
currentLoggedInUser: undefined,
snackbarOpen: true,
snackbarMessage: "Logout successful",
});
})
.catch((error) => {
console.error("Logout error:", error);
this.setState({
snackbarOpen: true,
snackbarMessage: "Logout failed",
});
});
}
handleDeleteAccount() {
console.log("Delete account button is clicked!");
axios.delete("/deleteaccount")
.then(() => {
console.log("Account Deleted Successfully!");
this.setState({
isLoggedIn: false,
loggedInUserId: undefined,
currentLoggedInUser: undefined,
snackbarOpen: true,
snackbarMessage: "Account Deleted Successfully!",
});
}).catch((error) => {
console.log("Unable to delete account", error);
this.setState({
snackbarOpen: true,
snackbarMessage: "Account Deletion failed",
});
});
}
handleSnackbarClose() {
this.setState({ snackbarOpen: false });
}
render() {
return (
<HashRouter>
<div>
<Grid container spacing={8}>
<Grid item xs={12}>
<TopBar
currentpageLabelOnTopBar={this.state.labelOnTopBar}
currentLoggedInUser={this.state.currentLoggedInUser}
handleLogout={this.handleLogout}
handleDeleteAccount={this.handleDeleteAccount}
/>
</Grid>
<div className="main-topbar-buffer" />
{this.state.isLoggedIn && (
<Grid item sm={3}>
<Paper className="main-grid-item">
<UserList />
</Paper>
</Grid>
)}
<Grid item sm={this.state.isLoggedIn ? 9 : 12}>
<Paper className="main-grid-item">
<Switch>
<Route exact path="/">
{this.state.isLoggedIn ? (
<Typography variant="body1">
Welcome to your photosharing app!
</Typography>
) : (
<Redirect to="/admin/login" />
)}
</Route>
<Route
path="/users/:userId"
render={(props) => (this.state.isLoggedIn ? (
<UserDetail
{...props}
labelOnTopBar={this.changeLabelOnTopBar}
/>
) : (
<Redirect to="/admin/login" />
))}
/>
<Route
path="/photos/:userId"
render={(props) => (this.state.isLoggedIn ? (
<UserPhotos
{...props}
labelOnTopBar={this.changeLabelOnTopBar}
loggedInUserId={this.state.loggedInUserId}
/>
) : (
<Redirect to="/admin/login" />
))}
/>
<Route
path="/admin/login"
render={(props) => (this.state.isLoggedIn ? (
<Redirect to="/" />
) : (
<LoginRegister
{...props}
toggleLogin={this.toggleLogin}
changeCurrentLoggedInUser={this.changeCurrentLoggedInUser}
handleLogout={this.handleLogout}
/>
))}
/>
</Switch>
</Paper>
</Grid>
</Grid>
<Snackbar
open={this.state.snackbarOpen}
autoHideDuration={6000}
onClose={this.handleSnackbarClose}
message={this.state.snackbarMessage}
/>
</div>
</HashRouter>
);
}
}
ReactDOM.render(<PhotoShare />, document.getElementById("photoshareapp"));