forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentCard.jsx
More file actions
139 lines (127 loc) · 4.18 KB
/
Copy pathCommentCard.jsx
File metadata and controls
139 lines (127 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
import { useState } from 'react';
import { useApp } from '../../context/AppContext';
import apiService from '../../services/api';
const CommentCard = ({ comment, postId, onCommentUpdated, onCommentDeleted }) => {
const [isEditing, setIsEditing] = useState(false);
const [editUsername, setEditUsername] = useState(comment.username);
const [editContent, setEditContent] = useState(comment.content);
const [isUpdating, setIsUpdating] = useState(false);
const [isDeleting, setIsDeleting] = useState(false);
const { handleError, setServerOnline } = useApp();
const handleEdit = () => {
setIsEditing(true);
setEditUsername(comment.username);
setEditContent(comment.content);
};
const handleCancelEdit = () => {
setIsEditing(false);
setEditUsername(comment.username);
setEditContent(comment.content);
};
const handleUpdate = async (e) => {
e.preventDefault();
if (!editUsername.trim() || !editContent.trim()) {
handleError(new Error('Username and content are required'));
return;
}
setIsUpdating(true);
try {
const updatedComment = await apiService.updateComment(postId, comment.id, {
username: editUsername.trim(),
content: editContent.trim(),
});
setServerOnline();
setIsEditing(false);
onCommentUpdated?.(updatedComment);
} catch (error) {
handleError(error);
} finally {
setIsUpdating(false);
}
};
const handleDelete = async () => {
if (!window.confirm('Are you sure you want to delete this comment?')) {
return;
}
setIsDeleting(true);
try {
await apiService.deleteComment(postId, comment.id);
setServerOnline();
onCommentDeleted?.(comment.id);
} catch (error) {
handleError(error);
} finally {
setIsDeleting(false);
}
};
const formatDate = (dateString) => {
return new Date(dateString).toLocaleString();
};
return (
<div className="comment-card">
{isEditing ? (
<form onSubmit={handleUpdate} className="edit-comment-form">
<div className="form-group">
<input
type="text"
value={editUsername}
onChange={(e) => setEditUsername(e.target.value)}
placeholder="Username"
disabled={isUpdating}
required
className="edit-username-input"
/>
</div>
<div className="form-group">
<textarea
value={editContent}
onChange={(e) => setEditContent(e.target.value)}
placeholder="Comment content"
rows={3}
disabled={isUpdating}
required
className="edit-content-input"
/>
</div>
<div className="form-actions">
<button type="submit" disabled={isUpdating} className="save-button">
{isUpdating ? 'Saving...' : 'Save'}
</button>
<button type="button" onClick={handleCancelEdit} disabled={isUpdating} className="cancel-button">
Cancel
</button>
</div>
</form>
) : (
<>
<div className="comment-header">
<div className="comment-meta">
<span className="username">@{comment.username}</span>
<span className="date">{formatDate(comment.createdAt)}</span>
{comment.updatedAt !== comment.createdAt && (
<span className="updated">(edited {formatDate(comment.updatedAt)})</span>
)}
</div>
<div className="comment-actions">
<button onClick={handleEdit} className="edit-button" title="Edit comment">
✏️
</button>
<button
onClick={handleDelete}
disabled={isDeleting}
className="delete-button"
title="Delete comment"
>
{isDeleting ? '⏳' : '🗑️'}
</button>
</div>
</div>
<div className="comment-content">
<p>{comment.content}</p>
</div>
</>
)}
</div>
);
};
export default CommentCard;