Skip to content

Commit adb9603

Browse files
committed
add delete button and functionality
1 parent cdbc499 commit adb9603

File tree

4 files changed

+59
-12
lines changed

4 files changed

+59
-12
lines changed

app/src/Dashboard/Project.jsx

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
@@ -19,9 +19,9 @@ const Test = {
1919
const update = { name: args.name, likes: args.likes };
2020
const options = { new: true };
2121
const resp = await Tests.findOneAndUpdate(filter, update, options);
22-
22+
2323
console.log('Updated database with', resp);
24-
24+
2525
if (resp) return { description: resp.name, id: resp._id, likes: resp.likes };
2626
return { description: 'Error updating', id: resp._id, likes: 0 };
2727
},
@@ -36,20 +36,21 @@ const Test = {
3636

3737

3838
const Project = {
39-
addLike: async (parent, { projId, likes}) => {
39+
addLike: async (parent, { projId, likes }) => {
4040
const filter = { _id: projId };
4141
const update = { likes };
4242
const options = { new: true };
4343
const resp = await Projects.findOneAndUpdate(filter, update, options);
44-
44+
4545
if (resp) {
4646
return ({
4747
name: resp.name,
4848
id: resp._id,
4949
userId: resp.userId,
5050
username: resp.username,
5151
likes: resp.likes,
52-
})}
52+
});
53+
}
5354

5455
// TODO: Go back to this to see how to handle error later
5556
return {
@@ -81,7 +82,8 @@ const Project = {
8182
userId: resp.userId,
8283
username: resp.username,
8384
likes: resp.likes,
84-
})}
85+
});
86+
}
8587

8688
// TODO: Go back to this to see how to handle error later
8789
return {
@@ -92,6 +94,29 @@ const Project = {
9294
likes: -1,
9395
};
9496
},
97+
98+
deleteProject: async (parent, { projId }) => {
99+
const filter = { _id: projId };
100+
const options = { strict: true };
101+
const resp = await Projects.findOneAndDelete(filter, options);
102+
console.log("resp", resp);
103+
if (resp) {
104+
return ({
105+
name: resp.name,
106+
likes: resp.likes,
107+
id: resp._id,
108+
userId: resp.userId,
109+
username: resp.username,
110+
});
111+
}
112+
return {
113+
name: 'Error',
114+
id: 'Error',
115+
userId: 'Error',
116+
username: 'Error',
117+
likes: -1,
118+
};
119+
},
95120
};
96121

97122
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)