Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rating frontend #101

Merged
merged 7 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gamersnet_backend/persistence/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ async function tokenValid(cookie) {
return result.toArray();
}

async function getUserIDFromToken(token) {
async function getTokenDocument(token) {
await connect();

let result = await tokens.findOne({token: token});

return result;
}

module.exports = {addUserToken, TOKEN_LIFE_SPAN, updateUserToken, tokenValid, getUserIDFromToken};
module.exports = {addUserToken, TOKEN_LIFE_SPAN, updateUserToken, tokenValid, getTokenDocument};
15 changes: 0 additions & 15 deletions gamersnet_backend/registerTimer.js

This file was deleted.

4 changes: 2 additions & 2 deletions gamersnet_backend/routes/messages/addMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let {addMessage} = require('../../persistence/messages');
let {verifyUserLoggedIn} = require('../utilities/tokenUtility')

let {getUserIDFromToken} = require('../../persistence/tokens.js')
let {getTokenDocument} = require('../../persistence/tokens.js')

async function newMessage(request, response) {

Expand All @@ -24,7 +24,7 @@ async function newMessage(request, response) {
}

if (loggedIn && message) {
let tokenDocument = await getUserIDFromToken(cookie);
let tokenDocument = await getTokenDocument(cookie);

let sender = tokenDocument.userID;

Expand Down
4 changes: 2 additions & 2 deletions gamersnet_backend/routes/messages/getInteractions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let {getInteractions} = require('../../persistence/messages');

let {verifyUserLoggedIn} = require('../utilities/tokenUtility');

let {getUserIDFromToken} = require('../../persistence/tokens.js');
let {getTokenDocument} = require('../../persistence/tokens.js');
const { getUserByID } = require('../../persistence/users');

async function listInteractedIDs(request, response) {
Expand All @@ -15,7 +15,7 @@ async function listInteractedIDs(request, response) {
cookie = cookie.split('=')[1];
loggedIn = await verifyUserLoggedIn(cookie);
}
let tokenDocument = await getUserIDFromToken(cookie);
let tokenDocument = await getTokenDocument(cookie);
//console.log("[listInteractedIDs.tokenDocument] ",tokenDocument); //for debugging

//get logged in user id from token
Expand Down
4 changes: 2 additions & 2 deletions gamersnet_backend/routes/posts/createPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let {addPost} = require('../../persistence/posts');
let {verifyUserLoggedIn} = require('../utilities/tokenUtility')

let {getUserIDFromToken} = require('../../persistence/tokens.js')
let {getTokenDocument} = require('../../persistence/tokens.js')

// this function handles the /post/createPost/ endpoint
async function createPost(request, response) {
Expand All @@ -21,7 +21,7 @@ async function createPost(request, response) {
}

if (loggedIn && verifyBody) {
let tokenDocument = await getUserIDFromToken(cookie);
let tokenDocument = await getTokenDocument(cookie);

//input type are verified here
let userID = tokenDocument.userID;
Expand Down
4 changes: 2 additions & 2 deletions gamersnet_backend/routes/posts/deletePost.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let {deletePost} = require('../../persistence/posts');
let {verifyUserLoggedIn} = require('../utilities/tokenUtility')

let {getUserIDFromToken} = require('../../persistence/tokens.js')
let {getTokenDocument} = require('../../persistence/tokens.js')

// this function handles the /posts/deletePost endpoint
async function removePost(request, response) {
Expand All @@ -19,7 +19,7 @@ async function removePost(request, response) {
}

if (loggedIn) {
let tokenDocument = await getUserIDFromToken(cookie);
let tokenDocument = await getTokenDocument(cookie);

//input type are verified here
let userID = tokenDocument.userID;
Expand Down
4 changes: 2 additions & 2 deletions gamersnet_backend/routes/posts/getPosts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let {getUserPosts, getPost, getAllPosts, getValidPosts} = require('../../persist

let {verifyUserLoggedIn} = require('../utilities/tokenUtility')

let {getUserIDFromToken} = require('../../persistence/tokens.js')
let {getTokenDocument} = require('../../persistence/tokens.js')

// this function handles the /posts/listUserPosts/ endpoint
async function listUserPosts(request, response) {
Expand All @@ -21,7 +21,7 @@ async function listUserPosts(request, response) {
}

if (loggedIn){
let tokenDocument = await getUserIDFromToken(cookie);
let tokenDocument = await getTokenDocument(cookie);

// Note: when fetching users' posts, an empty string is passed in URL if it is requested by the user that's logged in
// As lucas already has userID in the frontend, this will be changed after that pr is merged
Expand Down
4 changes: 2 additions & 2 deletions gamersnet_backend/routes/posts/updatePost.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// include our function from the database to add post
let {updatePostDB, getPost} = require('../../persistence/posts');
let {verifyUserLoggedIn} = require('../utilities/tokenUtility')
let {getUserIDFromToken} = require('../../persistence/tokens.js')
let {getTokenDocument} = require('../../persistence/tokens.js')


/**
Expand Down Expand Up @@ -50,7 +50,7 @@ async function updatePost(request, response) {
}

if (loggedIn) {
let tokenDocument = await getUserIDFromToken(cookie);
let tokenDocument = await getTokenDocument(cookie);

//get logged in user id from token
let loggedUserID = tokenDocument.userID;
Expand Down
7 changes: 3 additions & 4 deletions gamersnet_backend/routes/ratings/addRatings.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let {addRatingsDB} = require('../../persistence/ratings');
let {verifyUserLoggedIn} = require('../utilities/tokenUtility')

let {getUserIDFromToken} = require('../../persistence/tokens.js')
let {getTokenDocument} = require('../../persistence/tokens.js')
const { ObjectID } = require('bson');

// this function handles the /ratings/addRatings/ endpoint
Expand All @@ -13,8 +13,7 @@ async function addRatings(request, response) {
let body = request.body;
let queryUserID = request.query.userID;
let cookie = request.headers.cookie;
let verifyBody = queryUserID && body.strength && body.punctuality
&& body.friendliness && body.fun && body.playAgain // commenting on profile is optional
let verifyBody = queryUserID && body.strength && body.punctuality && body.friendliness && body.fun && (body.playAgain !== undefined);
let userID = ObjectID(queryUserID);

let loggedIn = false;
Expand All @@ -25,7 +24,7 @@ async function addRatings(request, response) {
}

if (loggedIn && verifyBody) {
let tokenDocument = await getUserIDFromToken(cookie);
let tokenDocument = await getTokenDocument(cookie);
let raterID = tokenDocument.userID;

// users can't rate their own profiles.
Expand Down
Binary file modified gamersnet_backend/routes/users/defaultAvatar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions gamersnet_backend/routes/users/getUsername.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
let {getUserByID} = require('../../persistence/users');

async function getUsername(request, response) {
let id = request.params.id
let result = null;

// for some reason, an invalid id parameter will be the string "undefined"
if (id !== 'undefined') {
result = await getUserByID(id);
}

if (result === null) {
response.status(404).end();
} else {
response.status(200).end(JSON.stringify({username: result.username}));
}
}

module.exports = getUsername;
2 changes: 2 additions & 0 deletions gamersnet_backend/routes/users/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ let getUserDetails = require('./getUserDetails');
let updateDetailsCallback = require('./updateDetails');
let getAchievements = require('./getAchievements');
let updateRank = require('./updateRank');
let getUsername = require('./getUsername');

app.post('/createAccount', createAccount)
app.post('/authenticate', authenticate)

app.get('/getAvatar/:id', getAvatar);
app.get('/getUserDetails/:id', getUserDetails);
app.get('/getAchievements/:id', getAchievements);
app.get('/getUsername/:id', getUsername);

app.patch('/updatePassword', updatePassword)
app.patch('/updateAvatar', upload.single('image'), updateAvatar);
Expand Down
4 changes: 2 additions & 2 deletions gamersnet_backend/routes/users/updateAvatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

let {upsertAvatar} = require('../../persistence/avatar');
let {verifyUserLoggedIn} = require('../utilities/tokenUtility');
let {getUserIDFromToken} = require('../../persistence/tokens');
let {getTokenDocument} = require('../../persistence/tokens');

let imageSize = require('image-size');

Expand All @@ -25,7 +25,7 @@ async function updateAvatar(request, response) {
let size = imageSize(request.file.buffer);

if (size.height <= 256 && size.width <= 256) {
let tokenDocument = await getUserIDFromToken(token);
let tokenDocument = await getTokenDocument(token);
let userID = tokenDocument.userID;

await upsertAvatar(userID, request.file.buffer, request.file.mimetype);
Expand Down
4 changes: 2 additions & 2 deletions gamersnet_backend/routes/users/updateDetails.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

let { getUserIDFromToken } = require('../../persistence/tokens');
let { getTokenDocument } = require('../../persistence/tokens');
let { updateDetails } = require('../../persistence/users');
let {verifyUserLoggedIn} = require('../utilities/tokenUtility');

Expand Down Expand Up @@ -40,7 +40,7 @@ async function updateDetailsCallback(request, response) {

//invalid cookie
if (await verifyUserLoggedIn(token)) {
let tokenDocument = await getUserIDFromToken(token);
let tokenDocument = await getTokenDocument(token);

await updateDetails(tokenDocument.userID, body);

Expand Down
4 changes: 2 additions & 2 deletions gamersnet_backend/routes/users/updatePassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const bcrypt = require('bcrypt');

let {getUserIDFromToken, updateUserToken, TOKEN_LIFE_SPAN} = require('../../persistence/tokens');
let {getTokenDocument, updateUserToken, TOKEN_LIFE_SPAN} = require('../../persistence/tokens');
let {updateUserPassword, getUserByID} = require('../../persistence/users');
const alphaNumericize = require('../utilities/alphaNumericize');
let makeHash = require('../utilities/makeHash');
Expand Down Expand Up @@ -30,7 +30,7 @@ async function updatePassword(request, response) {

if (isValid) {
// get the token document of this user
let tokenDocument = await getUserIDFromToken(token);
let tokenDocument = await getTokenDocument(token);
let userID = tokenDocument.userID;

// get the user document to validate old password
Expand Down
4 changes: 2 additions & 2 deletions gamersnet_backend/routes/users/updateRank.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

let { getUserIDFromToken } = require('../../persistence/tokens');
let { getTokenDocument } = require('../../persistence/tokens');
let { updateRankByUserID } = require('../../persistence/users');
let {verifyUserLoggedIn} = require('../utilities/tokenUtility');

Expand Down Expand Up @@ -33,7 +33,7 @@ async function updateRank(request, response) {

//invalid cookie
if (await verifyUserLoggedIn(token)) {
let tokenDocument = await getUserIDFromToken(token);
let tokenDocument = await getTokenDocument(token);

await updateRankByUserID(tokenDocument.userID, body);

Expand Down
2 changes: 1 addition & 1 deletion gamersnet_frontend/src/__tests__/profile/testAvatar.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { render, screen, waitFor } from '@testing-library/react';
import ProfileChangeAvatar from '../../components/profileChangeAvatar';
import ProfileChangeAvatar from '../../components/profileComponents/profileChangeAvatar';
import '@testing-library/jest-dom';
import { Simulate } from 'react-dom/test-utils';

Expand Down
2 changes: 1 addition & 1 deletion gamersnet_frontend/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import SignIn from './components/signin';
import Register from './components/register';
import Logout from './components/logout';
import AddPost from './components/createPost';
import Profile from './components/profile';
import Profile from './components/profileComponents/profileMain';
import GameSearch from './components/gameSearch'
import RecentChats from './components/recentChats';

Expand Down
2 changes: 1 addition & 1 deletion gamersnet_frontend/src/components/button/styles.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
button {
color: white;
padding: 0.5rem;
border-radius: 15px;
border-radius: 10px;
cursor: pointer;
font-size: 1rem;
outline: none;
Expand Down
24 changes: 12 additions & 12 deletions gamersnet_frontend/src/components/displayPosts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ export default class DisplayPosts extends React.Component{
}

componentDidMount() {
let userID = "";
let userID = this.props.userID || '';

let fetchPosts = APIFetch('/posts/listUserPosts?userID=' + userID, null, 'GET');
let fetchPosts = APIFetch('/posts/listUserPosts?userID=' + userID, null, 'GET');

fetchPosts.then(async (data) => {
if (await data.ok) {
let posts = await data.json();
this.setState({items: posts, status : ""});
} else if (await data.status === 404){
this.setState({status : "No posts found"});
} else {
this.setState({status : "Network Problem"});
}
});
fetchPosts.then(async (data) => {
if (await data.ok) {
let posts = await data.json();
this.setState({items: posts, status : ""});
} else if (await data.status === 404){
this.setState({status : "No posts found"});
} else {
this.setState({status : "Network Problem"});
}
});
}

render() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import APIFetch from '../../utilities/api';
import APIFetch from '../../../utilities/api';

import './styles.css';

Expand All @@ -26,9 +26,11 @@ export default class ProfileAchievements extends React.Component {
this.state = {
achievements: []
};

this.localFetch = this.localFetch.bind(this);
}

componentDidMount() {
localFetch() {
let fetchData = APIFetch('/users/getAchievements/' + this.props.userID);

fetchData.then(async (data) => {
Expand All @@ -49,6 +51,16 @@ export default class ProfileAchievements extends React.Component {
}
});
}

componentDidMount() {
this.localFetch();
}

componentDidUpdate(prevProps) {
if (prevProps.userID !== this.props.userID) {
this.localFetch();
}
}

render() {
return (
Expand Down
Loading