Skip to content

Commit

Permalink
getFriends etc working as intended probably maybe I think
Browse files Browse the repository at this point in the history
  • Loading branch information
ak373 committed Aug 30, 2021
1 parent 2ccc932 commit e7dd9de
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
17 changes: 9 additions & 8 deletions client/components/Profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ class Profile extends React.Component {

this.setVerified = this.setVerified.bind(this);
this.setRank = this.setRank.bind(this);
this.grabUserAndFriends = this.grabUserAndFriends.bind(this);
}

componentDidMount() {
this.props.getUser(this.props.match.params.id)
this.props.getLevels()
this.props.getFriends(this.props.match.params.id)
this.grabUserAndFriends(this.props.match.params.id);

let completedLevels = 0
if (this.props.user.levels && this.props.user.id) (completedLevels = this.props.user.levels.length)
Expand All @@ -32,9 +32,11 @@ class Profile extends React.Component {
else if (completedLevels <= 9){ this.setRank('Private'); }
else if (completedLevels <= 14){ this.setRank('Specialist'); }
else if (completedLevels <= 16){ this.setRank('Code Cracker'); }
}

this.props.getFriends()

async grabUserAndFriends(userId){
await this.props.getUser(userId);
await this.props.getFriends(this.props.user);
}

setVerified(bool){
Expand All @@ -59,9 +61,8 @@ class Profile extends React.Component {
<div id="profileContainer">
<h1>{this.props.user.username}</h1>
{(this.props.auth.id === this.props.user.id) && (verified ? <EditProfile setVerified={this.setVerified} /> : <VerifyPassword setVerified={this.setVerified} />) }
{this.props.auth.id !== this.props.user.id &&
(<button>Add Friend</button>)}
<h4 style={{display: "flex", flexDirection: "column", justifyContent: "center"}}>
{(this.props.auth.id !== this.props.user.id) && (<button>Add Friend</button>)}
<h4 style={{display: "flex", flexDirection: "column", justifyContent: "center"}}>
<div style={{display: "flex", justifyContent: "center"}}>Next Level:</div>
{this.props.levels[completedLevels] &&
<div id="profile-level-link"><Link to={`/level/${completedLevels+1}`}>
Expand Down Expand Up @@ -100,7 +101,7 @@ const mapState = (state) => ({
const mapDispatch = (dispatch) => ({
getUser: (id) => dispatch(fetchUser(id)),
getLevels: () => dispatch(fetchLevels()),
getFriends: (id) => dispatch(fetchFriends(id))
getFriends: (user) => dispatch(fetchFriends(user))
});

export default connect(mapState, mapDispatch)(Profile);
1 change: 0 additions & 1 deletion client/store/level.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export const nextLevel = (id) => async (dispatch) => {
});
dispatch(setLevel(data));
return data;
//changeCode(data.startingJS);
} catch (levelError) {
console.log('These are not the ducks you are looking for', levelError);
}
Expand Down
15 changes: 9 additions & 6 deletions client/store/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ export const verifyUser = (id, enteredPass) => async () =>{
}
}

export const fetchFriends = (id) => async (dispatch) => {
export const fetchFriends = (user) => async (dispatch) => {
try {
const token = window.localStorage.getItem('token');
const { data } = await axios.get(`/api/users/${id}/friends`, { headers: { authorization: token } });
console.log('friends:', data)
dispatch(setFriends(data));
const token = window.localStorage.getItem('token');
// const user = await axios.get(`/api/users/${id}`, { headers: { authorization: token } });
const budArray = await axios.get(`/api/users/${user.id}/friends`, { headers: { authorization: token } });
console.log('user: ', user)
// user.data.friends = budArray.data;
console.log('friends: ', budArray.data)
dispatch(setFriends(budArray.data));
} catch (e) {
console.log(e)
}
Expand All @@ -70,7 +73,7 @@ export default function( state = {}, action ) {
case SET_USER:
return action.user;
case SET_FRIENDS:
return action.friends
return {...user, friends: action.friends }
default:
return state
}
Expand Down
2 changes: 1 addition & 1 deletion server/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ router.put('/update/:id', requireToken, userIsUser, async (req, res, next) => {

router.get('/:id/friends', requireToken, async(req, res, next) => {
try {
// doesn't work
// doesn't work OR DOES IT???
const friends = await req.user.getFriends()
res.json(friends)
} catch (err) {
Expand Down

0 comments on commit e7dd9de

Please sign in to comment.