Skip to content

Commit 6f66bb9

Browse files
committedDec 4, 2018
20-Refactor Exercise
1 parent ee90989 commit 6f66bb9

File tree

9 files changed

+396
-370
lines changed

9 files changed

+396
-370
lines changed
 

‎src/components/Admin/index.js

+3-145
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import React, { Component } from 'react';
2-
import { Switch, Route, Link } from 'react-router-dom';
1+
import React from 'react';
2+
import { Switch, Route } from 'react-router-dom';
33
import { compose } from 'recompose';
44

5-
import { withFirebase } from '../Firebase';
65
import { withAuthorization, withEmailVerification } from '../Session';
6+
import { UserList, UserItem } from '../Users';
77
import * as ROLES from '../../constants/roles';
88
import * as ROUTES from '../../constants/routes';
99

@@ -19,148 +19,6 @@ const AdminPage = () => (
1919
</div>
2020
);
2121

22-
class UserListBase extends Component {
23-
constructor(props) {
24-
super(props);
25-
26-
this.state = {
27-
loading: false,
28-
users: [],
29-
};
30-
}
31-
32-
componentDidMount() {
33-
this.setState({ loading: true });
34-
35-
this.props.firebase.users().on('value', snapshot => {
36-
const usersObject = snapshot.val();
37-
38-
const usersList = Object.keys(usersObject).map(key => ({
39-
...usersObject[key],
40-
uid: key,
41-
}));
42-
43-
this.setState({
44-
users: usersList,
45-
loading: false,
46-
});
47-
});
48-
}
49-
50-
componentWillUnmount() {
51-
this.props.firebase.users().off();
52-
}
53-
54-
render() {
55-
const { users, loading } = this.state;
56-
57-
return (
58-
<div>
59-
<h2>Users</h2>
60-
{loading && <div>Loading ...</div>}
61-
<ul>
62-
{users.map(user => (
63-
<li key={user.uid}>
64-
<span>
65-
<strong>ID:</strong> {user.uid}
66-
</span>
67-
<span>
68-
<strong>E-Mail:</strong> {user.email}
69-
</span>
70-
<span>
71-
<strong>Username:</strong> {user.username}
72-
</span>
73-
<span>
74-
<Link
75-
to={{
76-
pathname: `${ROUTES.ADMIN}/${user.uid}`,
77-
state: { user },
78-
}}
79-
>
80-
Details
81-
</Link>
82-
</span>
83-
</li>
84-
))}
85-
</ul>
86-
</div>
87-
);
88-
}
89-
}
90-
91-
class UserItemBase extends Component {
92-
constructor(props) {
93-
super(props);
94-
95-
this.state = {
96-
loading: false,
97-
user: null,
98-
...props.location.state,
99-
};
100-
}
101-
102-
componentDidMount() {
103-
if (this.state.user) {
104-
return;
105-
}
106-
107-
this.setState({ loading: true });
108-
109-
this.props.firebase
110-
.user(this.props.match.params.id)
111-
.on('value', snapshot => {
112-
this.setState({
113-
user: snapshot.val(),
114-
loading: false,
115-
});
116-
});
117-
}
118-
119-
componentWillUnmount() {
120-
this.props.firebase.user(this.props.match.params.id).off();
121-
}
122-
123-
onSendPasswordResetEmail = () => {
124-
this.props.firebase.doPasswordReset(this.state.user.email);
125-
};
126-
127-
render() {
128-
const { user, loading } = this.state;
129-
130-
return (
131-
<div>
132-
<h2>User ({this.props.match.params.id})</h2>
133-
{loading && <div>Loading ...</div>}
134-
135-
{user && (
136-
<div>
137-
<span>
138-
<strong>ID:</strong> {user.uid}
139-
</span>
140-
<span>
141-
<strong>E-Mail:</strong> {user.email}
142-
</span>
143-
<span>
144-
<strong>Username:</strong> {user.username}
145-
</span>
146-
<span>
147-
<button
148-
type="button"
149-
onClick={this.onSendPasswordResetEmail}
150-
>
151-
Send Password Reset
152-
</button>
153-
</span>
154-
</div>
155-
)}
156-
</div>
157-
);
158-
}
159-
}
160-
161-
const UserList = withFirebase(UserListBase);
162-
const UserItem = withFirebase(UserItemBase);
163-
16422
const condition = authUser =>
16523
authUser && authUser.roles.includes(ROLES.ADMIN);
16624

0 commit comments

Comments
 (0)