Skip to content

Commit a4f86c6

Browse files
committed
Complete Redux usage with User data integration
* withAuthentication, withAuthorization deprecated * removed deprecated code * cleaned up code around user auth areas, profile, navbar * refactored code to hooks
1 parent e4bd0d5 commit a4f86c6

File tree

27 files changed

+358
-418
lines changed

27 files changed

+358
-418
lines changed

src/components/app/App.jsx

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
/* eslint react/prefer-stateless-function: 0 */
22
/* eslint no-undef: 0 */
3-
import React, { Component } from 'react';
3+
import React from 'react';
44
import { Route } from 'react-router-dom';
55
import ReactGA from 'react-ga';
66
import MessengerCustomerChat from 'react-messenger-customer-chat';
77

88
import { ThemeProvider } from 'styled-components';
99

10-
// Font Awesome
1110
import { library } from '@fortawesome/fontawesome-svg-core';
1211
import {
1312
faPlus,
@@ -20,8 +19,6 @@ import {
2019
} from '@fortawesome/free-solid-svg-icons';
2120

2221
import { Container } from 'reactstrap';
23-
import withAuthentication from '../utils/withAuthentication';
24-
2522
import Navbar from '../shared/navbar';
2623
import Footer from '../shared/footer';
2724

@@ -35,6 +32,7 @@ import ExploreProjects from '../explore-projects';
3532
import ForgotPassword from '../forgot-password';
3633

3734
import './App.css';
35+
import useUser from '../../hooks/use-user';
3836

3937
const mainContainerStyle = { marginBottom: '100px', marginTop: '50px' };
4038

@@ -53,32 +51,31 @@ library.add(
5351
faExternalLinkAlt,
5452
);
5553

56-
class App extends Component {
57-
render() {
58-
return (
59-
<ThemeProvider theme={{ mode: 'bubbly' }}>
60-
<div className="App">
61-
<link
62-
href="https://fonts.googleapis.com/css?family=Open+Sans:700&display=swap"
63-
rel="stylesheet"
64-
/>
65-
<Navbar />
66-
<Container style={mainContainerStyle}>
67-
<Route path="/" exact component={Home} />
68-
<Route path="/about" exact component={About} />
69-
<Route path="/projects" exact component={ExploreProjects} />
70-
<Route path="/projects/:shortname" exact component={ProjectPage} />
71-
<Route path="/profiles" exact component={ExploreProfiles} />
72-
<Route path="/profile/:id" exact component={ProfilePage} />
73-
<Route path="/signupin" exact component={SignUpIn} />
74-
<Route path="/signupin/forgotpassword" exact component={ForgotPassword} />
75-
<MessengerCustomerChat pageId="1191043211036808" appId="295451067827152" />
76-
</Container>
77-
<Footer />
78-
</div>
79-
</ThemeProvider>
80-
);
81-
}
82-
}
54+
const App = () => {
55+
useUser(true);
56+
return (
57+
<ThemeProvider theme={{ mode: 'bubbly' }}>
58+
<div className="App">
59+
<link
60+
href="https://fonts.googleapis.com/css?family=Open+Sans:700&display=swap"
61+
rel="stylesheet"
62+
/>
63+
<Navbar />
64+
<Container style={mainContainerStyle}>
65+
<Route path="/" exact component={Home} />
66+
<Route path="/about" exact component={About} />
67+
<Route path="/projects" exact component={ExploreProjects} />
68+
<Route path="/projects/:shortname" exact component={ProjectPage} />
69+
<Route path="/profiles" exact component={ExploreProfiles} />
70+
<Route path="/profile/:id" exact component={ProfilePage} />
71+
<Route path="/signupin" exact component={SignUpIn} />
72+
<Route path="/signupin/forgotpassword" exact component={ForgotPassword} />
73+
<MessengerCustomerChat pageId="1191043211036808" appId="295451067827152" />
74+
</Container>
75+
<Footer />
76+
</div>
77+
</ThemeProvider>
78+
);
79+
};
8380

84-
export default withAuthentication(App);
81+
export default App;

src/components/app/actions/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './project';
22
export * from './tag';
3+
export * from './user';

src/components/app/actions/project.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ import { schema } from '../../../utils';
44
import { db, Project, storage } from '../../../firebase';
55

66
export const createProject = createAction('CREATE_PROJECT', async (values) => {
7-
const response = await Project.create(values);
8-
return response;
7+
const id = await Project.create(values);
8+
return normalize({ id, ...values }, schema.project);
9+
});
10+
11+
export const deleteProject = createAction('DELETE_PROJECT', async (pid) => {
12+
await Project.destroy(pid);
13+
return normalize({ id: pid }, schema.project);
914
});
1015

1116
export const getProjects = createAction('GET_PROJECTS', async () => {

src/components/app/actions/user.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// import { normalize } from 'normalizr';
2+
import { createAction } from 'redux-actions';
3+
// import { schema } from '../../../utils';
4+
import { db, auth } from '../../../firebase';
5+
6+
export const createUser = () => {};
7+
8+
export const setUser = createAction('SET_USER', async (authUser) => {
9+
const user = await db.get('users')(authUser.uid);
10+
return user;
11+
});
12+
13+
export const loginUser = createAction('LOGIN', async ({ email, password }) => {
14+
const response = await auth.signInUser(email, password);
15+
const user = await db.get('users')(response.user.uid);
16+
return user;
17+
});
18+
19+
export const logoutUser = createAction('LOGOUT', auth.signOutUser);

src/components/app/reducers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { combineReducers } from 'redux';
22

33
import projects from './project';
44
import tags from './tag';
5+
import user from './user';
56

67
export default () => {
78
const data = combineReducers({
89
projects,
910
tags,
11+
user,
1012
});
1113

1214
const appReducer = combineReducers({

src/components/app/reducers/project.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { handleActions } from 'redux-actions';
2-
3-
import { createProject, getProjects, getProject, updateProject } from '../actions';
2+
import { omit } from 'lodash';
3+
import { createProject, getProjects, getProject, updateProject, deleteProject } from '../actions';
44

55
const reducer = handleActions(
66
{
@@ -14,6 +14,9 @@ const reducer = handleActions(
1414
};
1515
},
1616
},
17+
[deleteProject]: {
18+
FULFILLED: (state, { payload }) => omit(state, [ payload.result ]),
19+
},
1720
[getProjects]: {
1821
FULFILLED: (state, { payload }) => ({
1922
...state,

src/components/app/reducers/tag.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { handleActions } from 'redux-actions';
22

3-
import { getTags, getTag, updateTags } from '../actions';
3+
import { getTags, getTag } from '../actions';
44

55
const reducer = handleActions(
66
{
@@ -20,16 +20,6 @@ const reducer = handleActions(
2020
};
2121
},
2222
},
23-
// [updateTags]: {
24-
// FULFILLED: (state, { payload }) => {
25-
// const id = payload.result;
26-
// const values = payload.entities.projects[id].tags;
27-
// return {
28-
// ...state,
29-
// [id]: { ...state[id], ...values },
30-
// };
31-
// },
32-
// },
3323
},
3424
{},
3525
);

src/components/app/reducers/user.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { handleActions } from 'redux-actions';
2+
import { setUser, loginUser, logoutUser } from '../actions';
3+
4+
const reducer = handleActions(
5+
{
6+
[setUser]: {
7+
FULFILLED: (state, action) => action.payload,
8+
},
9+
[loginUser]: {
10+
FULFILLED: (state, action) => action.payload,
11+
},
12+
[logoutUser]: {
13+
FULFILLED: () => ({}),
14+
},
15+
},
16+
{},
17+
);
18+
19+
export default reducer;
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
import React, { useState } from 'react';
2+
import { withRouter } from 'react-router-dom';
23

3-
import { Project, db } from '../../firebase';
4-
import { getUserInfo } from '../../firebase/auth';
4+
import { auth } from '../../firebase';
55

6-
import NewProjectModal from './components/new-project-modal/NewProjectModal';
6+
import NewProjectModal from './components/new-project-modal';
77
import Button from '../shared/button';
8-
98
import useModal from '../../hooks/use-modal';
9+
import useProject from '../../hooks/use-project';
1010

1111
const NEW_PROJECT_MODAL_ID = 'NEW_PROJECT_MODAL_ID';
1212

13-
const CreateProject = () => {
13+
const CreateProject = ({ history }) => {
14+
const { createProject } = useProject();
1415
const { closeModal, openModal, isModalOpen } = useModal();
1516
const [ loading, setLoading ] = useState(false);
1617

1718
const handleCreate = name => async () => {
1819
setLoading(true);
1920
// fetch current logged in user
20-
const user = getUserInfo();
21+
const user = auth.getUserInfo();
2122
if (!user) {
2223
setLoading(false);
2324
return;
2425
}
25-
// create the project with one field only for simplicity
26-
const pid = await Project.create({ name });
27-
Project.updateFieldArrayWithId(db.add)('admin')(pid)(user.uid);
28-
Project.updateFieldArrayWithId(db.add)('team')(pid)(user.uid);
29-
// redirect
30-
window.location.replace(`/projects/${ pid }`);
26+
27+
const payload = await createProject({ name, team: [ user.uid ], admin: [ user.uid ] });
28+
if (payload) {
29+
history.replace(`/projects/${ payload.value.result }`, { id: payload.value.result });
30+
}
3131
};
3232

3333
return (
@@ -45,4 +45,4 @@ const CreateProject = () => {
4545
);
4646
};
4747

48-
export default CreateProject;
48+
export default withRouter(CreateProject);

src/components/explore-profiles/ExploreProfiles.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import ReactGA from 'react-ga';
33

44
import { db, auth } from '../../firebase';
55

6-
import ProfileList from './components/profile-list';
6+
// import ProfileList from './components/profile-list';
77
import SignUpModal from '../shared/sign-up-modal';
88
import Filter from '../shared/filter';
99

@@ -45,7 +45,7 @@ class ExploreProfiles extends Component {
4545
};
4646

4747
render() {
48-
const { profiles, modal } = this.state;
48+
const { modal } = this.state;
4949
return (
5050
<div>
5151
<h2 style={{ marginBottom: '20px' }}>Find People</h2>

src/components/home/Home.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import styled from 'styled-components';
44
import { Row, Col } from 'reactstrap';
55
import Banner from './components/banner';
66
import Card from '../shared/card';
7-
87
import useProjects from '../../hooks/use-projects';
98

109
const Header = styled.div`
Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,19 @@
1-
import React, { Component } from 'react';
2-
3-
import AuthUserContext from '../utils/AuthUserContext';
4-
import withAuthorization from '../utils/withAuthorization';
1+
import React from 'react';
2+
import { get } from 'lodash';
53
import Overview from './components/profile-summary';
64
import SocialContentSection from './components/social-content-section';
5+
import useUser from '../../hooks/use-user';
76

8-
class ProfilePage extends Component {
9-
state = { uid: null };
10-
11-
componentDidMount = () => {
12-
const { match } = this.props;
13-
if (match.params.id) {
14-
this.setState({ uid: match.params.id });
15-
}
16-
};
17-
18-
componentDidUpdate = (prevProps) => {
19-
const { match } = this.props;
20-
if (prevProps.match.params.id !== match.params.id) {
21-
this.setState({ uid: match.params.id });
22-
}
23-
};
7+
const ProfilePage = ({ match, location }) => {
8+
const { user } = useUser();
9+
const id = get(match, 'params.id', null);
2410

25-
render() {
26-
const { uid } = this.state;
27-
const { location: { hash } } = this.props;
28-
return (
29-
<AuthUserContext.Consumer>
30-
{authUser => (
31-
<div>
32-
<Overview authUser={authUser} />
33-
<SocialContentSection uid={uid} selected={hash} />
34-
</div>
35-
)}
36-
</AuthUserContext.Consumer>
37-
);
38-
}
39-
}
11+
return (
12+
<div>
13+
<Overview authUser={user} uid={id} />
14+
<SocialContentSection uid={id} authUser={user} selected={location.hash} />
15+
</div>
16+
);
17+
};
4018

41-
const authCondition = authUser => !!authUser;
42-
export default withAuthorization(authCondition)(ProfilePage);
19+
export default ProfilePage;

0 commit comments

Comments
 (0)