Skip to content

Commit 1f01772

Browse files
committed
Merge branch 'router' of https://github.com/slhoehn/ReacType into typescript
2 parents b1b9cab + adb9603 commit 1f01772

File tree

4 files changed

+59
-12
lines changed

4 files changed

+59
-12
lines changed

app/src/Dashboard/Project.tsx

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

55

6-
const Project = ({ name, likes, id, userId, username }) => {
7-
8-
// IMPORTANT:
6+
const Project = ({
7+
name, likes, id, userId, username,
8+
}) => {
9+
// IMPORTANT:
910
// 1) schema change projId => id to allows Apollo Client cache auto-update. Only works with 'id'
1011
// 2) always request the 'id' in a mutation request
1112

@@ -28,9 +29,18 @@ const Project = ({ name, likes, id, userId, username }) => {
2829
id
2930
}
3031
}`;
31-
32-
const [ makeCopy ] = useMutation(MAKE_COPY);
3332

33+
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+
43+
const [deleteProject] = useMutation(DELETE_PROJECT);
3444

3545
function handleLike(e) {
3646
e.preventDefault();
@@ -67,6 +77,17 @@ const Project = ({ name, likes, id, userId, username }) => {
6777
makeCopy(myVar);
6878
}
6979

80+
function handleDelete(e) {
81+
e.preventDefault();
82+
const myVar = {
83+
variables:
84+
{
85+
projId: id,
86+
},
87+
};
88+
deleteProject(myVar);
89+
}
90+
7091
return (
7192
<div className = 'project'>
7293
<h2>Project: { name }</h2>
@@ -76,6 +97,7 @@ const Project = ({ name, likes, id, userId, username }) => {
7697
<div>
7798
<button onClick={ handleLike }>like me!</button>
7899
<button onClick={ handleDownload }>download me!</button>
100+
<button onClick={ handleDelete }>delete</button>
79101
</div>
80102
</div>
81103
);

app/src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
Switch,
1717
} from 'react-router-dom';
1818

19-
2019
/*
2120
* Dashboard
2221
*/

server/graphQL/resolvers/mutation.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ const Test = {
2020
const update = { name: args.name, likes: args.likes };
2121
const options = { new: true };
2222
const resp = await Tests.findOneAndUpdate(filter, update, options);
23-
23+
2424
console.log('Updated database with', resp);
25-
25+
2626
if (resp) return { description: resp.name, id: resp._id, likes: resp.likes };
2727
return { description: 'Error updating', id: resp._id, likes: 0 };
2828
},
@@ -37,20 +37,21 @@ const Test = {
3737

3838

3939
const Project = {
40-
addLike: async (parent, { projId, likes}) => {
40+
addLike: async (parent, { projId, likes }) => {
4141
const filter = { _id: projId };
4242
const update = { likes };
4343
const options = { new: true };
4444
const resp = await Projects.findOneAndUpdate(filter, update, options);
45-
45+
4646
if (resp) {
4747
return ({
4848
name: resp.name,
4949
id: resp._id,
5050
userId: resp.userId,
5151
username: resp.username,
5252
likes: resp.likes,
53-
})}
53+
});
54+
}
5455

5556
// TODO: Go back to this to see how to handle error later
5657
return {
@@ -86,7 +87,8 @@ const Project = {
8687
userId: resp.userId,
8788
username: resp.username,
8889
likes: resp.likes,
89-
})}
90+
});
91+
}
9092

9193
// TODO: Go back to this to see how to handle error later
9294
return {
@@ -97,6 +99,29 @@ const Project = {
9799
likes: -1,
98100
};
99101
},
102+
103+
deleteProject: async (parent, { projId }) => {
104+
const filter = { _id: projId };
105+
const options = { strict: true };
106+
const resp = await Projects.findOneAndDelete(filter, options);
107+
console.log("resp", resp);
108+
if (resp) {
109+
return ({
110+
name: resp.name,
111+
likes: resp.likes,
112+
id: resp._id,
113+
userId: resp.userId,
114+
username: resp.username,
115+
});
116+
}
117+
return {
118+
name: 'Error',
119+
id: 'Error',
120+
userId: 'Error',
121+
username: 'Error',
122+
likes: -1,
123+
};
124+
},
100125
};
101126

102127
module.exports = {

server/graphQL/typeDefs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const Project = gql`
3030
type Mutation {
3131
addLike(projId: ID!, likes: Int!): Project
3232
makeCopy(projId: ID!, userId: ID!, username: String!): Project
33+
deleteProject(projId: ID!): Project
3334
}
3435
3536
type Project {

0 commit comments

Comments
 (0)