Skip to content

Commit 99b701b

Browse files
committed
implement project download feature for each user
1 parent 859bfc2 commit 99b701b

File tree

9 files changed

+79
-18
lines changed

9 files changed

+79
-18
lines changed

app/src/Dashboard/Project.jsx

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { gql, useMutation } from '@apollo/client';
33
import PropTypes from 'prop-types';
44

55

6-
const Project = ({ name, likes, projId, userId }) => {
6+
const Project = ({ name, likes, projId, userId, username }) => {
77

88
// IMPORTANT:
99
// 1) schema change projId => id to allows Apollo Client cache auto-update. Only works with 'id'
@@ -20,7 +20,19 @@ const Project = ({ name, likes, projId, userId }) => {
2020

2121
const [addLike] = useMutation(ADD_LIKE);
2222

23-
function handleClick(e) {
23+
24+
const MAKE_COPY = gql`
25+
mutation MakeCopy ($userId: ID!, $projId: ID!, $username: String!) {
26+
makeCopy(userId: $userId, projId: $projId, username: $username)
27+
{
28+
id
29+
}
30+
}`;
31+
32+
const [ makeCopy ] = useMutation(MAKE_COPY);
33+
34+
35+
function handleLike(e) {
2436
e.preventDefault();
2537
// IMPORTANT: DO NOT ADD extra comma to the last line of the variable object, the query will not work
2638
const myVar = {
@@ -34,12 +46,37 @@ const Project = ({ name, likes, projId, userId }) => {
3446
addLike(myVar);
3547
}
3648

49+
// Use current user info to make a make copy of another user's project
50+
const currUserSSID = window.localStorage.getItem('ssid') || 'unavailable';
51+
const currUsername = window.localStorage.getItem('username') || 'unavailable';
52+
// if (userSSID !== 'guest') {
53+
// myVar = { userId: userSSID };
54+
// }
55+
56+
function handleDownload(e) {
57+
e.preventDefault();
58+
const myVar = {
59+
variables:
60+
{
61+
projId,
62+
userId: currUserSSID,
63+
username: currUsername,
64+
},
65+
};
66+
// send Mutation
67+
makeCopy(myVar);
68+
}
69+
3770
return (
3871
<div className = 'project'>
3972
<h2>Project: { name }</h2>
4073
{/* <h3>Project ID: {projId} </h3> */}
74+
<h3>Author: { username }</h3>
4175
<h3>Likes: { likes }</h3>
42-
<button onClick={ handleClick }>like me!</button>
76+
<div>
77+
<button onClick={ handleLike }>like me!</button>
78+
<button onClick={ handleDownload }>download me!</button>
79+
</div>
4380
</div>
4481
);
4582
};
@@ -49,6 +86,7 @@ Project.propTypes = {
4986
name: PropTypes.string.isRequired,
5087
projId: PropTypes.string.isRequired,
5188
userId: PropTypes.string.isRequired,
89+
username: PropTypes.string.isRequired,
5290
likes: PropTypes.number.isRequired,
5391
};
5492

app/src/Dashboard/ProjectContainer.jsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@ const ProjectContainer = () => {
1414
getAllProjects(userId: $userId) {
1515
name
1616
likes
17-
id
17+
id
18+
userId
19+
username
1820
}
1921
}`;
2022

21-
// For now, if cookie exists, pull projects for the specific user only, otherwise pull all projects
22-
const userSSID = window.localStorage.getItem('ssid') || '';
2323
let myVar = {};
24-
if (userSSID !== 'guest') {
25-
myVar = { userId: userSSID };
26-
}
24+
// Need this for the individual user dasboard, for now, dashboard shows all projects from all users
25+
const userSSID = window.localStorage.getItem('ssid') || 'unavailable';
26+
const username = window.localStorage.getItem('username') || 'unavailable';
27+
// if (userSSID !== 'guest') {
28+
// myVar = { userId: userSSID };
29+
// }
30+
2731
// useQuery hook abstracts fetch request
2832
const { loading, error, data } = useQuery(GET_PROJECTS, { pollInterval: 2000, variables: myVar } ); // Need to find where the userId is stored for the logged in user.
2933
if (loading) return <p>Loading...</p>;
@@ -36,12 +40,14 @@ const ProjectContainer = () => {
3640
key= { index }
3741
name = {proj.name}
3842
likes = {proj.likes}
39-
userId = {userSSID}
43+
userId = {proj.userId}
44+
username = {proj.username}
4045
projId = {proj.id}
4146
/>);
4247

4348
return (
4449
<div>
50+
<h1> Public Dashboard </h1>
4551
<Link to="/">
4652
<button type="button">Go Back</button>
4753
</Link>

app/src/helperFunctions/auth.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ export const sessionIsCreated = (
3030
if (data.sessionId && typeof data.sessionId === 'string') {
3131
// check that a session id was passed down
3232
window.localStorage.setItem('ssid', data.sessionId);
33+
// save username locally, will be added to saved project for each user
34+
window.localStorage.setItem('username', username);
35+
3336
return 'Success';
3437
}
3538
return data; // error message returned from userController.verifyUser
@@ -61,6 +64,8 @@ export const newUserIsCreated = (
6164
if (data.sessionId && typeof data.sessionId === 'string') {
6265
// check that a session id was passed down
6366
window.localStorage.setItem('ssid', data.sessionId);
67+
// save username locally, will be added to saved project for each user
68+
window.localStorage.setItem('username', username);
6469
return 'Success';
6570
}
6671
return data; // response is either Email Taken or Username Taken, refer to userController.createUser

app/src/helperFunctions/projectGetSaveDel.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export const saveProject = (
3232
const body = JSON.stringify({
3333
name,
3434
project: workspace,
35-
userId: window.localStorage.getItem('ssid')
35+
userId: window.localStorage.getItem('ssid'),
36+
username: window.localStorage.getItem('username'),
3637
});
3738
const project = fetch(`${serverURL}/saveProject`, {
3839
method: 'POST',

server/controllers/projectController.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ const projectController = {};
66

77
projectController.saveProject = (req, res, next) => {
88
// pull project name and project itself from body
9-
const { name, project, userId } = req.body;
9+
const { name, project, userId, username } = req.body;
1010
// pull ssid from cookies for user id
1111
Projects.findOneAndUpdate(
1212
// looks in projects collection for project by user and name
13-
{ name, userId },
13+
{ name, userId, username },
1414
// update or insert the project
1515
{ project },
1616
// Options:

server/graphQL/resolvers/mutation.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const Project = {
4747
name: resp.name,
4848
id: resp._id,
4949
userId: resp.userId,
50+
username: resp.username,
5051
likes: resp.likes,
5152
})}
5253

@@ -55,11 +56,12 @@ const Project = {
5556
name: 'Error',
5657
id: 'Error',
5758
userId: 'Error',
59+
username: 'Error',
5860
likes: -1,
5961
};
6062
},
6163

62-
makeCopy: async (parent, { projId, userId }) => {
64+
makeCopy: async (parent, { projId, userId, username }) => {
6365
const filter = { _id: projId };
6466
const target = await Projects.findOne(filter);
6567
// make a copy with the passed in userId
@@ -68,6 +70,7 @@ const Project = {
6870
likes: target.likes,
6971
project: target.project,
7072
userId,
73+
username,
7174
};
7275
const resp = await Projects.create(copy);
7376

@@ -76,6 +79,7 @@ const Project = {
7679
name: resp.name,
7780
id: resp._id,
7881
userId: resp.userId,
82+
username: resp.username,
7983
likes: resp.likes,
8084
})}
8185

@@ -84,6 +88,7 @@ const Project = {
8488
name: 'Error',
8589
id: 'Error',
8690
userId: 'Error',
91+
username: 'Error',
8792
likes: -1,
8893
};
8994
},

server/graphQL/resolvers/query.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Tests, Projects } = require('../../models/reactypeModels');
1+
const { Tests, Projects, Users } = require('../../models/reactypeModels');
22

33
// Link to Apollo Query Types:
44
// https://www.apollographql.com/docs/apollo-server/data/resolvers/#defining-a-resolver
@@ -32,6 +32,7 @@ const Project = {
3232
name: resp.name,
3333
id: resp._id,
3434
userId: resp.userId,
35+
username: resp.username,
3536
likes: resp.likes,
3637
});
3738
}
@@ -41,13 +42,15 @@ const Project = {
4142
name: 'Error',
4243
id: 'Error',
4344
userId: 'Error',
45+
username: 'Error',
4446
likes: -1,
4547
};
4648
},
4749

4850
getAllProjects: async (parent, { userId }) => {
51+
4952
let resp = await Projects.find({});
50-
// console.log('resp >>> ', resp);
53+
// console.log('getAllProjects resp >>> ', resp);
5154
if (userId) {
5255
// use loosely equal for the callback because there are some discrepancy between the type of userId from the db vs from the mutation query
5356
resp = resp.filter(proj => proj.userId == userId);
@@ -58,6 +61,7 @@ const Project = {
5861
name: proj.name,
5962
id: proj._id,
6063
userId: proj.userId,
64+
username: proj.username,
6165
likes: proj.likes,
6266
}));
6367
}
@@ -67,6 +71,7 @@ const Project = {
6771
name: 'Error',
6872
id: 'Error',
6973
userId: 'Error',
74+
username: 'Error',
7075
likes: -1,
7176
}];
7277
},

server/graphQL/typeDefs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ const Project = gql`
2929
3030
type Mutation {
3131
addLike(projId: ID!, likes: Int!): Project
32-
makeCopy(projId: ID!, userId: ID!): Project
32+
makeCopy(projId: ID!, userId: ID!, username: String!): Project
3333
}
3434
3535
type Project {
3636
name: String!
3737
likes: Int
3838
id: ID!
3939
userId: ID!
40+
username: String!
4041
}
4142
4243
type Query {
4344
getProject(projId: ID!): Project
4445
getAllProjects(userId: ID): [Project]
45-
getLikedProjects(userId: ID): [Project]
4646
}
4747
`;
4848

server/models/reactypeModels.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const projectSchema = new Schema({
6464
type: Schema.Types.ObjectId,
6565
ref: 'Users',
6666
},
67+
username: {type: String, required: true },
6768
createdAt: { type: Date, default: Date.now }
6869
});
6970

0 commit comments

Comments
 (0)