Skip to content

Commit b6d2bb3

Browse files
1) displaying of the required profile 2) added some bugs
1 parent 05eea56 commit b6d2bb3

15 files changed

+155
-70
lines changed

api/handleUser.js

+41-20
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,47 @@ var createToken = require('../lib/createToken');
77

88
//handle signin
99
router.post('/signin', (req, res, next) => {
10-
let query = 'SELECT * from Users where user_name = \'' + req.body.userName + '\' AND hash = \'' + req.body.password + '\';';
11-
console.log('signin request recieved from ', req.body)
12-
connection.query(query, function (error, results, fields) {
13-
if (error) throw error;
14-
// if user exists send data
15-
if (results.length > 0) {
16-
//create jwt token
17-
var token = createToken(req.body.userName, req.body.password);
18-
res.send({
19-
auth: 'True',
20-
userData: results[0],
21-
jwt: token
22-
});
23-
} else
24-
res.send({
25-
auth: 'False',
26-
message: 'Please enter correct user name and password',
27-
userData: []
28-
});
29-
});
10+
let query = 'SELECT * from Users where user_name = \'' + req.body.userName + '\' AND hash = \'' + req.body.password + '\';';
11+
console.log('signin request recieved from ', req.body)
12+
connection.query(query, function (error, results, fields) {
13+
if (error) throw error;
14+
// if user exists send data
15+
if (results.length > 0) {
16+
//create jwt token
17+
var token = createToken(req.body.userName, req.body.password);
18+
res.send({
19+
auth: 'True',
20+
userData: results[0],
21+
jwt: token
22+
});
23+
} else
24+
res.send({
25+
auth: 'False',
26+
message: 'Please enter correct user name and password',
27+
userData: []
28+
});
3029
});
30+
});
31+
32+
//handle getuser
33+
router.post('/getuserprofile', (req, res, next) => {
34+
let query = "SELECT * from Users where user_name = '" + req.body.profileName + "';";
35+
console.log('profile request recieved from ', req.body)
36+
connection.query(query, function (error, results, fields) {
37+
if (error) throw error;
38+
// if user exists send data
39+
console.log(results,query, req.body);
40+
if (results.length > 0) {
41+
res.status(200).send({
42+
profileData: results[0],
43+
message: 'profile found'
44+
});
45+
} else
46+
res.status(404).send({
47+
message: 'profile not found',
48+
profileData: []
49+
});
50+
});
51+
});
3152

3253
module.exports = router;

api/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
var express = require('express');
22
var router = express.Router();
3-
var verifyToken = require('../lib/verifyToken');
43
var posts = require('./posts.js');
54
var handleUser = require('./handleUser');
6-
var connection = require('../lib/db');
75

86

97
router.use("/posts", posts);
10-
router.use('/handleUser', handleUser);
8+
router.use('/handleuser', handleUser);
119

1210

1311
module.exports = router;

