Newer
Older
/*Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
Departamento de Informatica - Universidade Federal do Parana
This file is part of Plataforma Integrada MEC.
Plataforma Integrada MEC is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Plataforma Integrada MEC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Plataforma Integrada MEC. If not, see <http://www.gnu.org/licenses/>.*/
import React, {useState, useContext} from 'react'
import {Store} from '../Store.js'
import styled from 'styled-components'
import Grid from '@material-ui/core/Grid';
import { Button } from '@material-ui/core';
import {Link} from 'react-router-dom'
import {apiDomain} from '../env';
import noAvatar from "../img/default_profile.png";
import Rating from '@material-ui/lab/Rating';
import StarBorderIcon from '@material-ui/icons/StarBorder';
import EditIcon from '@material-ui/icons/Edit';
import TextField from "@material-ui/core/TextField";
import Menu from '@material-ui/core/Menu';
import MenuItem from '@material-ui/core/MenuItem';
import ModalExcluir from './ModalExcluirComentario.js'

Lucas Eduardo Schoenfelder
committed
import {putRequest, deleteRequest} from './HelperFunctions/getAxiosConfig'
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
export default function Comment (props) {
/*
Required props:
rerenderCallback = callback function to trigger re-render on parent component
reviewRatings = required to update comment even though the user cannot update their rating score...
objectID = collection/learning object id
reviewID = self-explanatory I hope
authorID = author id; links to public user page
authorAvatar = either a string denoting the author's avatar file location or null
rating = star rating
name = title (?)
authorName = author username
description = the user comment itself
createdAt
recurso : boolean; determines whether to display orange or purple font
*/
var moment = require('moment')
const {state} = useContext(Store)
const [displayedComment, setDisplayedComment] = useState(props.description)
const [editando, setEditando] = useState(false)
const [anchorEl, setAnchorEl] = React.useState(null);
const handleClick = (event) => {
setAnchorEl(event.currentTarget);
};
const handleClose = () => {
setAnchorEl(null);
};
const [modalOpen, toggleModal] = useState(false)
const [comment, setComment] = useState({
error : false,
value : props.description
})
const handleChange = (e) => {
const userInput = e.target.value
const flag = (userInput.length === 0 ? true : false);
setComment({...comment, error : flag, value : userInput})
}
function handleOnSuccessfulComment (data) {
setDisplayedComment(comment.value)
setEditando(false)
props.handleSnackbar(2)
}
const updateComment = () => {
const finalComment = comment
if (!finalComment.error) {
let payload = {
"review" : {
"name":null,
"description":finalComment.value,
"pros":null,
"cons":null,
"review_ratings_attributes" : props.reviewRatings
}
}

Lucas Eduardo Schoenfelder
committed
putRequest(`/learning_objects/${props.objectID}/reviews/`, payload, handleOnSuccessfulComment, (error) => {console.log(error)})
function handleSuccessDeleteComment (data) {
props.rerenderCallback()
props.handleSnackbar(3)
}

Lucas Eduardo Schoenfelder
committed
deleteRequest(`/learning_objects/${props.objectID}/reviews/${props.reviewID}`, handleSuccessDeleteComment, (error) => {console.log(error)})
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
toggleModal(false)
}
return (
<React.Fragment>
<ModalExcluir
open={modalOpen} handleClose={() => {toggleModal(false)}}
handleConfirm={deleteComment}
/>
<Grid container style={{paddingLeft : "20px"}}>
<Grid item xs={1}>
{
props.authorID &&
<AvatarDiv>
<Link to={'/usuario-publico/' + props.authorID}>
<img src={props.authorAvatar ? apiDomain + props.authorAvatar : noAvatar} alt="author avatar"/>
</Link>
</AvatarDiv>
}
</Grid>
<Grid item xs={10}>
<Comentario>
<div className="star-rating-container">
<Rating
name="read-only"
value={props.rating}
readOnly
size="small"
style={{color:"#666"}}
emptyIcon={<StarBorderIcon fontSize="inherit" style={{color : "#a5a5a5"}} />}
/>
</div>
{
props.name &&
<strong>{props.name}</strong>
}
<div>
{
editando ?
(
<React.Fragment>
<div style={{marginTop : "5%", padding : "2px"}}>
<StyledTextField
colecao={!props.recurso}
id = "input-comentario"
label = {"Editar Comentário"}
margin = "normal"
value = {comment.value}
multiline={true}
rows="5"
onChange = {(e) => {handleChange(e)}}
style={{width:"100%"}}
/>
</div>
<div style={{float : "right"}}>
<StyledButton
style={props.recurso ? {backgroundColor : "#ff7f00"} : {backgroundColor : "#673ab7"}}
onClick={() => {setEditando(false)}}
>
Fechar
</StyledButton>
<StyledButton
style={props.recurso ? {backgroundColor : "#ff7f00"} : {backgroundColor : "#673ab7"}}
onClick={() => updateComment()}
>
Salvar
</StyledButton>
</div>
</React.Fragment>
)
:
(
<React.Fragment>
<p>
{
props.authorID &&
<Link
to={'/usuario-publico/' + props.authorID}
style={{
fontWeight : "bolder",
color : props.recurso ? "#ff7f00" : "#673ab7"
}}
>
{props.authorName}
</Link>
}
: {displayedComment}
</p>
{
props.authorID !== state.currentUser.id &&
<span className="date">
{moment(props.createdAt).format("DD/MM/YYYY")}
</span>
}
</React.Fragment>
)
}
</div>
</Comentario>
</Grid>
{
props.authorID === state.currentUser.id &&
<Grid item xs={1}>
<StyledDiv>
<Button onClick={handleClick}><EditIcon/></Button>
<Menu
id="simple-menu"
anchorEl={anchorEl}
keepMounted
open={Boolean(anchorEl)}
onClose={handleClose}
>
<MenuItem onClick={() => {setEditando(true); handleClose()}}>Editar</MenuItem>
<MenuItem onClick={() => {toggleModal(true);handleClose()}}>Excluir</MenuItem>
</Menu>
</StyledDiv>
</Grid>
}
</Grid>
</React.Fragment>
)
}
const StyledTextField = styled(TextField)`
label.Mui-focused {
color : ${props => props.colecao ? "rgb(103,58,183)" : "rgb(255,127,0)"};
}
.MuiInput-underline::after {
border-bottom: ${props => props.colecao ? "2px solid rgb(103,58,183)" : "2px solid rgb(255,127,0)"};
}
`
const StyledDiv = styled.div`
text-align : center;
.MuiButton-root {
@media screen and (max-width: 990px) {
padding-right : 35px !important;
}
}
`
const StyledButton = styled(Button)`
color : rgba(255,255,255,0.87) !important;
box-shadow : 0 2px 5px 0 rgba(0,0,0,.26) !important;
margin : 6px 8px !important;
font-weight : 600 !important;
`
const Comentario = styled.div`
@media screen and (max-width: 990px) {
padding-left : 55px !important;
}
font-size : 14px;
.star-rating-container {
width : 100px;
}
p {
margin : 0 0 10px;
padding-left : 2px
}
a {
text-decoration : none !important;
}
.date {
color : #ababab;
font-size : 12px;
font-weight : lighter;
padding-left : 3px;
}
`
const AvatarDiv = styled.div`
text-align : center;
float : left;
position : relative;
width : 65px;
height : 65px;
a {
text-decoration : none !important;
}
img {
width : 100% !important;
height : 100% !important;
border-radius : 100%
vertical-align : middle;
}
`