1
1
import React , { useState } from 'react' ;
2
2
import { useMutation } from '@apollo/client' ;
3
- import {
3
+ import {
4
4
ADD_LIKE ,
5
5
MAKE_COPY ,
6
6
DELETE_PROJECT ,
7
7
PUBLISH_PROJECT ,
8
- ADD_COMMENT ,
8
+ ADD_COMMENT
9
9
} from './gqlStrings' ;
10
10
import CloseIcon from '@mui/icons-material/Close' ;
11
11
import AddCommentIcon from '@mui/icons-material/AddComment' ;
@@ -19,22 +19,27 @@ import ListItemText from '@mui/material/ListItemText';
19
19
import createModal from '../components/right/createModal' ;
20
20
// Variable validation using typescript
21
21
type props = {
22
- name : string ,
23
- id : string ,
24
- userId : string ,
25
- username : string ,
26
- likes : number ,
27
- published : boolean ,
28
- comments : object [ ] ,
22
+ name : string ;
23
+ id : string ;
24
+ userId : string ;
25
+ username : string ;
26
+ likes : number ;
27
+ published : boolean ;
28
+ comments : object [ ] ;
29
29
} ;
30
30
31
31
// Use current user info to make a make copy of another user's project
32
32
const currUserSSID = window . localStorage . getItem ( 'ssid' ) || 'unavailable' ;
33
33
const currUsername = window . localStorage . getItem ( 'username' ) || 'unavailable' ;
34
34
35
35
const Project = ( {
36
- name, likes, id, username, published, comments,
37
- } : props ) : JSX . Element => {
36
+ name,
37
+ likes,
38
+ id,
39
+ username,
40
+ published,
41
+ comments
42
+ } : props ) : JSX . Element => {
38
43
// IMPORTANT:
39
44
// 1) schema change projId => id to allows Apollo Client cache auto-update. Only works with 'id'
40
45
// 2) always request the 'id' in a mutation request
@@ -46,56 +51,52 @@ const Project = ({
46
51
const [ publishProject ] = useMutation ( PUBLISH_PROJECT ) ;
47
52
const [ addComment ] = useMutation ( ADD_COMMENT ) ;
48
53
49
- const noPointer = { cursor : 'default' } ;
54
+ const noPointer = { cursor : 'default' } ;
50
55
//Likes the project when the star icon is clicked
51
56
function handleLike ( e ) {
52
57
e . preventDefault ( ) ;
53
58
const myVar = {
54
- variables :
55
- {
59
+ variables : {
56
60
projId : id ,
57
- likes : likes + 1 ,
58
- } ,
61
+ likes : likes + 1
62
+ }
59
63
} ;
60
64
addLike ( myVar ) ;
61
65
}
62
66
//Makes a copy of the public project and saves as a user project
63
67
function handleDownload ( e ) {
64
68
e . preventDefault ( ) ;
65
69
const myVar = {
66
- variables :
67
- {
70
+ variables : {
68
71
projId : id ,
69
72
userId : currUserSSID ,
70
- username : currUsername ,
71
- } ,
73
+ username : currUsername
74
+ }
72
75
} ;
73
76
makeCopy ( myVar ) ;
74
77
}
75
78
//Publishes project from user dashboard to public dashboard
76
79
function handlePublish ( e ) {
77
80
e . preventDefault ( ) ;
78
81
const myVar = {
79
- variables :
80
- {
82
+ variables : {
81
83
projId : id ,
82
- published : ! published ,
83
- } ,
84
+ published : ! published
85
+ }
84
86
} ;
85
87
publishProject ( myVar ) ;
86
88
}
87
89
//Adds the comment to the project
88
90
function handleComment ( e ) {
89
91
e . preventDefault ( ) ;
90
92
const myVar = {
91
- variables :
92
- {
93
- projId : id ,
94
- comment : commentVal ,
95
- username : currUsername ,
96
- } ,
93
+ variables : {
94
+ projId : id ,
95
+ comment : commentVal ,
96
+ username : currUsername
97
+ }
97
98
} ;
98
- addComment ( myVar )
99
+ addComment ( myVar ) ;
99
100
}
100
101
//sets state of commentVal to what the user types in to comment
101
102
function handleChange ( e ) {
@@ -104,16 +105,17 @@ const Project = ({
104
105
setCommentVal ( commentValue ) ;
105
106
}
106
107
const recentComments = [ ] ;
107
- if ( comments ?. length > 0 ) {
108
+ if ( comments ?. length > 0 ) {
108
109
const reversedCommentArray = comments . slice ( 0 ) . reverse ( ) ;
109
- const min = Math . min ( 6 , reversedCommentArray . length )
110
- for ( let i = 0 ; i < min ; i ++ ) {
111
- recentComments . push (
112
- < p className = 'comment' >
113
- < b > { reversedCommentArray [ i ] . username } </ b > :
114
- { reversedCommentArray [ i ] . text }
115
- </ p >
116
- ) }
110
+ const min = Math . min ( 6 , reversedCommentArray . length ) ;
111
+ for ( let i = 0 ; i < min ; i ++ ) {
112
+ recentComments . push (
113
+ < p className = "comment" >
114
+ < b > { reversedCommentArray [ i ] . username } </ b > :
115
+ { reversedCommentArray [ i ] . text }
116
+ </ p >
117
+ ) ;
118
+ }
117
119
}
118
120
// Closes out the open modal
119
121
const closeModal = ( ) => setModal ( '' ) ;
@@ -123,13 +125,12 @@ const Project = ({
123
125
const handleDelete = ( e ) => {
124
126
e . preventDefault ( ) ;
125
127
const myVar = {
126
- variables :
127
- {
128
- projId : id ,
129
- } ,
128
+ variables : {
129
+ projId : id
130
+ }
130
131
} ;
131
132
deleteProject ( myVar ) ;
132
- }
133
+ } ;
133
134
// Set modal options
134
135
const children = (
135
136
< List className = "export-preference" >
@@ -138,7 +139,7 @@ const Project = ({
138
139
button
139
140
onClick = { handleDelete }
140
141
style = { {
141
- border : '1px solid #1b544b ' ,
142
+ border : '1px solid #3c59ba ' ,
142
143
marginBottom : '2%' ,
143
144
marginTop : '5%'
144
145
} }
@@ -168,61 +169,79 @@ const Project = ({
168
169
} ;
169
170
170
171
return (
171
- < div className = 'project' >
172
- < div className = 'header' >
173
- { currUsername === username ?
174
- < IconButton
175
- tooltip = "Delete Project"
176
- onClick = { deleteProjectModal }
177
- style = { { position : 'absolute' , right : '0' } }
178
- size = "large" >
179
- < CloseIcon />
180
- </ IconButton >
181
- : '' }
182
- < div className = 'projectInfo' >
172
+ < div className = "project" >
173
+ < div className = "header" >
174
+ { currUsername === username ? (
175
+ < IconButton
176
+ tooltip = "Delete Project"
177
+ onClick = { deleteProjectModal }
178
+ style = { { position : 'absolute' , right : '0' } }
179
+ size = "large"
180
+ >
181
+ < CloseIcon />
182
+ </ IconButton >
183
+ ) : (
184
+ ''
185
+ ) }
186
+ < div className = "projectInfo" >
183
187
< b >
184
- < h2 > Project: { name } </ h2 >
185
- < h3 > Author: { username } </ h3 >
186
- < h3 > Likes: { likes } </ h3 >
188
+ < h2 > Project: { name } </ h2 >
189
+ < h3 > Author: { username } </ h3 >
190
+ < h3 > Likes: { likes } </ h3 >
187
191
</ b >
188
192
</ div >
189
193
190
- < div className = "icons" >
194
+ < div className = "icons" >
195
+ < IconButton
196
+ tooltip = "Like Template"
197
+ style = { noPointer }
198
+ onClick = { handleLike }
199
+ size = "large"
200
+ >
201
+ < ThumbUpAltIcon fontSize = "Large" />
202
+ </ IconButton >
203
+ { currUsername !== username ? (
191
204
< IconButton
192
- tooltip = "Like Template"
205
+ tooltip = "Download Template"
193
206
style = { noPointer }
194
- onClick = { handleLike }
195
- size = "large" >
196
- < ThumbUpAltIcon fontSize = 'Large' />
197
- </ IconButton >
198
- { currUsername !== username ?
199
- < IconButton
200
- tooltip = "Download Template"
201
- style = { noPointer }
202
- onClick = { handleDownload }
203
- size = "large" >
204
- < GetAppIcon fontSize = "large" className = "download" />
205
- </ IconButton >
206
- : '' }
207
- { currUsername === username ?
207
+ onClick = { handleDownload }
208
+ size = "large"
209
+ >
210
+ < GetAppIcon fontSize = "large" className = "download" />
211
+ </ IconButton >
212
+ ) : (
213
+ ''
214
+ ) }
215
+ { currUsername === username ? (
208
216
< IconButton
209
- tooltip = "Publish Template"
217
+ tooltip = "Publish Template"
210
218
style = { noPointer }
211
- onClick = { handlePublish }
212
- size = "large" >
213
- < PublishIcon fontSize = "large" />
219
+ onClick = { handlePublish }
220
+ size = "large"
221
+ >
222
+ < PublishIcon fontSize = "large" />
214
223
</ IconButton >
215
- : '' }
224
+ ) : (
225
+ ''
226
+ ) }
216
227
</ div >
217
228
</ div >
218
- < div className = "commentContainer" >
219
- { recentComments }
220
- </ div >
221
- < div className = 'commentInput' >
222
- < input type = "text" placeholder = "Add Comment" className = "commentField" onChange = { handleChange } > </ input >
223
- < AddCommentIcon className = 'commentBtn' fontSize = 'Large' onClick = { handleComment } style = { { position : 'absolute' , right : '8' , top : '13' } } />
224
- </ div >
225
- { modal }
229
+ < div className = "commentContainer" > { recentComments } </ div >
230
+ < div className = "commentInput" >
231
+ < input
232
+ type = "text"
233
+ placeholder = "Add Comment"
234
+ className = "commentField"
235
+ onChange = { handleChange }
236
+ > </ input >
237
+ < AddCommentIcon
238
+ className = "commentBtn"
239
+ fontSize = "Large"
240
+ onClick = { handleComment }
241
+ style = { { position : 'absolute' , right : '8' , top : '13' } }
242
+ />
243
+ </ div >
244
+ { modal }
226
245
</ div >
227
246
) ;
228
247
} ;
0 commit comments