Skip to content

Commit cc6dff5

Browse files
committed
implement toggle publish state
1 parent 5211416 commit cc6dff5

File tree

4 files changed

+66
-15
lines changed

4 files changed

+66
-15
lines changed

app/src/Dashboard/Project.tsx

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import React from 'react';
22
import { useMutation } from '@apollo/client';
3-
import { ADD_LIKE, MAKE_COPY, DELETE_PROJECT } from './gqlStrings';
3+
import {
4+
ADD_LIKE,
5+
MAKE_COPY,
6+
DELETE_PROJECT,
7+
PUBLISH_PROJECT,
8+
} from './gqlStrings';
49

510
// Variable validation using typescript
611
type props = {
@@ -9,10 +14,15 @@ type props = {
914
userId: string,
1015
username: string,
1116
likes: number,
17+
published: boolean,
1218
};
1319

20+
// Use current user info to make a make copy of another user's project
21+
const currUserSSID = window.localStorage.getItem('ssid') || 'unavailable';
22+
const currUsername = window.localStorage.getItem('username') || 'unavailable';
23+
1424
const Project = ({
15-
name, likes, id, username,
25+
name, likes, id, username, published,
1626
}: props) : JSX.Element => {
1727
// IMPORTANT:
1828
// 1) schema change projId => id to allows Apollo Client cache auto-update. Only works with 'id'
@@ -21,10 +31,10 @@ const Project = ({
2131
const [addLike] = useMutation(ADD_LIKE);
2232
const [makeCopy] = useMutation(MAKE_COPY);
2333
const [deleteProject] = useMutation(DELETE_PROJECT);
34+
const [publishProject] = useMutation(PUBLISH_PROJECT);
2435

2536
function handleLike(e) {
2637
e.preventDefault();
27-
// IMPORTANT: DO NOT ADD extra comma to the last line of the variable object, the query will not work
2838
const myVar = {
2939
variables:
3040
{
@@ -36,10 +46,6 @@ const Project = ({
3646
addLike(myVar);
3747
}
3848

39-
// Use current user info to make a make copy of another user's project
40-
const currUserSSID = window.localStorage.getItem('ssid') || 'unavailable';
41-
const currUsername = window.localStorage.getItem('username') || 'unavailable';
42-
4349

4450
function handleDownload(e) {
4551
e.preventDefault();
@@ -51,7 +57,6 @@ const Project = ({
5157
username: currUsername,
5258
},
5359
};
54-
// send Mutation
5560
makeCopy(myVar);
5661
}
5762

@@ -66,15 +71,31 @@ const Project = ({
6671
deleteProject(myVar);
6772
}
6873

74+
function handlePublish(e) {
75+
e.preventDefault();
76+
const myVar = {
77+
variables:
78+
{
79+
projId: id,
80+
published: !published,
81+
},
82+
};
83+
publishProject(myVar);
84+
}
85+
86+
6987
return (
7088
<div className = 'project'>
7189
<h2>Project: { name }</h2>
7290
<h3>Author: { username }</h3>
7391
<h3>Likes: { likes }</h3>
7492
<div>
7593
<button onClick={ handleLike }>like me!</button>
76-
{currUsername !== username ? <button onClick={ handleDownload }>download me!</button> : <span></span>}
77-
<button onClick={ handleDelete }>delete</button>
94+
{currUsername !== username ? <button onClick={ handleDownload }>download me!</button> : <span></span>}
95+
{currUsername === username ? <button onClick={ handleDelete }>delete</button> : <span></span>}
96+
{ currUsername === username
97+
? <button onClick={ handlePublish }> {published ? 'Unpublish Me!' : 'Publish Me!'} </button>
98+
: <span></span> }
7899
</div>
79100
</div>
80101
);

app/src/Dashboard/ProjectContainer.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@ const ProjectContainer = () => {
2626
console.log('Projects >>> ', projects);
2727
// generate an array of Project components based on data
2828
const publicProjects = [];
29-
const userProjects = [];
29+
const userProjects = [];
3030
projects.forEach((proj, index) => {
3131
const component = <Project
3232
key= { index }
3333
name = {proj.name}
3434
likes = {proj.likes}
35+
published = { proj.published }
3536
userId = {proj.userId}
3637
username = {proj.username}
3738
id = {proj.id}
3839
/>;
3940
if (username === proj.username) userProjects.push(component);
40-
else publicProjects.push(component);
41+
if (proj.published) publicProjects.push(component);
4142
});
4243

4344
return (

app/src/Dashboard/gqlStrings.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export const GET_PROJECTS = gql`query GetAllProjects($userId: ID) {
88
likes
99
id
1010
userId
11-
username
11+
username
12+
published
1213
}
1314
}`;
1415

@@ -19,11 +20,10 @@ export const ADD_LIKE = gql`
1920
addLike(projId: $projId, likes: $likes)
2021
{
2122
id
22-
likes
2323
}
2424
}`;
2525

26-
export const MAKE_COPY = gql`
26+
export const MAKE_COPY = gql`
2727
mutation MakeCopy ($userId: ID!, $projId: ID!, $username: String!) {
2828
makeCopy(userId: $userId, projId: $projId, username: $username)
2929
{
@@ -38,3 +38,12 @@ export const DELETE_PROJECT = gql`
3838
id
3939
}
4040
}`;
41+
42+
export const PUBLISH_PROJECT = gql`
43+
mutation Publish($projId: ID!, $published: Boolean!) {
44+
publishProject(projId: $projId, published: $published)
45+
{
46+
id
47+
published
48+
}
49+
}`;

server/graphQL/resolvers/mutation.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,26 @@ const Project = {
7171
});
7272
},
7373

74+
deleteProject: async (parent, { projId }) => {
75+
const filter = { _id: projId };
76+
const options = { strict: true };
77+
const resp = await Projects.findOneAndDelete(filter, options);
78+
79+
if (resp) {
80+
return ({
81+
name: resp.name,
82+
id: resp._id,
83+
userId: resp.userId,
84+
likes: resp.likes,
85+
published: resp.published,
86+
});
87+
}
88+
89+
throw new UserInputError('Project is not found. Please try another project ID', {
90+
argumentName: 'projId',
91+
});
92+
},
93+
7494
publishProject: async (parent, { projId, published }) => {
7595

7696
const filter = { _id: projId };

0 commit comments

Comments
 (0)