Skip to content

Commit 04e6c14

Browse files
committed
migrate React component to Typescript
2 parents 1f01772 + ec0fb18 commit 04e6c14

File tree

7 files changed

+73
-172
lines changed

7 files changed

+73
-172
lines changed

app/src/Dashboard/Form.tsx

Lines changed: 0 additions & 54 deletions
This file was deleted.

app/src/Dashboard/FormsContainer.tsx

Lines changed: 0 additions & 35 deletions
This file was deleted.

app/src/Dashboard/Project.tsx

Lines changed: 15 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,25 @@
11
import React from 'react';
2-
import { gql, useMutation } from '@apollo/client';
3-
import PropTypes from 'prop-types';
4-
2+
import { useMutation } from '@apollo/client';
3+
import { ADD_LIKE, MAKE_COPY, DELETE_PROJECT } from './gqlStrings';
4+
5+
// Variable validation using typescript
6+
type props = {
7+
name: string,
8+
id: string,
9+
userId: string,
10+
username: string,
11+
likes: number,
12+
};
513

614
const Project = ({
7-
name, likes, id, userId, username,
8-
}) => {
15+
name, likes, id, username,
16+
}: props) : JSX.Element => {
917
// IMPORTANT:
1018
// 1) schema change projId => id to allows Apollo Client cache auto-update. Only works with 'id'
1119
// 2) always request the 'id' in a mutation request
1220

13-
const ADD_LIKE = gql`
14-
mutation AddLike($projId: ID!, $likes: Int!) {
15-
addLike(projId: $projId, likes: $likes)
16-
{
17-
id
18-
likes
19-
}
20-
}`;
21-
2221
const [addLike] = useMutation(ADD_LIKE);
23-
24-
25-
const MAKE_COPY = gql`
26-
mutation MakeCopy ($userId: ID!, $projId: ID!, $username: String!) {
27-
makeCopy(userId: $userId, projId: $projId, username: $username)
28-
{
29-
id
30-
}
31-
}`;
32-
3322
const [makeCopy] = useMutation(MAKE_COPY);
34-
35-
const DELETE_PROJECT = gql`
36-
mutation DeleteProject($projId: ID!) {
37-
deleteProject(projId: $projId)
38-
{
39-
id
40-
}
41-
}`;
42-
4323
const [deleteProject] = useMutation(DELETE_PROJECT);
4424

4525
function handleLike(e) {
@@ -59,9 +39,7 @@ const Project = ({
5939
// Use current user info to make a make copy of another user's project
6040
const currUserSSID = window.localStorage.getItem('ssid') || 'unavailable';
6141
const currUsername = window.localStorage.getItem('username') || 'unavailable';
62-
// if (userSSID !== 'guest') {
63-
// myVar = { userId: userSSID };
64-
// }
42+
6543

6644
function handleDownload(e) {
6745
e.preventDefault();
@@ -91,26 +69,16 @@ const Project = ({
9169
return (
9270
<div className = 'project'>
9371
<h2>Project: { name }</h2>
94-
{/* <h3>Project ID: {projId} </h3> */}
9572
<h3>Author: { username }</h3>
9673
<h3>Likes: { likes }</h3>
9774
<div>
9875
<button onClick={ handleLike }>like me!</button>
99-
<button onClick={ handleDownload }>download me!</button>
76+
{currUsername !== username ? <button onClick={ handleDownload }>download me!</button> : <span></span>}
10077
<button onClick={ handleDelete }>delete</button>
10178
</div>
10279
</div>
10380
);
10481
};
10582

106-
// Variable validation using propTypes
107-
Project.propTypes = {
108-
name: PropTypes.string.isRequired,
109-
id: PropTypes.string.isRequired,
110-
userId: PropTypes.string.isRequired,
111-
username: PropTypes.string.isRequired,
112-
likes: PropTypes.number.isRequired,
113-
};
114-
11583

11684
export default Project;
Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
11
import React from 'react';
22
import { Link } from 'react-router-dom';
3-
4-
import { gql, useQuery } from '@apollo/client';
5-
3+
import { useQuery } from '@apollo/client';
4+
import { GET_PROJECTS } from './gqlStrings';
65
import Project from './Project.tsx';
6+
77
// Implement Apollo Client useQuery hook to retrieve data from the server through graphQL. This includes 2 steps:
88
// 1) Impliment Apollo Provider in the top component in ./src/index.js, this allows children components access to the queried data
99
// 2) useQuery hook will update the data stored in Apollo Client's cache and automatically trigger child components rendering
1010

1111
const ProjectContainer = () => {
12-
// define the graphQL query string
13-
const GET_PROJECTS = gql`query GetAllProjects($userId: ID) {
14-
getAllProjects(userId: $userId) {
15-
name
16-
likes
17-
id
18-
userId
19-
username
20-
}
21-
}`;
2212

2313
let myVar = {};
2414
// Need this for the individual user dasboard, for now, dashboard shows all projects from all users
@@ -29,33 +19,44 @@ const ProjectContainer = () => {
2919
// }
3020

3121
// useQuery hook abstracts fetch request
32-
const { loading, error, data } = useQuery(GET_PROJECTS, { pollInterval: 2000, variables: myVar } ); // Need to find where the userId is stored for the logged in user.
22+
const { loading, error, data } = useQuery(GET_PROJECTS, { pollInterval: 2000, variables: myVar }); // Need to find where the userId is stored for the logged in user.
3323
if (loading) return <p>Loading...</p>;
3424
if (error) return <p>Error :{error}</p>;
3525
// based on resolver(getAllProject) for this query, the data is stored in the data object with the key 'getAllProjects'
36-
const myProjs = data.getAllProjects;
37-
console.log('Projects >>> ', myProjs);
26+
const projects = data.getAllProjects;
27+
console.log('Projects >>> ', projects);
3828
// generate an array of Project components based on data
39-
const projects = myProjs.map((proj, index) => <Project
40-
key= { index }
41-
name = {proj.name}
42-
likes = {proj.likes}
43-
userId = {proj.userId}
44-
username = {proj.username}
45-
id = {proj.id}
46-
/>);
29+
const publicProjects = [];
30+
const userProjects = [];
31+
projects.forEach((proj, index) => {
32+
const component = <Project
33+
key= { index }
34+
name = {proj.name}
35+
likes = {proj.likes}
36+
userId = {proj.userId}
37+
username = {proj.username}
38+
id = {proj.id}
39+
/>;
40+
if (username === proj.username) userProjects.push(component);
41+
else publicProjects.push(component);
42+
});
4743

4844
return (
4945
<div>
50-
<h1> Public Dashboard </h1>
5146
<Link to="/">
5247
<button type="button">Go Back</button>
5348
</Link>
49+
<h1> Public Dashboard </h1>
5450
<div className = "projectContainer">
55-
{projects}
51+
{publicProjects}
52+
</div>
53+
<hr></hr>
54+
<h1> User Dashboard </h1>
55+
<div className = "projectContainer">
56+
{userProjects}
57+
</div>
58+
5659
</div>
57-
</div>
5860
);
5961
};
60-
6162
export default ProjectContainer;

app/src/Dashboard/gqlStrings.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
11
import { gql } from '@apollo/client';
22

3-
export const {
4-
ADD_LIKE: gql`mutation AddLike($projId: ID!, $likes: Int!) {
3+
4+
// Query
5+
export const GET_PROJECTS = gql`query GetAllProjects($userId: ID) {
6+
getAllProjects(userId: $userId) {
7+
name
8+
likes
9+
id
10+
userId
11+
username
12+
}
13+
}`;
14+
15+
16+
// Mutation
17+
export const ADD_LIKE = gql`
18+
mutation AddLike($projId: ID!, $likes: Int!) {
519
addLike(projId: $projId, likes: $likes)
620
{
721
id
822
likes
923
}
10-
}`,
11-
12-
MAKE_COPY: gql`
24+
}`;
25+
26+
export const MAKE_COPY = gql`
1327
mutation MakeCopy ($userId: ID!, $projId: ID!, $username: String!) {
1428
makeCopy(userId: $userId, projId: $projId, username: $username)
1529
{
1630
id
1731
}
1832
}`;
1933

20-
}
34+
export const DELETE_PROJECT = gql`
35+
mutation DeleteProject($projId: ID!) {
36+
deleteProject(projId: $projId)
37+
{
38+
id
39+
}
40+
}`;

server/graphQL/resolvers/mutation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const Project = {
104104
const filter = { _id: projId };
105105
const options = { strict: true };
106106
const resp = await Projects.findOneAndDelete(filter, options);
107-
console.log("resp", resp);
107+
// console.log("resp", resp);
108108
if (resp) {
109109
return ({
110110
name: resp.name,

server/models/reactypeModels.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ const projectSchema = new Schema({
6666
},
6767
username: {type: String, required: true },
6868
createdAt: { type: Date, default: Date.now }
69-
}, { minimize: false });
69+
}, { minimize: false }
70+
);
7071
// option 'minimize' prevent Mongoose from removing empty 'style' value in the copy
7172

7273
// Test schema for implementing GraphQL

0 commit comments

Comments
 (0)