Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
7480 stevensc 1
import axios from 'axios'
7386 stevensc 2
import React, { useState } from 'react'
3
import CommentTemplate from './CommentTemplate'
4
 
5
const CommentSection = ({ owner_url, owner_image, comments, comments_link_add }) => {
6
 
11227 stevensc 7
	const [inputState, setInputState] = useState('')
8
	const [commentState, setCommentState] = useState(comments)
7386 stevensc 9
 
11227 stevensc 10
	const handleSubmit = (e) => {
11
		e.preventDefault()
7428 stevensc 12
 
11227 stevensc 13
		const formData = new FormData()
14
		formData.append('comment', inputState)
7488 stevensc 15
 
11227 stevensc 16
		axios.post(comments_link_add, formData)
17
			.then(({ data }) => {
18
				if (!data.success) {
19
					return console.log('Error de envio')
20
				}
7428 stevensc 21
 
11227 stevensc 22
				setCommentState([...commentState, data.data])
23
				setInputState('')
24
			})
25
			.catch((err) => console.log(err))
26
	}
7428 stevensc 27
 
11227 stevensc 28
	const deleteComment = (id) => {
29
		setCommentState(commentState.filter((comment) => comment.unique !== id))
30
	}
7498 stevensc 31
 
11227 stevensc 32
	return (
33
		<>
34
			<div className="d-flex mb-3">
35
				<div className="avatar avatar-xs me-2">
36
					<a href={owner_url}>
37
						<img
38
							className="avatar-img rounded-circle"
39
							src={owner_image}
40
							alt="user avatar image"
41
						/>
42
					</a>
43
				</div>
11230 stevensc 44
				<form className="d-flex w-100" onSubmit={(e) => handleSubmit(e)}>
11227 stevensc 45
					<input
11233 stevensc 46
						className="form-control pe-4 bg-light mr-2"
11227 stevensc 47
						type='text'
48
						placeholder="Escribe un comentario"
49
						value={inputState}
50
						onChange={(e) => setInputState(e.target.value)}
11233 stevensc 51
						style={{height: 'auto'}}
11227 stevensc 52
					/>
53
					<button type="submit" className="btn btn-primary">Comentar</button>
54
				</form>
55
 
56
			</div>
57
			<ul className="comment-wrap list-unstyled">
58
				{
59
					commentState.map((comment) =>
60
						<li className="comment-item" key={comment.unique}>
61
							<CommentTemplate
62
								feed_comment={comment}
63
								deleteComment={deleteComment}
64
							/>
65
						</li>
66
					)
67
				}
68
			</ul>
69
		</>
70
	)
7386 stevensc 71
}
72
export default CommentSection