api/posts.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ router.post('/writePost', verifyToken, (req, res, next) => {
4141

4242
//get all the posts by the user
4343
router.post('/getAllUserPosts', verifyToken, function(req, res, next){
44-
let query = "SELECT user_name,pid,cid,title,description,time FROM posts where user_name = '" + req.userName + "';";
44+
let query = "SELECT user_name,pid,cid,title,description,time FROM posts where user_name = '" + req.body.profileName + "';";
4545
connection.query(query, function (error, results, fields) {
4646
if (error) {
4747
res.status(500).send({
@@ -61,7 +61,7 @@ router.post('/getAllUserPosts', verifyToken, function(req, res, next){
6161
//get recent posts for home page
6262

6363
router.post('/getRecentPosts', verifyToken, function(req, res, next){
64-
let query = "SELECT user_name,pid,cid,title,description,time FROM posts ;";
64+
let query = "SELECT user_name,pid,cid,title,description,time FROM posts ORDER BY time DESC;";
6565
connection.query(query, function (error, results, fields) {
6666
if (error) {
6767
res.status(500).send({

blog/src/App.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class App extends Component {
3939
<Router>
4040
<div className="App">
4141
<PrivateRoute exact path="/" auth={this.props.auth} component={Profile} />
42-
<PrivateRoute exact path="/profile" auth={this.props.auth} component={Profile} />
42+
<PrivateRoute path="/profile" auth={this.props.auth} component={Profile} />
4343
<PrivateRoute exact path="/home" auth={this.props.auth} component={Home} />
4444
<PrivateRoute exact path="/writepost" auth={this.props.auth} component={WritePost} />
4545
<PrivateRoute path = "/displayPost" auth = {this.props.auth} component = {DisplayPostContent} />

blog/src/actions/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default function signin(data) {
99
return (dispatch) => {
1010
axios({
1111
method: 'post',
12-
url: SERVER_URL + '/handleUser/signin',
12+
url: SERVER_URL + '/handleuser/signin',
1313
data: loginData,
1414
config: {
1515
headers: {

blog/src/actions/postActions.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function getAllUserPosts(userData) {
1919
}
2020
})
2121
.then(function (response) {
22-
console.log(response);
22+
console.log(response.data);
2323
if (response.status == 200) {
2424
let payload = { userPosts: response.data.posts };
2525
dispatch({ type: 'GET_ALL_USER_POSTS_SUCCESS', payload: payload });

blog/src/actions/profileActions.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import axios from 'axios';
2+
const SERVER_URL = 'http://localhost:3001';
3+
4+
5+
export function getProfile(data) {
6+
return (dispatch) => {
7+
axios({
8+
method: 'post',
9+
url: SERVER_URL + '/handleuser/getuserprofile',
10+
data: data,
11+
config: {
12+
headers: {
13+
'Content-Type': 'application/json',
14+
'x-access-token': data.jwt,
15+
}
16+
}
17+
})
18+
.then(function (response) {
19+
20+
if (response.status == 200)
21+
dispatch({ type: 'GET_PROFILE_SUCCESS', payload: response.data.profileData });
22+
else
23+
dispatch({ type: 'GET_PROFILE_FAILURE', payload: response.data.profileData });
24+
})
25+
.catch(function (response) {
26+
dispatch({ type: 'GET_PROFILE_FAILURE', payload: response.profileData });
27+
});
28+
}
29+
}

blog/src/components/DisplayPost/Post.js

+14-13
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,19 @@ function Posts(props) {
6666
return (
6767
<div className="post">
6868
<Card className={classes.card}>
69-
<CardHeader
70-
avatar={
71-
<Avatar aria-label="Recipe" className={classes.avatar}>
72-
{props.content.user_name[0]}
73-
</Avatar>
74-
}
75-
action={deleteBtn}
76-
title={props.content.user_name}
77-
subheader={props.content.time}
78-
className="cardHeader"
79-
/>
80-
69+
<Link to={"/profile/" + props.content.user_name}>
70+
<CardHeader
71+
avatar={
72+
<Avatar aria-label="Recipe" className={classes.avatar}>
73+
{props.content.user_name[0]}
74+
</Avatar>
75+
}
76+
action={deleteBtn}
77+
title={props.content.user_name}
78+
subheader={props.content.time}
79+
className="cardHeader"
80+
/>
81+
</Link>
8182
<CardActionArea>
8283
<Link to={"/displayPost/" + props.pid}>
8384
<CardMedia
@@ -89,7 +90,7 @@ function Posts(props) {
8990
title="Contemplative Reptile"
9091
/>
9192
<CardContent>
92-
<Typography gutterBottom variant="title" component="h1" className = "title">
93+
<Typography gutterBottom variant="title" component="h1" className="title">
9394
{props.content.title}
9495
</Typography>
9596
<Typography variant="subheading">

blog/src/components/DisplayPost/Wrapper.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ class Wrapper extends Component {
1616

1717
componentWillMount() {
1818
if (this.props.type == "user") {
19-
let data = { userName: this.props.name, jwt: this.props.jwt };
20-
console.log("userPosts")
19+
let data = { profileName: this.props.name, jwt: this.props.jwt };
2120
this.props.getAllUserPosts(data);
2221
}
2322
else {
@@ -28,6 +27,7 @@ class Wrapper extends Component {
2827

2928

3029
componentWillReceiveProps(nextProps) {
30+
console.log("next props in wrapper", nextProps)
3131
this.setState({ loading: false, userPosts: nextProps.userPosts, recentPosts: nextProps.recentPosts });
3232
}
3333

blog/src/components/Profile/profile.js

+46-6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,47 @@ import './profile.css'
55
import { connect } from 'react-redux';
66
import Navbar from '../Navbar/Navbar';
77
import PostWrapper from '../DisplayPost/Wrapper';
8+
import { getProfile } from '../../actions/profileActions';
9+
import { bindActionCreators } from 'redux';
10+
import CircularProgress from '@material-ui/core/CircularProgress';
11+
812
class Profile extends Component {
913
constructor(props) {
1014
super(props);
15+
this.state = { loading: true, profileData: [], owner: "true" };
16+
}
17+
18+
componentWillMount() {
19+
var profileName = this.props.location.pathname.split('/').pop();
20+
console.log("mounting", profileName)
21+
if (profileName == 'profile')
22+
profileName = this.props.userData.user_name;
23+
24+
if (profileName == this.props.userData.user_name) {
25+
this.setState(this.setState({ profileData: this.props.userData, loading: false }))
26+
}
27+
else {
28+
let data = { profileName: profileName, jwt: this.props.jwt };
29+
this.props.getProfile(data);
30+
}
31+
}
32+
33+
34+
componentWillReceiveProps(nextProps) {
35+
if (this.props.location.pathname.split('/').pop() != 'profile')
36+
this.setState({ loading: false, profileData: nextProps.currentProfile });
37+
else
38+
this.setState({ loading: false, profileData: nextProps.userData });
1139
}
40+
1241
render() {
42+
if (this.state.loading) {
43+
return (
44+
<div>
45+
<CircularProgress size={200} />
46+
</div>
47+
);
48+
}
1349
return (
1450
<React.Fragment>
1551
<Navbar />
@@ -20,25 +56,29 @@ class Profile extends Component {
2056
<img src={require('../../images/superman2.jpg')} width="200px" alt="profilepic" />
2157
</div>
2258
<div className="name">
23-
<Typography variant="title" component="h1">{this.props.userData.fname + " " + this.props.userData.lname}</Typography>
24-
<Typography variant="subheading">{this.props.userData.details}</Typography>
59+
<Typography variant="title" component="h1">{this.state.profileData.fname + " " + this.state.profileData.lname}</Typography>
60+
<Typography variant="subheading">{this.state.profileData.details}</Typography>
2561
</div>
2662
</Paper>
2763
{/* <Typography variant="title" component="h1" className = "title descriptionHelper" >
2864
Posts by the user
2965
</Typography> */}
3066
<div>
31-
<PostWrapper type="user" name={this.props.userData.user_name} owner="true" />
67+
<PostWrapper type="user" name={this.state.profileData.user_name} owner={this.state.owner} />
3268
</div>
3369
</React.Fragment>
3470
)
3571
}
3672
}
3773

38-
function mapStateToProps(store) {
39-
return { userData: store.userData.userData };
74+
function mapDispatchToProps(dispatch) {
75+
return bindActionCreators({ getProfile }, dispatch);
4076
}
4177

4278

79+
function mapStateToProps(store) {
80+
console.log(store.currentProfile);
81+
return { userData: store.userData.userData, currentProfile: store.currentProfile, jwt: store.userData.jwt };
82+
}
4383

44-
export default connect(mapStateToProps)(Profile);
84+
export default connect(mapStateToProps, mapDispatchToProps)(Profile);
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const initialState = { currentProfile : []};
2+
3+
export default function (state = initialState, action) {
4+
switch (action.type) {
5+
case 'GET_PROFILE_SUCCESS':
6+
return action.payload;
7+
case 'GET_PROFILE_FAILURE':
8+
return state;
9+
default:
10+
return state;
11+
}
12+
}

blog/src/reducers/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import userData from './SigninReducer';
33
import posts from './postsReducer';
44
import currentPost from './currentPostReducer';
55
import recentPosts from './recentPostsReducer';
6+
import currentProfile from './currentProfileReducer';
67
export default combineReducers({
78
userData,
89
posts,
910
currentPost,
10-
recentPosts
11+
recentPosts,
12+
currentProfile
1113
});

package-lock.json

-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
"jsonwebtoken": "^8.3.0",
2222
"mysql": "^2.16.0",
2323
"react-redux": "^5.0.7",
24-
"react-responsive-carousel": "^3.1.43",
2524
"recompose": "^0.30.0",
2625
"redux": "^4.0.0",
2726
"redux-thunk": "^2.3.0"

server.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ app.use(cors());
88
app.use(bodyParser.json());
99
app.use(bodyParser.urlencoded({
1010
extended: true
11-
}));
11+
}));
12+
1213

1314
app.use("/", api);
1415

0 commit comments

Comments
 (0)